computer graphics - University of Engineering and Technology, Taxila

advertisement

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

COMPUTER GRAPHICS

&

IMAGE PROCESSING

LAB MANUAL 16

PREPARED BY:: ENGR. ALI JAVED

UET Taxila Computer Graphics & Image Processing 7 th Term-SE

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

MORPHOLOGICAL IMAGE PROCESSING

LAB OBJECTIVE:

The objective of this lab is to understand & implement

1.

Dilation

2.

Erosion

3.

Opening

4.

Closing

5.

Skeletonization

6.

Convex Hull

Dilation and Erosion

Dilation and erosion are two fundamental morphological operations. Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels on object boundaries. The number of pixels added or removed from the objects in an image depends on the size and shape of the structuring element used to process the image.

Dilating an Image

To dilate an image, use the imdilate function. The imdilate function accepts two primary arguments:

 The input image to be processed (grayscale, binary, or packed binary image)

 A structuring element object, returned by the strel function, or a binary matrix defining the neighborhood of a structuring element

imdilate also accepts two optional arguments: PADOPT and PACKOPT. The PADOPT argument affects the size of the output image. The PACKOPT argument identifies the input image as packed binary.

 Imdilate

Dilate image

Syntax

IM2 = imdilate(IM,SE)

IM2 = imdilate(IM,NHOOD)

IM2 = imdilate(IM,SE,PACKOPT)

IM2 = imdilate(...,PADOPT)

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Description

IM2 = imdilate(IM,SE) dilates the grayscale, binary, or packed binary image IM, returning the dilated image, IM2. The argument SE is a structuring element object, or array of structuring element objects, returned by the strel function.

If IM is logical and the structuring element is flat, imdilate performs binary dilation; otherwise, it performs grayscale dilation. If SE is an array of structuring element objects, imdilate performs multiple dilations of the input image, using each structuring element in SE in succession.

IM2 = imdilate(IM,NHOOD) dilates the image IM, where NHOOD is a matrix of 0's and 1's that specifies the structuring element neighborhood. This is equivalent to the syntax imdilate(IM,strel(NHOOD)). The imdilate function determines the center element of the neighborhood by floor((size(NHOOD)+1)/2).

IM2 = imdilate(IM,SE,PACKOPT) or imdilate(IM,NHOOD,PACKOPT) specifies whether IM is a packed binary image. PACKOPT can have either of the following values. Default value is enclosed in braces

({}).

IM2 = imdilate(...,PADOPT) specifies the size of the output image. PADOPT can have either of the following values. Default value is enclosed in braces ({}).

PADOPT is analogous to the optional SHAPE argument to the conv2 and filter2 functions.

Class Support

IM can be logical or numeric and must be real and nonsparse. It can have any dimension. If IM is logical, SE must be flat. The output has the same class as the input. If the input is packed binary, then the output is also packed binary.

Examples

This example dilates a binary image with a vertical line structuring element. bw = imread('text.png'); se = strel('line',11,90); bw2 = imdilate(bw,se); imshow(bw), title('Original') figure, imshow(bw2), title('Dilated')

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

This example dilates a grayscale image with a rolling ball structuring element.

I = imread('cameraman.tif'); se = strel('ball',5,5);

I2 = imdilate(I,se); imshow(I), title('Original') figure, imshow(I2), title('Dilated')

To determine the domain of the composition of two flat structuring elements, dilate the scalar value 1 with both structuring elements in sequence, using the 'full' option. se1 = strel('line',3,0) se1 =

Flat STREL object containing 3 neighbors.

Neighborhood:

1 1 1 se2 = strel('line',3,90) se2 =

Flat STREL object containing 3 neighbors.

Neighborhood:

1

1

1 composition = imdilate(1,[se1 se2],'full') composition =

1 1 1

1 1 1

1 1 1

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Example

This example dilates a simple binary image containing one rectangular object.

BW = zeros(9,10);

BW(4:6,4:7) = 1

BW =

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 0 0

0 0 0 1 1 1 1 0 0 0

0 0 0 1 1 1 1 0 0 0

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

To expand all sides of the foreground component, the example uses a 3-by-3 square structuring element object.

SE = strel('square',3)

SE =

Flat STREL object containing 3 neighbors.

Neighborhood:

1 1 1

1 1 1

1 1 1

To dilate the image, pass the image BW and the structuring element SE to the imdilate function. Note how dilation adds a rank of 1's to all sides of the foreground object.

BW2 = imdilate(BW,SE)

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Eroding an Image

To erode an image, use the imerode function. The imerode function accepts two primary arguments:

The input image to be processed (grayscale, binary, or packed binary image)

A structuring element object, returned by the strel function, or a binary matrix defining the neighborhood of a structuring element imerode also accepts three optional arguments: PADOPT, PACKOPT, and M.

The PADOPT argument affects the size of the output image. The PACKOPT argument identifies the input image as packed binary. If the image is packed binary, M identifies the number of rows in the original image.

 Imerode

Erode image

Syntax

IM2 = imerode(IM,SE)

IM2 = imerode(IM,NHOOD)

IM2 = imerode(IM,SE,PACKOPT,M)

IM2 = imerode(...,PADOPT)

Description

IM2 = imerode(IM,SE) erodes the grayscale, binary, or packed binary image IM, returning the eroded image IM2. The argument SE is a structuring element object or array of structuring element objects returned by the strel function.

If IM is logical and the structuring element is flat, imerode performs binary dilation; otherwise it performs grayscale erosion. If SE is an array of structuring element objects, imerode performs multiple erosions of the input image, using each structuring element in SE in succession.

IM2 = imerode(IM,NHOOD) erodes the image IM, where NHOOD is an array of 0's and 1's that specifies the structuring element neighborhood. This is equivalent to the syntax imerode(IM,strel(NHOOD)). The imerode function determines the center element of the neighborhood by floor((size(NHOOD)+1)/2)

IM2 = imerode(IM,SE,PACKOPT,M) or imerode(IM,NHOOD,PACKOPT,M) specifies whether IM is a packed binary image and, if it is, provides the row dimension M of the original unpacked image.

PACKOPT can have either of the following values. Default value is enclosed in braces ({}).

If PACKOPT is 'ispacked', you must specify a value for M.

IM2 = imerode(...,PADOPT) specifies the size of the output image. PADOPT can have either of the following values. Default value is enclosed in braces ({}).

PADOPT is analogous to the SHAPE input to the CONV2 and FILTER2 functions.

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Class Support

IM can be numeric or logical and it can be of any dimension. If IM is logical and the structuring element is flat, the output image is logical; otherwise the output image has the same class as the input. If the input is packed binary, then the output is also packed binary.

Examples

This example erodes a binary image with a disk structuring element. originalBW = imread('circles.png'); se = strel('disk',11); erodedBW = imerode(originalBW,se); imview(originalBW), imview(erodedBW)

This example erodes a grayscale image with a rolling ball.

I = imread('cameraman.tif'); se = strel('ball',5,5);

I2 = imerode(I,se); imshow(I), title('Original') figure, imshow(I2), title('Eroded')

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Example

The following example erodes the binary image circbw.tif:

1.

Read the image into the MATLAB workspace.

BW1 = imread('circbw.tif');

2.

Create a structuring element. The following code creates a diagonal structuring element object.

SE = strel('arbitrary',eye(5));

SE=

Flat STREL object containing 5 neighbors.

Neighborhood:

1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

0 0 0 0 1

3.

Call the imerode function, passing the image BW and the structuring element SE as arguments.

BW2 = imerode(BW1,SE);

Notice the diagonal streaks on the right side of the output image. These are due to the shape of the structuring element. imshow(BW1) figure, imshow(BW2)

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Combining Dilation and Erosion

Dilation and erosion are often used in combination to implement image processing operations. For example, the definition of a morphological opening of an image is an erosion followed by a dilation, using the same structuring element for both operations. The related operation, morphological closing of an image, is the reverse: it consists of dilation followed by an erosion with the same structuring element.

The following section uses imdilate and imerode to illustrate how to implement a morphological opening. Note, however, that the toolbox already includes the imopen function, which performs this processing. The toolbox includes functions that perform many common morphological operations.

Morphological Opening and Closing

You can use morphological opening to remove small objects from an image while preserving the shape and size of larger objects in the image. For example, you can use the imopen function to remove all the circuit lines from the original circuit image, circbw.tif, creating an output image that contains only the rectangular shapes of the microchips.

To morphologically open the image, perform these steps:

1.

Read the image into the MATLAB workspace.

BW1 = imread('circbw.tif');

2.

Create a structuring element.

SE = strel('rectangle',[40 30]);

The structuring element should be large enough to remove the lines when you erode the image, but not large enough to remove the rectangles. It should consist of all 1's, so it removes everything but large contiguous patches of foreground pixels.

3.

Erode the image with the structuring element.

BW2 = imerode(BW1,SE); imshow(BW2)

This removes all the lines, but also shrinks the rectangles.

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

4.

To restore the rectangles to their original sizes, dilate the eroded image using the same structuring element, SE.

BW3 = imdilate(BW2,SE); imshow(BW3)

Dilation- and Erosion-Based Functions

 Imclose

 Imopen

 Imclose

Close an image

Syntax

IM2 = imclose(IM,SE)

IM2 = imclose(IM,NHOOD)

Description

IM2 = imclose(IM,SE) performs morphological closing on the grayscale or binary image IM, returning the closed image, IM2. The structuring element, SE, must be a single structuring element object, as opposed to an array of objects.

IM2 = imclose(IM,NHOOD) performs closing with the structuring element strel(NHOOD), where NHOOD is an array of 0's and 1's that specifies the structuring element neighborhood.

Class Support

IM can be any numeric or logical class and any dimension, and must be nonsparse. If IM is logical, then

SE must be flat. IM2 has the same class as IM.

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Example

This example uses imclose to join the circles in the image together by filling in the gaps between them and by smoothening their outer edges.

1.

Read the image into the MATLAB workspace and view it. originalBW = imread('circles.png'); imview(originalBW);

2.

Create a disk-shaped structuring element. Use a disk structuring element to preserve the circular nature of the object. Specify a radius of 10 pixels so that the largest gap gets filled. se = strel('disk',10);

3.

Perform a morphological close operation on the image. closeBW = imclose(originalBW,se); imview(closeBW)

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 Imopen

Open an image

Syntax

IM2 = imopen(IM,SE)

IM2 = imopen(IM,NHOOD)

Description

IM2 = imopen(IM,SE) performs morphological opening on the grayscale or binary image IM with the structuring element SE. The argument SE must be a single structuring element object, as opposed to an array of objects.

IM2 = imopen(IM,NHOOD) performs opening with the structuring element strel(NHOOD), where

NHOOD is an array of 0's and 1's that specifies the structuring element neighborhood.

Class Support

IM can be any numeric or logical class and any dimension, and must be nonsparse. If IM is logical, then

SE must be flat. IM2 has the same class as IM.

Example

This example uses imopen to filter out the smaller objects in an image.

1.

Read the image into the MATLAB workspace and display it.

I = imread('snowflakes.png'); imview(I)

2.

Create a disk-shaped structuring element with a radius of 5 pixels. se = strel('disk',5);

3.

Remove snowflakes having a radius less than 5 pixels by opening it with the disk-shaped structuring element created in step 2.

I_opened = imopen(I,se); imview(I_opened,[])

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Skeletonization

To reduce all objects in an image to lines, without changing the essential structure of the image, use the bwmorph function. This process is known as skeletonization.

BW1 = imread('circbw.tif');

BW2 = bwmorph(BW1,'skel',Inf); imshow(BW1) figure, imshow(BW2)

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Convex Hull

Convhull

Convex hull

Syntax

K = convhull(x,y)

K = convhull(x,y,options)

[K,a] = convhull(...)

Description

K = convhull(x,y) returns indices into the x and y vectors of the points on the convex hull. convhull uses Qhull.

K = convhull(x,y,options) specifies a cell array of strings options to be used in Qhull via convhulln. The default option is {'Qt'}.

If options is [], the default options are used. If options is {''}, no options will be used, not even the default. For more information on Qhull and its options.

[K,a] = convhull(...) also returns the area of the convex hull.

Visualization

Use plot to plot the output of convhull.

Examples xx = -1:.05:1; yy = abs(sqrt(xx));

[x,y] = pol2cart(xx,yy); k = convhull(x,y); plot(x(k),y(k),'r-',x,y,'b+')

Computer Graphics & Image Processing 7 th Term-SE UET Taxila

Download