Lab 1 - Basics of Image Processing
Lab 1 - Basics of Image Processing
Lab 1 - Basics of Image Processing
ECE 619: Computer Vision Lab 1: Basics of Image Processing (Using Matlab image processing toolbox Issued Thursday 1/10 Due 1/24) Task 1: Execute the steps outlined below to get familiar with basics of image processing using Matlab. Task 2: Use your own image(s); submit a short report on the following tasks: 1) Image enhancement by histogram equalization; 2) Edge enhancement by the Cross window operator; 3) Edge detection by Canny operator; 4) Removal of white Gaussian noise using Gaussian and Median filters for different SNR. Help on this assignment was provided by Shireen Elhabian.
Main Topics
Example 1 Some Basic Concepts .......................................................................................... 1 Introduction............................................................................................................................. 1 Step 1: Read and Display an Image ........................................................................................ 2 Step 2: Check How the Image Appears in the Workspace ..................................................... 2 Step 3: Improve Image Contrast ............................................................................................. 3 Step 4: Write the Image to a Disk File.................................................................................... 4 Example 2 Advanced Topics ................................................................................................. 4 Introduction............................................................................................................................. 4 Step 1: Read and Display an Image ........................................................................................ 4 Step 2: Estimate the Value of Background Pixels .................................................................. 5 Step 3: View the Background Approximation as a Surface ................................................... 5 Step 4: Create an Image with a Uniform Background............................................................ 6 Step 5: Adjust the Contrast in the Processed Image ............................................................... 6 Step 6: Create a Binary Version of the Image ........................................................................ 7 Step 7: Determine the Number of Objects in the Image......................................................... 8 Step 8: Examine the Label Matrix .......................................................................................... 8 Step 9: Display the Label Matrix as a Pseudocolor Indexed Image ....................................... 9 Step 10: Measure Object Properties in the Image................................................................... 9 Step 11: Compute Statistical Properties of Objects in the Image ......................................... 10
Page 1 of 10
First, clear the MATLAB workspace of any variables and close open figure windows.
close all
To read an image, use the imread command. The example reads one of the sample images included with Image Processing Toolbox, pout.tif, and stores it in an array named I.
I = imread('pout.tif');
Now display the image. The toolbox includes two image display functions: imshow and imtool. imshow is the toolbox's fundamental image display function. imtool starts the Image Tool which presents an integrated environment for displaying images and performing some common image processing tasks. The Image Tool provides all the image display capabilities of imshow but also provides access to several other tools for navigating and exploring images, such as scroll bars, the Pixel Region tool, Image Information tool, and the Contrast Adjustment tool. This example uses imshow.
imshow(I)
Grayscale Image pout.tif
Step 2: Check How the Image Appears in the Workspace To see how the imread function stores the image data in the workspace, check the Workspace browser in the MATLAB desktop. The Workspace browser displays information about all the variables you create during a MATLAB session. The imread function returned the image data in the variable I, which is a 291-by-240 element array of uint8 data. MATLAB can store images as uint8, uint16, or double arrays. You can also get information about variables in the workspace by calling the whos command.
whos
MATLAB responds with
Name
Size
Bytes
Class
Attributes
291x240
69840
uint8
Page 2 of 10
pout.tif is a somewhat low contrast image. To see the distribution of intensities in pout.tif, 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)
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. The toolbox provides several ways to improve the contrast in an image. One way is to call the histeq function to spread the intensity values over the full range of the image, a process called histogram equalization.
I2 = histeq(I);
Display the new equalized image, I2, in a new figure window.
figure, imshow(I2)
Equalized Version of pout.tif
Page 3 of 10
Call imhist again to create a histogram of the equalized image I2. If you compare the two histograms, the histogram of I2 is more spread out than the histogram of I1.
figure, imhist(I2)
The toolbox includes several other functions that perform contrast adjustment, including the imadjust and adapthisteq functions. In addition, the toolbox includes an interactive tool, called the Adjust Contrast tool, that you can use to adjust the contrast and brightness of an image displayed in the Image Tool. To use this tool, call the imcontrast function or access the tool from the Image Tool. Step 4: Write the Image to a Disk File To write the newly adjusted image I2 to a disk file, use the imwrite function. If you include the filename extension '.png', the imwrite function writes the image to a file in Portable Network Graphics (PNG) format, but you can specify other formats.
close all
Read and display the grayscale image rice.png.
I = imread('rice.png'); imshow(I)
Page 4 of 10
Step 2: Estimate the Value of Background Pixels In the sample image, the background illumination is brighter in the center of the image than at the bottom. In this step, the example uses a morphological opening operation to estimate the background illumination. Morphological opening is an erosion followed by a dilation, using the same structuring element for both operations. The opening operation has the effect of removing objects that cannot completely contain the structuring element. The example calls the imopen function to perform the morphological opening operation and then calls the imshow function to view the results. Note how the example calls the strel function to create a disk-shaped structuring element with a radius of 15. To remove the rice grains from the image, the structuring element must be sized so that it cannot fit entirely inside a single grain of rice.
Page 5 of 10
Step 4: Create an Image with a Uniform Background To create a more uniform background, subtract the background image, background, from the original image, I, and then view the image.
Step 5: Adjust the Contrast in the Processed Image After subtraction, the image has a uniform background but is now a bit too dark. Use imadjust to adjust the contrast of the image. imadjust increases the contrast of the image by saturating 1% of the data at both low and high intensities of I2 and by stretching the intensity values to fill the uint8 dynamic range. The following example adjusts the contrast in the image created in the previous step and displays it.
I3 = imadjust(I2);
Page 6 of 10
Step 6: Create a Binary Version of the Image Create a binary version of the image so that you can use toolbox functions to count the number of rice grains. Use the im2bw function to convert the grayscale image into a binary image by using thresholding. The function graythresh automatically computes an appropriate threshold to use to convert the grayscale image to binary.
Page 7 of 10
After converting the image to a binary image, you can use the bwlabel function to determine the number of grains of rice in the image. The bwlabel function labels all the components in the binary image bw and returns the number of components it finds in the image in the output value, numObjects.
[labeled,numObjects] = bwlabel(bw,4);
The accuracy of the results depends on a number of factors, including
The size of the objects Whether or not any objects are touching (in which case they might be labeled as one object) The accuracy of the approximated background The connectivity selected. The parameter 4, passed to the bwlabel function, means that pixels must touch along an edge to be considered connected.
Step 8: Examine the Label Matrix To better understand the label matrix returned by the bwlabel function, this step explores the pixel values in the image. There are several ways to get a close-up view of pixel values. For example, you can use imcrop to select a small portion of the image. Another way is to use the Pixel Region tool to examine pixel values. The following example displays the label matrix, using imshow, and then starts a Pixel Region tool associated with the displayed image.
Page 8 of 10
A good way to view a label matrix is to display it as a pseudocolor indexed image. In the pseudocolor image, the number that identifies each object in the label matrix maps to a different color in the associated colormap matrix. The colors in the image make objects 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.
Step 10: 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. The following example uses regionprops to create a structure array containing some basic properties for labeled. When you set the properties parameter to 'basic', the regionprops function returns three commonly used measurements, area, centroid, and bounding box, for all the objects in the label matrix.. The bounding box represents the smallest rectangle that can contain a component, or in this case, a grain of rice.
graindata = regionprops(labeled,'basic')
MATLAB responds with
graindata =
area51 = graindata(51).Area
Page 9 of 10
area51 =
140
Step 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. (In this example, the largest grain is actually two grains of rice that are touching.)
maxArea = max([graindata.Area])
returns
maxArea =
404
Use the find command to return the component label of the grain of rice with this area.
biggestGrain = find([graindata.Area]==maxArea)
returns
biggestGrain =
59
Find the mean of all the rice grain sizes.
meanArea = mean([graindata.Area])
returns
meanArea =
175.0396
Make a histogram containing 20 bins that show the distribution of rice grain sizes. The histogram shows that the most common sizes for rice grains in this image are in the range of 150 to 250 pixels.
hist([graindata.Area],20)
Page 10 of 10