How to batch process images from folder

Greetings, i have 100 signature images on a folder which would be preprocessed with this code
if true
I=imread('1.png');
%figure,imshow(I);
img=imresize(I,[256 ,256]);
%figure,imshow(img);
Im=rgb2gray(img);
Im=im2double(Im);
Im=im2bw(Im);
Im = bwmorph(~Im, 'thin',inf);
Im=~Im;
%figure,imshow(Im);
xstart=256;
xend=1;
ystart=256;
yend=1;
for y=1:256
for x=1:256
if((Im(y,x)==0)) %(y,x)
if (y<ystart)
ystart=y;
end
if((y>yend))
yend=y;
end
if (x<xstart)
xstart=x;
end
if (x>xend)
xend=x;
end
end
end
end
for i=ystart:yend
for j=xstart:xend
im((i-ystart+1),(j-xstart+1))=Im(i,j);
end
end
%figure,imshow(im);
end
what i wanted to ask is how do i batch call all those images and preprocess them, i've readed this question http://www.mathworks.com/matlabcentral/answers/7342-reading-multiple-images-in-a-folder-imread but it seems it can only be used if the name is the same eg.(image001,image002...) which is not the case with my images. they're all of the same file extension but have 10 different names eg(dimas1,fakedimas1...)
Thank you and sorry for the bad english

 Accepted Answer

See this nice framework that handles a lot of the stuff for you: http://www.mathworks.com/matlabcentral/fileexchange/24224-magic-matlab-generic-imaging-component
Or if you want to do it yourself, not quite so fancy and nice, use the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
The code snippet you want is the second one.

7 Comments

hello there, thanks for answering. I've actually tried that code earlier but i noticed that it would be pretty much needed to have files with fixed name eg(image1,image2...). in which i'm more hoping if there's anyway to select all the images without changing the files name.
No. Not true. Did you look at the second snippet like I said? It has this:
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
Where in there does it say anything about the filenames starting with "image"? Nowhere.
sorry, i mistook the meaning of "snippet" as the second code from first snippet and thank you! from that code you point out i managed to create this one
Files = dir('C:\Users\Dimas\Documents\MATLAB\*.png');
for I = 1 : length(Files)
filename = strcat('C:\Users\Dimas\Documents\MATLAB\',Files(I).name);
Im{I} = imread(filename);
end
is there a shorter way to do this ?
Doesn't look like the FAQ much anymore. Anyway, you can pull the folder name out of the loop and use fullfile() like you should instead of strcat():
folder = 'C:\Users\Dimas\Documents\MATLAB\';
for I = 1 : length(Files)
fullFileName = fullfile(folder, Files(I).name);
Im{I} = imread(fullFileName);
end
Not sure why you're storing all of the images in a cell array. Is there some reason why you're doing that???
it's to use them in the code above and the struct would be used to label the images back after the process, the whole code is a part of my to-be signature verification using perceptron system.
for the code i believe there should be
Files = dir(folder);
since Files won't be defined otherwise
apparently upon testing this i would get "Error using imread (line 347) Cannot open file "C:\Users\Dimas\Documents\MATLAB\2nd\." for reading. You might not have read permission."
Yes of course you still need dir(). You got the error because you didn't follow the FAQ. You will get ALL files, even . and .. and folders, not just image files because you did not specify a file pattern. Either specify a file pattern like the FAQ showed you or you'll have to use isdir(fullFileName) to check whether or not the file is a folder instead of a file. Obviously using a file pattern is simplest.
whoops was a bit confused first time since dir would only accept one input arguments once again thanks for pointing it out

Sign in to comment.

More Answers (1)

You could use the image batch processing app in recent releases. Either from the apps tab or:
>>imageBatchProcessor

1 Comment

Dimas Riansa
Dimas Riansa on 24 May 2016
Edited: Dimas Riansa on 24 May 2016
it seems like mine R2014b doesn't have it

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!