MATLAB Answers

reading multiple images in a folder (imread)

310 views (last 30 days)
Ok so I want to read 10 different images into matrices in Matlab. the images are saved as image001, image002 etc. up to image010. I tried this loop thinking it would work:
for n=1:10
image_{n}= imread(sprintf('image00%s.png',num2str(n)));
end
but it came up with the error:
??? Error using ==> strfind
Input strings must have one row.
Error in ==> imread at 340
if (strfind(filename, '://'))
it also would read the last image as image0010, which is not right. I then thought I could just do this:
for r= 1:10
sprintf('scan_%s', num2str(r)) = imread('*.png','png');
end
Because i thought that by putting "*.png" it would read all the images in the folder. But I go this error:
?? Error using ==> imread at 408
Can't open file "*.png" for reading;
you may not have read permission.
can someone see where I went wrong in my coding or suggest a better way to do it?
Thanks

  1 Comment

Udaynarayansingh Kushwaha
Udaynarayansingh Kushwaha on 30 Aug 2019
% I have the code that read only 4 images and show using subplot function. But can someone suggest me that how I show more then 4 images in matlab without subplot function.
clc;
clear all;
for i=1:4
FileName=uigetfile({'.'});
im=imread(FileName);
subplot(2,2,i);
imshow(im);
end

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 May 2011
for n=1:10
images{n} = imread(sprintf('image%03d.png',n));
end
See also this FAQ and this one

  1 Comment

Jack Williams
Jack Williams on 13 May 2011
perfect so i assume the '%03d' makes 1 display as 001 and 10 010?

Sign in to comment.

More Answers (2)

Prateek Srivastava
Prateek Srivastava on 18 Dec 2016
I am trying to make the code/Script which could do following task:-- 1.>It could read all images in a given folder(Ex:-102 Images). 2.>It could crop the images by using Imcrop function and its (x,y,width,height) would be exported from a matlab table in a loop. 3.>Cropped Images to be put down in different folder.
This code I had made it but not working.
xdatatemp = Help5(:,[10,11,12,13,19]); datapath_to_be_cropped='E:\EXTRACT\Motorcycle\COMPLETED_20160625191937\cc_traffic light_1466853840396\rear_raw_images'; datapath_cropped='E:\EXTRACT\Motorcycle\COMPLETED_20160625-191937\cc_traffic_light_1466853840396\rear_raw_images\cropped image'; J=xdatatemp; allFiles = dir(datapath_to_be_cropped); fileNames2 = {allFiles(~[allFiles.isdir]).name}; nbofimginfolder = size(fileNames2,2); h_im=figure(); h = waitbar(0,'Applying cropping...'); for n=1:nbofimginfolder waitbar(n / nbofimginfolder,h,sprintf('Applying Crop ... (%n/%n)',i,nbofimginfolder)) for J=[1,2,3,4]; for x=1:562; for y=1:335; for width=1:41; for height=1:84; srcFiles = dir('E:\EXTRACT\Motorcycle\Pos*.jpg'); for i = 1 : length(srcFiles) filename = strcat('E:\EXTRACT\Motorcycle\Pos*.jpg',srcFiles(i).name); I = imread(filename); figure, imshow(I); end end % I=imread('E:\EXTRACT\Motorcycle\COMPLETED_20160625-191937\cc_traffic_light_1466853840396\rear_raw_images\rear_raw_images'); I2=imcrop(I,[x,y,width,height]); subplot(1,2,1) imshow(I) title('Original Image') subplot(1,2,2) imshow(I2) title('Cropped Image') end end end end end
data_str = strrep(strrep(datestr(datetime),' ','_'),':','_'); save(fullfile(datapath_to_be_cropped, ['SVM_HOG__',data_str,'.mat']),'SVMModel','SVMinfo'); %% Adjust your path fprintf('saved as: %s %%\n',fullfile(datapath_SVMmodels, [data_str,'__SVMModel.mat']))

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 18 Dec 2016
Your line
filename = strcat('E:\EXTRACT\Motorcycle\Pos*.jpg',srcFiles(i).name);
Should not have the 'Pos*.jpg' part of the string.
I recommend that you learn to use fullfile()

  0 Comments

Sign in to comment.

Categories