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)    
6
  • you could use dir to get the image filenames and run your code in a for loop. Commented Mar 11, 2015 at 17:22
  • 3
    Something like this? stackoverflow.com/questions/11621846/… Commented Mar 11, 2015 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. Commented Mar 11, 2015 at 17:29
  • 1
    @MikeMiller Hmm... wildcard completion... I'll poke around and see if I can find anything... Commented Mar 11, 2015 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. Commented Mar 11, 2015 at 17:45

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.