Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 1 - Basics of Image Processing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

University of Louisville Electrical and Computer Engineering

Dr. Aly A. Farag Spring 2008

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

Example 1 Some Basic Concepts


Introduction This example introduces some basic image processing concepts. The example starts by reading an image into the MATLAB workspace. The example then performs some contrast adjustment on the image. Finally, the example writes the adjusted image to a file.

Page 1 of 10

University of Louisville Electrical and Computer Engineering


Step 1: Read and Display an Image

Dr. Aly A. Farag Spring 2008

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

University of Louisville Electrical and Computer Engineering


Step 3: Improve Image Contrast

Dr. Aly A. Farag Spring 2008

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

University of Louisville Electrical and Computer Engineering

Dr. Aly A. Farag Spring 2008

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.

imwrite (I2, 'pout2.png');

Example 2 Advanced Topics


Introduction This example introduces some advanced image processing concepts, such as calculating statistics about objects in the image. The example performs some preprocessing of the image, such as evening out the background illumination and converting the image into a binary image, that help achieve better results in the statistics calculation. Step 1: Read and Display an Image First, clear the MATLAB workspace of any variables, close open figure windows, and close all open Image Tools.

close all
Read and display the grayscale image rice.png.

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

Page 4 of 10

University of Louisville Electrical and Computer Engineering


Grayscale Image rice.png

Dr. Aly A. Farag Spring 2008

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.

background = imopen(I,strel('disk',15)); figure, imshow(background)


Step 3: View the Background Approximation as a Surface Use the surf command to create a surface display of the background approximation background. The surf command creates colored parametric surfaces that enable you to view mathematical functions over a rectangular region. 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). 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.png) 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.

Page 5 of 10

University of Louisville Electrical and Computer Engineering

Dr. Aly A. Farag Spring 2008

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.

I2 = imsubtract(I,background); figure, imshow(I2)


Image with Uniform Background

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

University of Louisville Electrical and Computer Engineering


figure, imshow(I3);
Image After Intensity Adjustment

Dr. Aly A. Farag Spring 2008

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.

level = graythresh(I3); bw = im2bw(I3,level); figure, imshow(bw)


Binary Version of the Image

The binary image bw returned by im2bw is of class logical.

Page 7 of 10

University of Louisville Electrical and Computer Engineering


Step 7: Determine the Number of Objects in the Image

Dr. Aly A. Farag Spring 2008

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.

figure, imshow(labeled); impixelregion


By default, the Pixel Region tool automatically associates itself with the image in the current figure. The Pixel Region tool draws a rectangle, called the pixel region rectangle, in the center of the visible part of the image. This rectangle defines which pixels are displayed in the Pixel Region tool. As you move the rectangle, the Pixel Region tool updates its display of pixel values. The following figure shows the Pixel Region rectangle positioned over the edges of two rice grains. Note how all the pixels in the rice grains have the values assigned by the bwlabel function and the background pixels have the value 0 (zero). Examining the Label Matrix with the Pixel Region Tool

Page 8 of 10

University of Louisville Electrical and Computer Engineering


Step 9: Display the Label Matrix as a Pseudocolor Indexed Image

Dr. Aly A. Farag Spring 2008

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.

pseudo_color = label2rgb(labeled, @spring, 'c', 'shuffle'); figure, imshow(pseudo_color);


Label Matrix Displayed as Pseudocolor Image

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 =

101x1 struct array with fields: Area Centroid BoundingBox


To find the area of the 51st labeled component (grain of rice), access the Area field in the 51st element in the graindata structure array. Note that structure field names are case sensitive.

area51 = graindata(51).Area

Page 9 of 10

University of Louisville Electrical and Computer Engineering


returns the following results

Dr. Aly A. Farag Spring 2008

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

You might also like