Read in multiple .pmg files and perform imread(), im2double(), and imgradient() Principle component analysis
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
David Barry
el 6 de Dic. de 2016
Editada: David Barry
el 6 de Dic. de 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.
1 comentario
Walter Roberson
el 6 de Dic. de 2016
After
Files = dir(a);
add
Files([Files.isdir]) = [];
That will delete all directory entries including . and ..
Image Analyst
el 6 de Dic. de 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 comentarios
Image Analyst
el 6 de Dic. de 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.
Ver también
Categorías
Más información sobre Image Segmentation and Analysis en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!