Mike Miller
Mike Miller

Reputation: 263

Running matlab code through a folder

I have the following code for which instead of loading one image at a time, I'd like to run through every image in a folder (the defective folder in this code). I'd like the output to be an array containing the values of 'G' for each of the input images. I'm not too sure how to go about this - so any points appreciated. Many thanks!

%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)    

Upvotes: 0

Views: 104

Answers (1)

kkuilla
kkuilla

Reputation: 2256

Are you looking for the dir command?

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

    ....

    G = sum(F);
 end

Upvotes: 1

Related Questions