Clear Filters
Clear Filters

Read in multiple .pmg files and perform imread(), im2double(), and imgradient() Principle component analysis

2 views (last 30 days)
I read in the file names fine for the multiple images but I cannot seem to get the latter parts to work here is my code:
Using the recommended code below I got this to finally work, but I am not sure if I am using the princomp function correctly for performing the principle component analysis on the images
% Load Images
filePattern = 'C:\Users\Morgan Weiss\Documents\MATLAB\STA5635_HW12\faces\*.pgm';
fileList = dir(filePattern); % Will not contain any directories, only .pgm files.
for k = 1:length(fileList)
thisFileName = fileList(k).name;
thisImage = imread(thisFileName);
drawnow;
% Get the gradient of this image.
A = imgradient(thisImage);
% Now do something with Gmag and Gdir....
[COEFF,SCORE,latent] = princomp(A);
end

Answers (2)

David Barry
David Barry on 6 Dec 2016
Edited: David Barry on 6 Dec 2016
Don't forget that the dir command will also return two entries for current directory and parent directory (. and ..) so you can't simply loop over everything that dir returns. Also, if your folder contains files other than images then you will need to filter those out by checking for file extension for example.

Image Analyst
Image Analyst on 6 Dec 2016
You can loop over all files and call imread(). So what's the remaining problem(s)? You don't need to call im2double() - I never do. And you can call imgradient() if you want - just read the help on it, so that's no big deal - you should be able to do that. What exactly do you need help with?
By the way, you don't need to store all your images in the loop in a cell array, unless you'd need them later after the loop. You can certainly call imgradient on the images inside the loop if you want.
% Process all *.PGM images by getting the gradient.
filePattern = 'C:\Users\Morgan Weiss\Documents\MATLAB\STA5635_HW12\faces\*.pgm';
fileList = dir(filePattern); % Will not contain any directories, only .pgm files.
for k = 1:length(fileList)
thisFileName = fileList(k).name
thisImage = imread(thisFileName);
imshow(thisImage);
drawnow;
% Get the gradient of this image.
[Gmag,Gdir] = imgradient(thisImage);
% Now do something with Gmag and Gdir....
end
  6 Comments
Image Analyst
Image Analyst on 6 Dec 2016
thisFileName is only the base file name. You need to prepend the folder with fullfile() to get the full file name.
I haven't used princomp() before.

Sign in to comment.

Categories

Find more on Agriculture in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!