필터 지우기
필터 지우기

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

조회 수: 2 (최근 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

답변 (2개)

David Barry
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.

Image Analyst
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
Morgan Weiss
Morgan Weiss 2016년 12월 6일
I got it to work now but I am not sure if I am using the princomp function correctly
Image Analyst
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 CenterFile Exchange에서 Agriculture에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by