Image Processing Toolbox    

Exercise 2 -- Advanced Topics

In this exercise you will work with another intensity image, rice.tif and explore some more advanced operations. The goals of this exercise are to remove the nonuniform background from rice.tif, convert the resulting image to a binary image by using thresholding, use components labeling to return the number of objects (grains or partial grains) in the image, and compute feature statistics.

1. 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.

2. Perform Block Processing to Approximate the Background

Notice that the background illumination is brighter in the center of the image than at the bottom. Use the blkproc function to find a coarse estimate of the background illumination by finding the minimum pixel value of each 32-by-32 block in the image.

To see what was returned to backApprox, type

MATLAB responds with

3. Display the Background Approximation As a Surface

Use the surf command to create a surface display of the background approximation, backApprox. surf requires data of class double, however, so you first need to convert backApprox using the double command. You also need to divide the converted data by 255 to bring the pixel values into the proper range for an image of class double, [0 1].

To see the other side of the surface, reverse the y-axis with this command,

To rotate the surface in any direction, click on the rotate button in the toolbar (shown at left), then click and drag the surface to the desired view.

Here's What Just Happened
Step 3. You used the surf command to examine the background image. The surf command creates colored parametric surfaces that enable you to view mathematical functions over a rectangular region. In the first surface display, [0, 0] represents the origin, or upper-left corner of the image. The highest part of the curve indicates that the highest pixel values of backApprox (and consequently rice.tif) occur near the middle rows of the image. The lowest pixel values occur at the bottom of the image and are represented in the surface plot by the lowest part of the curve. Because the minimum intensity values in each block of this image make a smooth transition across the image, the surface is comprised of fairly smooth curves.
The surface plot is a Handle Graphics® object, and you can therefore fine-tune its appearance by setting properties. ("Handle Graphics" is the name for the collection of low-level graphics commands that create the objects you generate using MATLAB.) The call to reverse the y-axis is one of many property settings that you can make. It was made using the set command, which is used to set all properties. In the line,
    set(gca,'ydir','reverse');
    
gca refers to the handle of the current axes object and stands for "get current axes." You can also set many properties through the Property Editor. To invoke the Property Editor, open the figure window's Edit menu, and select Figure Properties, Axes Properties, or Current Object Properties. To select an object to modify with the Property Editor, click the property the following button on the figure window, , then click on the object. You can also use the other buttons in the toolbar to add new text or line objects to your figure.
For information on working with MATLAB graphics, see the MATLAB graphics documentation.

4. Resize the Background Approximation

Our estimate of the background illumination is only 8-by-8. Expand the background to the same size as the original background image (256-by-256) by using the imresize function, then display it.

5. Subtract the Background Image from the Original Image

Now subtract the background image from the original image to create a more uniform background. First, change the storage class of I to double, because subtraction can only be performed on double arrays.

Now subtract backApprox256 from I and store it in a new array, I2.

Subtracting backApprox256 from I may yield some out-of-range values in the image. To correct the dynamic range of pixel values, use the max and min functions to clip pixel values outside the range [0,1].

Now display the image with its more uniform background.

6. Adjust the Image Contrast

The image is now a bit too dark. Use imadjust to adjust the contrast.

Display the newly adjusted image.

7. Apply Thresholding to the Image

Create a new binary thresholded image, bw, by comparing each pixel in I3 to a threshold value of 0.2.

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

MATLAB responds with

8. Use Connected Components Labeling to Determine the Number of Objects in the Image

Use the bwlabel function to label all of the connected components in the binary image bw.

Show the number of objects found by bwlabel.

MATLAB responds with

You have just calculated how many objects (grains or partial grains of rice) are in rice.tif.

To add some color to the figure, display labeled using a vibrant colormap created by the hot function.

9. Examine an Object

You may find it helpful to take a closer look at labeled to see what bwlabel has done to it. 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.

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

10. Compute Feature Measurements of Objects in the Image

The imfeature command computes feature measurements for objects 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 imfeature to create a structure array containing some basic types of feature information for labeled.

MATLAB responds with

Find the area of the grain labeled with 51's, or "grain 51." To do this, use dot notation to access the data in the Area field. Note that structure field names are case sensitive, so you need to capitalize the name as shown.

returns the following results

Find the smallest possible bounding box and the centroid (center of mass) for grain 51.

returns

Create a new vector, allgrains, which holds just the area measurement for each grain. Then call the whos command to see how allgrains is allocated in the MATLAB workspace.

MATLAB responds with

allgrains contains 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.

returns

which is the same result that you received when using dot notation to access the Area field of grains(51).

Here's What Just Happened

Step 10. You called imfeature to return a structure of basic feature measurements for each thresholded grain of rice. imfeature supports many types of feature measurement, but setting the measurements parameter to basic is a convenient way to return three of the most commonly used measurements: the area, the centroid (or center of mass), and the bounding box. The bounding box represents the smallest rectangle that can contain a region, or in this case, a grain. The four-element vector returned by the BoundingBox field,

    [141.5000   89.5000   26.0000   27.0000]
    

shows that the upper left corner of the bounding box is positioned at [141.5 89.5], and the box has a width of 26.0 and a height of 27.0. (The position is defined in spatial coordinates, hence the decimal values. For more information on the spatial coordinate system, see Spatial Coordinates.) For more information about working with MATLAB structure arrays, see Structures in the MATLAB graphics documentation.

You used dot notation to access the Area field of all of the elements of grain and stored this data to a new vector allgrains. This step simplifies analysis made on area measurements because you do not have to use field names to access the area.

11. 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).

returns

Use the find command to return the component label of this large-sized grain.

returns

Find the mean grain size.

returns

Make a histogram containing 20 bins that show the distribution of rice grain sizes.


 Exercise 1 -- Some Basic Topics Where to Go From Here