通过文件夹运行matlab代码

时间:2015-03-11 17:15:31

标签: matlab

我有以下代码,而不是一次加载一个图像,我想浏览文件夹中的每个图像(此代码中的缺陷文件夹)。我希望输出是一个包含' G'的值的数组。对于每个输入图像。我不太清楚如何解决这个问题 - 所以任何一点都值得赞赏。非常感谢!

%PCA code, 
img = imread('C:\users\m7-miller\desktop\250images\defective\inkblob01.png');   
img_gray = rgb2gray(img);       
img_gray_double = im2double(img_gray);     
figure, 
set(gcf,'numbertitle','off','name','Grayscale Image'),  
imshow(img_gray_double) 

%find mean of the image
img_mean = mean(img_gray_double);  
[m n] = size(img_gray); 

%Make column vector of mean image value
new_mean = repmat(img_mean,m,1); 

%Mean corrected image
Corrected_img = img_gray_double - new_mean; 

%Covariance matrix of corrected image
cov_img = cov(Corrected_img);

%Eigenvalues of covariance matrix - columns of V are e-vectors, 
%diagonals of D e-values

[V, D] = eig(cov_img); 
V_T = transpose(V); 
Corrected_image_T = transpose(Corrected_img);  
FinalData = V_T * Corrected_image_T;   

% Image approximation by choosing only a selection of principal components

PCs = 3;                    
PCs = n - PCs;                                                         
Reduced_V = V;  

for i = 1:PCs,                                                         
Reduced_V(:,1) =[]; 
end 

Y=Reduced_V'* Corrected_image_T;                                        
Compressed_img = Reduced_V*Y;                                           
Compressed_img = Compressed_img' + new_mean; 

figure,                                                                
set(gcf,'numbertitle','off','name','Compressed Image'),  
imshow(Compressed_img) 
% End of image compression 

% Difference of original image and compressed
S = (img_gray_double - Compressed_img);
figure,                                                                
set(gcf,'numbertitle','off','name','Difference'),  
imshow(S) 

% Sum of the differences
F = sum(S);
G = sum(F)    

1 个答案:

答案 0 :(得分:1)

您是否在寻找 dir 命令?

files = dir('*.png');
 for n=1:size(files,1)
    filename = files(n).name;
    img = imread(filename);

    ....

    G = sum(F);
 end