This example shows how to use the Image Batch Processor app to process a group of images in the same folder. The batch processing operation typically follows these steps.
Open Image Batch Processor App — Select the app icon from the apps gallery, or open it from the command line.
Load Images into the Image Batch Processor App — Specify the name of a folder containing image files. The app can retrieve images from subfolders as well, retaining the hierarchical folder structure.
Specify the Batch Processing Function — Provide the app with the function you want applied to all the images in the folder. You can specify the name of an existing function or create a new function using a batch processing function template.
Perform the Operation on the Images — Process the group of images. You can process all files in the folder or select files to process. The app displays the results in the Results tab.
Obtain the Results of the Batch Processing Operation — You can save the results to a variable in the workspace or, for image results, files. You can also get the MATLAB® code that the app used to generate the results.
This part of the example shows how to open the Image Batch Processor app.
From the MATLAB Toolstrip, on the Apps tab, in the Image Processing and
Computer Vision group, click Image Batch Processor
. You can also open the app at the command
line using the
imageBatchProcessor command. (If you have
Computing Toolbox™, the app includes the Use Parallel
This part of the example shows how to load images into the Image Batch Processor app.
For this example, create a new folder in an area where you have write
permission, and load a set of 10 images from the Image Processing
Click Load Images and specify the folder containing
the images you want to process in the Load Images from
Folder dialog box. For this example, specify the folder that
you created in the first step,
cellprocessing, and then
click Load. By default, the app includes images in
subfolders. To change this behavior, clear the Include images in
subfolders check box.
The Image Batch Processor app creates thumbnails of the images in the folder and displays them in a scrollable pane. The app displays the first selected image in larger resolution in the Input Image pane.
This part of the example shows how to specify the function you want to execute on all the images.
Specify the name of the function you want to use to process the images in the folder. To specify an existing function, type its name in the Function name field, or click the folder icon to browse and select the function. To create a new batch processing function, click New. The app opens the batch processing function template in the MATLAB Editor. This example creates a new function-click New.
Edit the batch processing function template opened in the MATLAB Editor. Paste your code into the space reserved in the
template file and click Save. The example uses the
default function name,
myimfcn, but you can give the
function another name. The example code creates a mask image, calculates the
total number of cells in the image, and creates a thresholded version of the
function results = myimfcn(im) %Image Processing Function % % IM - Input image. % RESULTS - A scalar structure with the processing results. % %-------------------------------------------------------------------------- % Auto-generated by imageBatchProcessor App. % % When used by the App, this function will be called for every input image % file automatically. IM contains the input image as a matrix. RESULTS is a % scalar structure containing the results of this processing function. % %-------------------------------------------------------------------------- imstd = stdfilt(im,ones(27)); bw = imstd>30; thresholdMask = imfuse(im, bw); [~, n] = bwlabel(bw); results.bw = bw; results.thresholdMask = thresholdMask; results.numCells = n;
When you save the file, the app displays the name of your new function in the Function name field.
This part of the example shows how to process all the images in a folder (or a subset of the images) in a single operation .
Run the batch processor on one of your images as a test of your function. It can be useful to try out your batch processing function on one of the images and check the results before running it on all the images. When you are satisfied that the function is working properly, run the function on all the images in the folder. With one image selected, click Process Selected to process the selected image.
Examine the results of the test run. The app displays the results of the processing in a new tab called Results. For this example, the app displays the binary mask, a count of the number of objects (cells) in the image, and a thresholded version of the image. To get a closer view of the image results, click Show. The app open a larger resolution version of the image in another tab. You can use the zoom and pan options to examine the image. When zooming and panning, the app links the result image to the original image—moving or zooming on one causes the other image to move.
If the results of the test run are successful, execute the function on all the images in your input folder. Click the additional options menu on the Process Selected button, and select Process All. You can also select the multiple images to process using either Ctrl-Click or Shift-Click. If you have Parallel Computing Toolbox, you can click Use Parallel to process the images on a local parallel pool.
The app processes all the images in the specified folder, filling in the square next to the thumbnail names with green. The Results tab contains the results of the selected image.
This part of the example shows how to obtain the results of the batch processing operation
Click Export to view the options available. You can export results to the workspace or to a file, or you can get the MATLAB code the app used to generate the results.
Choose theExport result of all processed images to
workspace option to save the results in a workspace variable.
Select the results you want to save, and click OK. A
common approach is to save the nonimage results to the workspace and save
the images that result from the processing in files. The example saves the
cell count along with the name of the input file to the workspace variable
By default, the app returns the results you choose in a table named
allresults. You can specify another name and you can
also choose to store the results in a structure instead. If you select
Include input image file name, the app includes the
name of the image associated with the results.
numCells fileName ________ _____________________________________________ 7 'C:\cellprocessing\cellimages\AT3_1m4_10.tif' 7 'C:\cellprocessing\cellimages\AT3_1m4_01.tif' 7 'C:\cellprocessing\cellimages\AT3_1m4_02.tif' 7 'C:\cellprocessing\cellimages\AT3_1m4_03.tif' 7 'C:\cellprocessing\cellimages\AT3_1m4_04.tif' 7 'C:\cellprocessing\cellimages\AT3_1m4_05.tif' 6 'C:\cellprocessing\cellimages\AT3_1m4_06.tif' 7 'C:\cellprocessing\cellimages\AT3_1m4_07.tif' 9 'C:\cellprocessing\cellimages\AT3_1m4_08.tif' 7 'C:\cellprocessing\cellimages\AT3_1m4_09.tif'
Choose the Export result of all processed images to files option to save the images produced by the batch processing. Select the file format for each returned file, and click OK. By default, the app stores the files in the same folder that you specified when you loaded your images, but you can specify another folder.
Here is what the folder looks like after saving results in files.
06/19/2005 10:06 PM 310,202 AT3_1m4_01.tif 12/12/2015 11:22 AM 3,822 AT3_1m4_01_bw.tif 12/12/2015 11:22 AM 929,544 AT3_1m4_01_thresholdMask.tif 06/19/2005 10:06 PM 310,434 AT3_1m4_02.tif 12/12/2015 11:22 AM 3,840 AT3_1m4_02_bw.tif 12/12/2015 11:22 AM 929,550 AT3_1m4_02_thresholdMask.tif 06/19/2005 10:06 PM 310,314 AT3_1m4_03.tif 12/12/2015 11:22 AM 3,756 AT3_1m4_03_bw.tif 12/12/2015 11:22 AM 929,508 AT3_1m4_03_thresholdMask.tif 06/19/2005 10:06 PM 310,336 AT3_1m4_04.tif 12/12/2015 11:22 AM 3,806 AT3_1m4_04_bw.tif 12/12/2015 11:22 AM 929,512 AT3_1m4_04_thresholdMask.tif 06/19/2005 10:06 PM 310,378 AT3_1m4_05.tif 12/12/2015 11:22 AM 3,744 AT3_1m4_05_bw.tif 12/12/2015 11:22 AM 929,518 AT3_1m4_05_thresholdMask.tif 06/19/2005 10:06 PM 310,342 AT3_1m4_06.tif 12/12/2015 11:22 AM 3,730 AT3_1m4_06_bw.tif 12/12/2015 11:22 AM 929,538 AT3_1m4_06_thresholdMask.tif 06/19/2005 10:06 PM 310,352 AT3_1m4_07.tif 12/12/2015 11:22 AM 3,756 AT3_1m4_07_bw.tif 12/12/2015 11:22 AM 929,532 AT3_1m4_07_thresholdMask.tif 06/19/2005 10:06 PM 310,364 AT3_1m4_08.tif 12/12/2015 11:22 AM 3,746 AT3_1m4_08_bw.tif 12/12/2015 11:22 AM 929,482 AT3_1m4_08_thresholdMask.tif 06/19/2005 10:06 PM 310,266 AT3_1m4_09.tif 12/12/2015 11:22 AM 3,732 AT3_1m4_09_bw.tif 12/12/2015 11:22 AM 929,500 AT3_1m4_09_thresholdMask.tif 06/19/2005 10:06 PM 310,288 AT3_1m4_10.tif 12/12/2015 11:22 AM 3,718 AT3_1m4_10_bw.tif 12/12/2015 11:22 AM 929,504 AT3_1m4_10_thresholdMask.tif
Choose the Generate function option to get the MATLAB code the app used to process your files. The app generates a function that accepts the input folder name and the output folder name as input arguments. By default, the function returns a table with the results, but you can choose a structure instead. For image results, you can specify the file format and whether you want the function to write the image to the specified output folder. To get the code, click OK.