2010-06-17 60 views
2

我有一个项目,可以检测图片中的对象,它使用反向传播尺度共轭梯度进行训练。我使用了10个组件进行输入。 r,g,b,标准偏差,熵,阈值(Onsu方法),包含对比度,均匀性,相关性和能量的glcm。我手动提取它们。我有100个输入。 50个对象,50个不是对象。它仍然很难保持手动方法。所以我想使用循环和数组。我使用2文件夹的文件对象,而不是对象。如何提取2个文件夹中的文件? 第一个文件夹:C:\ Documents and Settings \ User \ My Documents \ MATLAB \ object 第二个文件夹:C:\ Documents and Settings \ User \ My Documents \非对象纹理分析统计,检测对象在matlab中的特征提取

这是我的编码。我手动写入他们直到100.你能帮我把它们分组并循环它们吗?


kl=imread('1.jpg'); 
g=rgb2gray(kl); 
rgb=mean(mean(kl)); 
r1=rgb(:,:,1); 
g1=rgb(:,:,2); 
b1=rgb(:,:,3); 
std1=std2(g); 
entropy1=entropy(g); 
tres=graythresh(g); 
glcm=graycomatrix(g); 
F=graycoprops(glcm,{'Contrast','Homogeneity','Correlation','Energy'}); 

我希望你能给出解决方案。请帮助我。

回答

3

如果您所有的文件被命名为1.JPG,2.JPG,......,那么你就可以做到以下几点:


for i = 1:50 
    fileName = sprintf('%d.jpg', i); 
    kl = imread(fileName); 

    ... the rest of your code... 

end 
3

如果,除了使用@Dima遍历图像中的每个目录的解决方案,您还想循环两个目录,您可以执行以下操作:

dirNames = {'C:\Documents and Settings\User\My Documents\MATLAB\object','C:\Documents and Settings\User\My Documents\non object'}; 

for directory = dirNames %# loops directly over the elements of the cell array dirNames 
    fileList = dir(fullfile(directory{1},'*.jpg')); %# list all jpgs in the directory 
    for iFile = 1:length(fileList) 
     %# read the ith file 
     kl = imread(fullfile(directory{1},fileList(iFile).name)); 

     %# do the calculations here 

    end 
end