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:
- Get a list of filenames.
- Determine the processing steps to follow for each file.
- Put everything together in a for loop.
The processing steps for each file typically looked like this:
- Read in the data from the file.
- Process the data.
- Construct the output filename.
- 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 = imread(files(k).name); 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.
- Load the images. You can specify the folder containing your data.
- 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.
- Where do you want to put the output? Here you can say you just want to overwrite the input files. (Be careful!)
- Do you want to use a parallel cluster?
- Do you want to process all the files, or just a selection?
- 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
6 CommentsOldest to Newest
Clarification: I just learned that the app doesn’t let you overwrite the input files.
hi how you combine the new command imageSet with batch processing
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?
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.
This is actually in response to your previous blog, the link to which I`ve 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 .
I`ve pasted the modified code and the error I get upon running it below.Kindly help me correcting it as I`m 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
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.