Read in multiple .pmg files and perform imread(), im2double(), and imgradient() Principle component analysis
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
David Barry
2016-12-6
编辑:David Barry
2016-12-6
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.
1 个评论
Walter Roberson
2016-12-6
After
Files = dir(a);
add
Files([Files.isdir]) = [];
That will delete all directory entries including . and ..
Image Analyst
2016-12-6
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 个评论
Image Analyst
2016-12-6
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!