You are on page 1of 15

What Is the Image Processing Toolbox?

The Image Processing Toolbox is a collection of functions that extend the capability of the MATLAB numeric computing environment. The toolbox supports a wide range of image processing operations, including:

Spatial image transformations Morphological operations Neighborhood and block operations Linear filtering and filter design Transforms Image analysis and enhancement Image registration Deblurring Region of interest operations

Many of the toolbox functions are MATLAB M-files, a series of MATLAB statements that implement specialized image processing algorithms. You can view the MATLAB code for these functions using the statement
type function_name

You can extend the capabilities of the Image Processing Toolbox by writing your own M-files, or by using the toolbox in combination with other toolboxes, such as the Signal Processing Toolbox and the Wavelet Toolbox.

Read and Display an Image


Clear the MATLAB workspace of any variables and close open figure windows.
clear, close all

To read an image, use the imread command. Let's read in a TIFF image named pout.tif (which is one of the sample images that is supplied with the Image Processing Toolbox), and store it in an array named I.
I=imread('pout.tif');

Now call imshow to display I.


imshow(I)

Check the Image in Memory


Enter the whos command to see how I is stored in memory.
whos

MATLAB responds with


Name I Size 291x240 Bytes 69840 Class uint8 array

Grand total is 69840 elements using 69840 bytes

Perform Histogram Equalization


As you can see, pout.tif is a somewhat low contrast image. To see the distribution of intensities in pout.tif in its current state, you can create a histogram by calling the imhist function. (Precede the

call to imhist with the figure command so that the histogram does not overwrite the display of the image I in the current figure window.)
figure, imhist(I) % Display a histogram of I in a new figure.

Notice how the intensity range is rather narrow. It does not cover the potential range of [0, 255], and is missing the high and low values that would result in good contrast. Now call histeq to spread the intensity values over the full range, thereby improving the contrast of I. Return the modified image in the variable I2.
I2 = histeq(I); % Equalize I and output in new array I2.

Display the new equalized image, I2, in a new figure window.


figure, imshow(I2) % Display the new equalized image I2.

Call imhist again, this time for I2.


figure, imhist(I2) % Show the histogram for the new image I2.

See how the pixel values now extend across the full range of possible values.

Write the Image


Write the newly adjusted image I2 back to disk. Let's say you'd like to save it as a PNG file. Use imwrite and specify a filename that includes the extension 'png'.
imwrite (I2, 'pout2.png');

Check the Contents of the Newly Written File


Now, use the imfinfo function to see what was written to disk. Be sure not to end the line with a semicolon so that MATLAB displays the results. Also, be sure to use the same path (if any) as you did for the call to imwrite, above.
imfinfo('pout2.png')

MATLAB responds with


Filename: FileModDate: FileSize: Format: 'pout2.png' '03-Jun-1999 15:50:25' 36938 'png' ans =

FormatVersion: [] Width: Height: BitDepth: ColorType: 240 291 8 'grayscale'

Read and Display an Image


Clear the MATLAB workspace of any variables and close open figure windows. Read and display the intensity image rice.tif.
clear, close all

I = imread('rice.tif'); imshow(I)

Use Morphological Opening to Estimate the Background


Notice that the background illumination is brighter in the center of the image than at the bottom. Use the imopen function to estimate the background illumination.
background = imopen(I,strel('disk',15));

To see the estimated background image, type


imshow(background)

Display the Background Approximation as a Surface


Use the surf command to create a surface display of the background approximation, background. The surf function requires data of class double, however, so you first need to convert background using the double command.
figure, surf(double(background(1:8:end,1:8:end))),zlim([0 255]); set(gca,'ydir','reverse');

The example uses MATLAB indexing syntax to view only 1 out of 8 pixels in each direction; otherwise the surface plot would be too dense. The example also sets the scale of the plot to better match the range of the uint8 data and reverses the y-axis of the display to provide a better view of the data (the pixels at the bottom of the image appear at the front of the surface plot).

Subtract the Background Image from the Original Image


Now subtract the background image, background, from the original image, I, to create a more uniform background.
I2 = imsubtract(I,background);

Now display the image with its more uniform background.


figure, imshow(I2)

Adjust the Image Contrast


The image is now a bit too dark. Use imadjust to adjust the contrast.
I3 = imadjust(I2, stretchlim(I2), [0 1]);

Display the newly adjusted image.


figure, imshow(I3);

Apply Thresholding to the Image


Create a new binary thresholded image, bw, by using the functions graythresh and im2bw.
level = graythresh(I3); bw = im2bw(I3,level); figure, imshow(bw)

Now call the whos command to see what type of array the thresholded image bw is.
whos

MATLAB responds with


Name I I2 I3 background bw level Size 256x256 256x256 256x256 256x256 256x256 1x1 Bytes 65536 65536 65536 65536 65536 8 Class uint8 array uint8 array uint8 array uint8 array logical array double array

Grand total is 327681 elements using 327688 bytes

Determine the Number of Objects in the Image


To determine the number of grains of rice in the image, use the bwlabel function. This function labels all of the connected components in the binary image bw and returns the number of objects it finds in the image in the output value, numobjects.
[labeled,numObjects] = bwlabel(bw,4);% Label components. numObjects = 80

The accuracy of your results depends on a number of factors, including:


The size of the objects The accuracy of your approximated background Whether you set the connectivity parameter to 4 or 8 Whether or not any objects are touching (in which case they may be labeled as one object) In the example, some grains of rice are touching, so bwlabel treats them as one object.

Examine the Label Matrix


You may find it helpful to take a closer look at labeled to see what bwlabel has created. Use the imcrop command to select and display pixels in a region of labeled that includes an object and some background. To ensure that the output is displayed in the MATLAB window, do not end the line with a semicolon. In addition, choose a small rectangle for this exercise, so that the displayed pixel values don't wrap in the MATLAB command window. The syntax shown below makes imcrop work interactively. Your mouse cursor becomes a cross-hair when placed over the image. Click at a position in labeled where you would like to select the upper left corner of a region. Drag the mouse to create the selection rectangle, and release the button when you are done.
grain = imcrop(labeled) % Crop a portion of labeled.

We chose the left edge of a grain and got the following results.
grain = 0 0 0 0 0 0 0 60 60

0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0

0 60 60 60 60 60 0 0 0

0 60 60 60 60 60 0 0 0

60 60 60 60 60 60 60 0 0

60 60 60 60 60 60 60 0 0

60 60 60 60 60 60 60 0 0

60 60 60 60 60 60 60 0 0

A good way to view a label matrix is to display it as a pseudo-color indexed image. In the pseudo-color image, the number that identifies each object in the label matrix maps to a different color in the associated colormap matrix. When you view a label matrix as an RGB image, the objects in the image are easier to distinguish. To view a label matrix in this way, use the label2rgb function. Using this function, you can specify the colormap, the background color, and how objects in the label matrix map to colors in the colormap.
RGB_label = label2rgb(labeled, @spring, 'c', 'shuffle'); imshow(RGB_label);

9. Measure Object Properties in the Image


The regionprops command measures object or region properties in an image and returns them in a structure array. When applied to an image with labeled components, it creates one structure element for each component. Use regionprops to create a structure array containing some basic properties for labeled.
graindata = regionprops(labeled,'basic')

MATLAB responds with


graindata = 80x1 struct array with fields: Area Centroid BoundingBox

To find the area of the component labeled with 51's, use dot notation to access the Area field in the 51st element in the graindata structure array. Note that structure field names are case sensitive, so you need to capitalize the name as shown.
graindata(51).Area

returns the following results


ans = 296

To find the smallest possible bounding box and the centroid (center of mass) for the same component, use this code:
graindata(51).BoundingBox, graindata(51).Centroid ans = 142.5000 ans = 155.3953 89.5000 24.0000 26.0000

102.1791

To create a new vector, allgrains, which holds just the area measurement for each grain, use this code:
allgrains = [graindata.Area]; whos allgrains

Call the whos command to see how MATLAB allocated the allgrains variable.
Name Size Bytes Class

allgrains

1x80

640

double array

Grand total is 80 elements using 640 bytes

is a one-row array of 80 elements, where each element contains the area measurement of a grain. Check the area of the 51st element of allgrains.
allgrains allgrains(51)

returns
ans = 296

Compute Statistical Properties of Objects in the Image


Now use MATLAB functions to calculate some statistical properties of the thresholded objects. First use max to find the size of the largest grain. (If you have followed all of the steps in this exercise, the "largest grain" is actually two grains that are touching and have been labeled as one object).
max(allgrains)

returns
ans = 695

Use the find command to return the component label of this large-sized grain.
biggrain = find(allgrains==695)

returns
biggrain =

68

Find the mean grain size.


mean(allgrains)

returns
ans = 249

Make a histogram containing 20 bins that show the distribution of rice grain sizes.
hist(allgrains,20)

References:
www.mathworks.com/products/image/ www.mathtools.net/MATLAB/Image_Processing/ www.amath.colorado.edu/courses/4720/2000Spr/Labs/Worksheets/Matlab_tutorial/matlabimpr.html www.imageprocessingplace.com/DIPUM/dipum_book_description/book_description.htm