How to read mutliple images in a for loop in MATLAB?

孤街醉人 提交于 2020-01-11 10:39:12

问题


I have segmented results in a folder. Those need to be read in a for loop and processed further in the loop. I tried reading as below:

for i=1:10 
file_name=dir(strcat('C:\Users\adminp\Desktop\dinosaurs\')); 
  im=imread(strcat('C:\Users\adminp\Desktop\dinosaurs\',file_name(i).name));
  %processing of read image
end

An error was thrown :??? Error using ==> imread at 370 Can't open file "C:\Users\adminp\Desktop\dinosaurs\." for reading; you may not have read permission.

Please suggest where I have gone wrong.


回答1:


I guess your problem is that:

file_name(1).name = .     % Stands for current directory
file_name(2).name = ..    % Stands for parent directory
file_name(3).name = your_file_name.jpg

Now, do:

images = dir('*JPG')
for i=1:numel(images) 
file_name=dir(strcat('C:\Users\adminp\Desktop\dinosaurs\')); 
  im=imread(strcat('C:\Users\adminp\Desktop\dinosaurs\',images(i).name));
  %processing of read image
end    



回答2:


If you have the R2014b release of MATLAB with the Computer Vision System Toolbox, you can do this in one line using the imageSet object.

images = imageSet('C:\Users\adminp\Desktop\dinosaurs\');

will create an object containing the paths to all the images in the dinosaurs directory. It will exclude any non-image files automaticaly.

Then you can process your images as follows

for i = 1:images.Count
  im = read(images, i);
  % process the image
end


来源:https://stackoverflow.com/questions/27978817/how-to-read-mutliple-images-in-a-for-loop-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!