Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Steve on Image Processing

June 6th, 2006

Batch processing

A couple of months ago I was working with a bunch of pictures I had taken at home. I had about 40 of them, and I needed to crop and resize them all the same way. Naturally, I wrote a MATLAB script to do it.

This experience reminded me that customers sometimes ask "How do I use the Image Processing Toolbox to do batch processing of my images?" Really, though, this isn't a toolbox question; it's a MATLAB question. In other words, the basic MATLAB techniques for batch processing apply to any domain, not just image processing.

Contents

Step 1: Get a list of filenames

If you use the dir function with an output argument, you get back a structure array containing the filenames, as well as other information about each file.

Let's say I want to process all files ending in .jpg:

files = dir('*.jpg')
files = 
42x1 struct array with fields:
    name
    date
    bytes
    isdir

The files struct array has 42 elements, indicating that there are 42 files matching "*.jpg" in the current directory. Let's look at the details for a couple of these files.

files(1)
ans = 
     name: 'IMG_0175.jpg'
     date: '12-Feb-2006 10:49:30'
    bytes: 962477
    isdir: 0
files(end)
ans = 
     name: 'IMG_0216.jpg'
     date: '12-Feb-2006 11:09:10'
    bytes: 1004842
    isdir: 0

Step 2: Determine the processing steps to follow for each file

There are four basic steps to follow for each file:

1. Read in the data from the file.

2. Process the data.

3. Construct the output filename.

4. Write out the processed data.

Here's what my read and processing steps looked like:

rgb = imread('IMG_0175.jpg');  % or rgb = imread(files(1).name);
rgb = rgb(1:1800, 520:2000, :);
rgb = imresize(rgb, 0.2, 'bicubic');

You have many options to consider for constructing the output filename. In my case, I wanted to use the same name but in a subfolder:

output_name = ['cropped\' files(1).name] % Use fullfile instead if you want
                                         % multiplatform portability
output_name =
cropped\IMG_0175.jpg

Here's another example of output name construction. You might use something like this if you want to change image formats.

input_name = files(1).name
input_name =
IMG_0175.jpg
[path, name, extension] = fileparts(input_name)
path =
     ''
name =
IMG_0175
extension =
.jpg
output_name = fullfile(path, [name '.tif'])
output_name =
IMG_0175.tif

Step 3: Put everything together in a for loop

Here's my complete batch processing loop:

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


Get the MATLAB code

Published with MATLAB® 7.2

13 Responses to “Batch processing”

  1. shan replied on :

    hope you would kindly consider my problem and give me a solution for it.
    thank you

  2. zahid replied on :

    above program is not workin

  3. Steve replied on :

    zahid - Unless you provide a lot more detail about what problem you are having, I can’t possibly help you.

  4. Raja R replied on :

    its very useful to develop my self in image processing .

    thanking you sir.

    with warm regards
    R.Raja

  5. Lucy replied on :

    Hi Steve
    I am trying to run part of your for loop for batch processing a folder of 52 images. The only part I am interested in is the
    reading a batch of images in. That is I want to extract area of an object as opposed to cropping. So I started with the beginning of your code
    files = dir(’F:\EPD Validation Study\Lab Processing from Field Trip 221107\Boags A\greater than 2\4\Boags 4 Lab JPGS Cropped\*.jpg’)

    for k = 1:numel(files)
    rgb = imread(files(k).name)
    end

    but I receive an error message “??? Error using ==> imread at 315
    File “R0010864.JPG” does not exist.”

    R0010864.jpg is the first image file in the folder “Boags 4 Lab JPGS Cropped”.
    I am not sure what I am doing wrong. These images are rgb and are large in size (2000 x 2000 pix)and are being stored on an external hard drive.
    I would appreciate any helpful suggestions/solutions you might have.
    Thanks
    Lucy

  6. Steve replied on :

    Lucy—If you look at files(k).name, you’ll see that it doesn’t include the full path. You either need to modify files(k).name to prepend the full path, or add the image directory to the MATLAB path, or make that directory your working directory.

  7. Lucy replied on :

    Thanks for your help Steve. I shifted my images into the current (working) directory and used the code listed below:

    files = dir(’*.JPG’);
    for k = 1:(numel(files));
    rgb = imread(files(k).name)
    end

    and I received the following error message:

    ??? Error using ==> imagesci\private\readjpg
    Too many input arguments.

    Error in ==> imread at 389
    [X, map] = feval(fmt_s.read, filename, extraArgs{:});

    This appears to be a syntax error that may be associated with the wrong file format? (my image is class unit 8 it is not an indexed color map). But I also initially though that this loop would read all images into the workspace (at once) but I have since realized that this cannot be the case as there is only a single variable rgb. Therefore each image must be read in one at a time and once the algorithm is applied to the image (or any other functions in the for loop) it will move onto the next image file in the directory. Am I on the right page or have I misunderstood what this code is doing?

  8. Steve replied on :

    Lucy—I don’t see any problem with your code, except that you left off the semicolon on the imread line. That will cause the pixel values (all of them!) to display in the command window. I recommend that you use the debugger to help with your debugging problem. Single-step through the code, and in particular step into imread. Examine the input arguments to imread to make sure they are what you expect. And yes, your loop (once debugged) will read all the images into the same variable, one at a time. If that’s not what you want, consider using a cell array instead.

  9. Lucy replied on :

    Thanks Steve. Problem Solved!

  10. Manu replied on :

    Hi Steve,

    can I import a big number of MODIS images in HDF-EOS format whithout using HDF-toolbox?
    Thank in advance, Manu

  11. Steve replied on :

    Manu—MATLAB supports HDF-EOS import and export via the functions hdfgd, hdfpt, hdfsw. I don’t know what you mean by “HDF Toolbox.” There’s no MathWorks product with that name.

  12. Lakshman N replied on :

    Thanks so much Steve.You are simply the best.I knew the problem was simple but just did not know where to look for.I googled “matlab code to read all images in a folder” and got the best result.its quite surprising to see that the crux of this problem is just one line of code, namely, “files = dir(’*.JPG’);”.

  13. Steve replied on :

    Lakshman—I’m glad you found this information to be useful.

Leave a Reply


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Assaf: Hi, I have 3 questions regarding the following sentence: “Rather than using the power spectrum from a...
  • Steve: Cara—I do not understand your question. Can you clarify it?
  • Cara Schiek: Hi. How could I do this same thing with image vales within the patches? cara
  • Steve: Vincent—OK, thanks for the information.
  • Vincent: My only data point on multithreading is that the Performance tab of the Task Manager shows increased CPU...
  • Steve: Vincent—Thanks for giving it a try and reporting back. I’m a bit skeptical that multithreading...
  • Vincent: Oops numbers were wrong. Data set was 450MB large so the numbers are: Results: ImageJ alone =< 5 s (or 90...
  • Vincent: Steve- I just had a quick run at the new imread.m patch. It’s much faster than the previous version...
  • Steve: Erik—Also, separability of the kernel provides no speed benefit in FFT-based implementations....
  • Steve: Erik—Good questions. Remember that, practically speaking, when we filter a 2-D signal with a 1-D filter...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics