# New image batch processor app in R2015a6

Posted by Steve Eddins,

Nine years ago I wrote a blog post showing how to do batch processing of image files. That is, I showed how to use MATLAB to perform the same operation on a bunch of image files in a particular folder.

The basic procedure is:

1. Get a list of filenames.
2. Determine the processing steps to follow for each file.
3. Put everything together in a for loop.

The processing steps for each file typically looked like this:

1. Read in the data from the file.
2. Process the data.
3. Construct the output filename.
4. Write out the processed data.

I showed a sample processing loop that cropped and resized a bunch of images the same way:

files = dir('*.JPG');
for k = 1:numel(files)
rgb = rgb(1:1800, 520:2000, :);
rgb = imresize(rgb, 0.2, 'bicubic');
imwrite(rgb, ['cropped\' files(k).name]);
end

You can still do it exactly this way today, of course, in 2015. But you really might want to use the new Image Batch Processor App instead:

This is a very nice app that was just added to the Image Processing Toolbox in R2015a (released earlier this year, in March). If you look at the tool strip from left to right, you can see the entire workflow laid out for you.

1. Load the images. You can specify the folder containing your data.
2. What function do you want to run on each image? You can specify an existing function, or you can let the app make a shell function for you, and then you can fill in the details.
3. Where do you want to put the output? Here you can say you just want to overwrite the input files. (Be careful!)
4. Do you want to use a parallel cluster?
5. Do you want to process all the files, or just a selection?
6. Do you want to generate some code so you can automate the entire process?

I encourage you to give it a try. The toolbox documentation gives you more details for getting started.

Get the MATLAB code

Published with MATLAB® R2015a

### Note

Steve Eddins replied on : 1 of 6

Clarification: I just learned that the app doesn’t let you overwrite the input files.

Steve Eddins replied on : 3 of 6

Michael—The doc page you linked to shows the basic usage. You create an imageSet based on particular folder location, and then you can call read (probably in a for loop) to read and then process all the images one at a time. Do you have a specific question about its use?

michael replied on : 4 of 6

i can run it for example i converted rgb2gray and see the output .
after generated the code there was function
[imageFiles, subDirs] = getAllImages(inDir,includeSubdirectories)
so it is similar to read all use imageSet so i believe it is different approach for processing multiple images.

Parul Tandon replied on : 5 of 6

Dear Sir,

This is actually in response to your previous blog, the link to which Ive pasted here (to avoid any confusion ) http://blogs.mathworks.com/steve/2006/06/06/batch-processing/.

I was trying to modify your code for batch processign of image files for my porject work.I have 1300 text files with header lines and numeric data in them.I need a matlab program to read and extract all of them .

Ive pasted the modified code and the error I get upon running it below.Kindly help me correcting it as Im very new to MATLAB.

files = dir('*.n2o');
numel(files) = 1301;

for k = 1:numel(files)
fid = fopen('files(k)', 'rt');
Data = textscan(fid, '%f %f %f %f %f', 'headerLines', 43, 'CollectOutput', true);
fclose(fid);
Data= cell2mat( Data);
end
`
Steve Eddins replied on : 6 of 6

Parul—The second line, numel(files) = 1301;, is a mistake. numel is a function that returns the number of elements of an array. You can’t assign to it like that. You can delete that line.

The first line in the body of the for loop contains another error. Don’t put 'files(k)' in quotes like that. Also, it should be files(k).name.

I recommend that you use MATLAB Answers as a resource for posting MATLAB coding questions like this. When you do, be sure to post the details of any error message you receive when running your code.