0

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)    
Mike Miller
  • 253
  • 6
  • 18
  • you could use [`dir`](http://www.mathworks.com/help/matlab/ref/dir.html?searchHighlight=dir) to get the image filenames and run your code in a for loop. – Cecilia Mar 11 '15 at 17:22
  • 3
    Something like this? http://stackoverflow.com/questions/11621846/loop-through-files-in-a-folder-in-matlab?rq=1 – beaker Mar 11 '15 at 17:22
  • @beaker I could be misremembering, but I think I remember something that runs through files in a folder using '$' - like file0$.png for example and running $ from 0-9. – Mike Miller Mar 11 '15 at 17:29
  • 1
    @MikeMiller Hmm... wildcard completion... I'll poke around and see if I can find anything... – beaker Mar 11 '15 at 17:30
  • @MikeMiller All I can find is using a regex to filter the file list received from `dir`. I don't see any direct way of loading all images at once. Which is totally understandable, it would be a huge memory hog. – beaker Mar 11 '15 at 17:45
  • @beaker I can't remember the application I used it for, but it wasn't loading all of the images at once, it would load the image, run it through the loop, output and repeat for the next image in the folder etc. I've had a google, and I can't find anything similar to what I'm talking about. Maybe it was a lucid dream :-) – Mike Miller Mar 11 '15 at 17:48

1 Answers1

1

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
rayryeng
  • 102,964
  • 22
  • 184
  • 193
kkuilla
  • 2,226
  • 3
  • 34
  • 37