Image Processing Toolbox | ![]() ![]() |
Example 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 object 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. 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.
To see the estimated background image, type
Here's What Just Happened |
Step 1. You used the toolbox functions imread and imshow to read and display an 8-bit intensity image. imread and imshow are discussed in Exercise 1, in 2. Check the Image in Memory, under the "Here's What Just Happened" discussion.Step 2. You performed a morphological opening operation by calling imopen with the input image, I , and a disk-shaped structuring element with a radius of 15. The structuring element was created by the strel function. The morphological opening has the effect of removing objects that cannot completely contain a disk of radius 15. For more information about morphological opening, see Dilation- and Erosion-Based Functions. |
3. 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.
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).
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 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 background (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. The surface plot is a Handle Graphics® object, and you can therefore fine-tune its appearance by setting properties. For information on working with MATLAB graphics, see the MATLAB graphics documentation. |
4. 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.
Now display the image with its more uniform background.
Here's What Just Happened |
Step 4. You subtracted a background approximation image from The Image Processing Toolbox has a demo, |
5. Adjust the Image Contrast
The image is now a bit too dark. Use imadjust
to adjust the contrast.
Display the newly adjusted image.
Here's What Just Happened |
Step 5. You used the You called |
6. Apply Thresholding to the Image
Create a new binary thresholded image, bw
, by using the functions graythresh
and im2bw
.
Now call the whos
command to see what type of array the thresholded image bw
is.
Name Size Bytes Class I 256x256 65536 uint8 array I2 256x256 65536 uint8 array I3 256x256 65536 uint8 array background 256x256 65536 uint8 array bw 256x256 65536 logical array level 1x1 8 double array Grand total is 327681 elements using 327688 bytes
Here's What Just Happened |
Step 6. You called Notice that when you call the |
7. 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
.
The accuracy of your results depends on a number of factors, including:
bwlabel
treats them as one object.
Here's What Just Happened |
Step 7. You called bwlabel to search for connected components and label them with unique numbers. bwlabel takes a binary input image and a value specifying the connectivity of objects. The parameter 4 , passed to the bwlabel function, means that pixels must touch along an edge to be considered connected. For more information about the connectivity of objects, see Pixel Connectivity. You can also determine the number of objects in a label matrix by asking for the maximum pixel value in the image. For example, max(labeled(:)) ans = 80 |
8. 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.
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 60 60 60 60 0 0 0 60 60 60 60 60 60 0 0 0 60 60 60 60 60 60 0 0 0 60 60 60 60 60 60 0 0 0 60 60 60 60 60 60 0 0 0 60 60 60 60 60 60 0 0 0 0 0 60 60 60 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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.
Here's What Just Happened |
Step 8. You called The You are not restricted to rectangular regions of interest. The toolbox also has a The call to |
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
.
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.
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 89.5000 24.0000 26.0000 ans = 155.3953 102.1791
To create a new vector, allgrains
, which holds just the area measurement for each grain, use this code:
Call the whos
command to see how MATLAB allocated the allgrains
variable.
allgrains
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
.
which is the same result that you received when using dot notation to access the Area
field of graindata(51)
.
Here's What Just Happened |
Step 9. You called shows that the upper left corner of the bounding box is positioned at You used dot notation to access the |
10. 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).
Use the find
command to return the component label of this large-sized grain.
Make a histogram containing 20 bins that show the distribution of rice grain sizes.
hist(allgrains,20)
Here's What Just Happened |
Step 10. You used some of the MATLAB statistical functions, The Image Processing Toolbox also has some statistical functions, such as The histogram shows that the most common sizes for rice grains in this image are in the range of 300 to 400 pixels. |
![]() | Example 1 -- Some Basic Topics | Where to Go from Here | ![]() |