Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

 

Image Processing Toolbox 6.4

Batch Processing Image Files in Parallel

A common image processing task is to apply an image processing algorithm to a series of files. This procedure can be time consuming if the algorithm is computationally intensive, if you are processing a large number of files, or if the files are very large. This demo shows how to batch process a set of image files in parallel.

Batch processing performance can be improved if you have the Parallel Computing Toolbox™ software.

Contents

Get the Names of Files to be Processed

List the filenames with a common prefix. In this example, the image files are a set of 10 microscope images of rat prostate cancer cells. These files are only the first 10 of 100 images that were acquired.

p = which('AT3_1m4_01.tif');
filelist = dir([fileparts(p) filesep 'AT3_1m4_*.tif']);
fileNames = {filelist.name}'
fileNames =

    'AT3_1m4_01.tif'
    'AT3_1m4_02.tif'
    'AT3_1m4_03.tif'
    'AT3_1m4_04.tif'
    'AT3_1m4_05.tif'
    'AT3_1m4_06.tif'
    'AT3_1m4_07.tif'
    'AT3_1m4_08.tif'
    'AT3_1m4_09.tif'
    'AT3_1m4_10.tif'

View one of the images.

I = imread(fileNames{1});
imshow(I)
text(size(I,2),size(I,1)+15, ...
    'Image files courtesy of Alan Partin', ...
    'FontSize',7,'HorizontalAlignment','right');
text(size(I,2),size(I,1)+25, ...
    'Johns Hopkins University', ...
    'FontSize',7,'HorizontalAlignment','right');

Define Image Processing Algorithm to Use on Each Image

Create an image processing function that segments non-touching cells in the microscope images. Test the function on one of your images.

segmentedCells = ipexbatchDetectCells(I);
figure, imshow(segmentedCells)

To view ipexbatchDetectCells use this command.

type ipexbatchDetectCells
function segmentedCells = ipexbatchDetectCells(I)
%ipexbatchDetectCells Algorithm to detect cells in image.
%   segmentedCells = ipexbatchDetectCells(I) detects cells in the cell
%   image I and returns the result in segmentedCells.
%
%   Supports batch processing demo, ipexbatch.

%   Copyright 2005-2007 The MathWorks, Inc.

% Use |edge| and the Sobel operator to calculate the threshold
% value. Tune the threshold value and use |edge| again to obtain a
% binary mask that contains the segmented cell.
[junk threshold] = edge(I, 'sobel');
fudgeFactor = .5;
BW = edge(I,'sobel', threshold * fudgeFactor);

se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWdilate = imdilate(BW, [se90 se0]);
BWnobord = imclearborder(BWdilate, 4);
BWopen = bwareaopen(BWnobord,200);
BWclose = bwmorph(BWopen,'close');
BWfill = imfill(BWclose, 'holes');
BWoutline = bwperim(BWfill);
segmentedCells = I;
segmentedCells(BWoutline) = 255;

Loop over Images

The function ipexbatchProcessFiles uses parfor to process each image independently. This works like a regular for loop but can take advantage of multiple processors if you have the Parallel Computing Toolbox software as described below.

To view ipexbatchProcessFiles use this command.

type ipexbatchProcessFiles
function segmentedCellSequence = ipexbatchProcessFiles(fileNames,fcn)
%ipexbatchProcessFiles Process image files.
%   SEQUENCE = ipexbatchProcessFiles(FILENAMES,FCN) loops over all the files
%   listed in FILENAMES, calls the function FCN on each of them, and combine
s
%   the restults in SEQUENCE. FCN is a function handle for a function with
%   signature: B = FCN(A).
%
%   Supports batch processing demo, ipexbatch.

%   Copyright 2007 The MathWorks, Inc.

I = imread(fileNames{1});

[mrows,ncols] = size(I);
nImages = length(fileNames);

segmentedCellSequence = zeros(mrows,ncols,nImages,class(I));

parfor (k = 1:nImages)

    I = imread(fileNames{k});
    segmentedCellSequence(:,:,k) = fcn(I);

end

If you have the Parallel Computing Toolbox software, you can use up to 4 local workers to distribute each cycle through the loop to a different worker. If you also have the MATLAB® Distributed Computing Server™, you can run your batch processing job on a cluster.

You can allocate workers with this command.

matlabpool open 4

Now call ipexbatchProcessFiles to apply the cell detection algorithm to each file.

segmentedCellSequence = ipexbatchProcessFiles(fileNames,@ipexbatchDetectCell
s);

Clean Up

If you called matlabpool to start some workers, be sure to close them.

matlabpool close

Display Results

Run the following command to see the sequence in implay.

implay(segmentedCellSequence)

You can also watch an animation of the sequence in your system's browser by clicking on the link below.

Watch the cell video.

If you have data that does not all fit in memory, instead of returning the segmented image data in one large array, you can modify the body of the parfor loop in ipexbatchProcessFiles.m to either write each segmented image to a new file or write the whole segmented sequence to a multimedia file one image at a time. Then, you could review the results separate from the processing.

Contact sales
Free technical kit
Trial software
E-mail this page

Get Pricing and
Licensing Options