Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pmi432 / LR07 / 2read / image processing toolbox guide.pdf
Скачиваний:
150
Добавлен:
18.03.2015
Размер:
18.08 Mб
Скачать

Analyzing Images

Tracing Object Boundaries in an Image

The toolbox includes two functions you can use to find the boundaries of objects in a binary image:

bwtraceboundary

bwboundaries

The bwtraceboundary function returns the row and column coordinates of all the pixels on the border of an object in an image. You must specify the location of a border pixel on the object as the starting point for the trace.

The bwboundaries function returns the row and column coordinates of border pixels of all the objects in an image.

For both functions, the nonzero pixels in the binary image belong to an object, and pixels with the value 0 (zero) constitute the background.

The following example uses bwtraceboundary to trace the border of an object in a binary image. Then, using bwboundaries, the example traces the borders of all the objects in the image:

1Read image and display it.

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

11-15

11 Analyzing and Enhancing Images

2Convert the image to a binary image. bwtraceboundary and bwboundaries only work with binary images.

BW = im2bw(I); imshow(BW)

3Determine the row and column coordinates of a pixel on the border of the object you want to trace. bwboundary uses this point as the starting location for the boundary tracing.

dim = size(BW)

col = round(dim(2)/2)-90; row = min(find(BW(:,col)))

4Call bwtraceboundary to trace the boundary from the specified point. As required arguments, you must specify a binary image, the row and column coordinates of the starting point, and the direction of the first step. The example specifies north ('N'). For information about this parameter, see

11-16

Analyzing Images

“Choosing the First Step and Direction for Boundary Tracing” on page 11-18.

boundary = bwtraceboundary(BW,[row, col],'N');

5Display the original grayscale image and use the coordinates returned by bwtraceboundary to plot the border on the image.

imshow(I) hold on;

plot(boundary(:,2),boundary(:,1),'g','LineWidth',3);

6To trace the boundaries of all the coins in the image, use the bwboundaries function. By default, bwboundaries finds the boundaries of all objects in an image, including objects inside other objects. In the binary image used in this example, some of the coins contain black areas that bwboundaries interprets as separate objects. To ensure that bwboundaries only traces the coins, use imfill to fill the area inside each coin.

BW_filled = imfill(BW,'holes'); boundaries = bwboundaries(BW_filled);

bwboundaries returns a cell array, where each cell contains the row/column coordinates for an object in the image.

7Plot the borders of all the coins on the original grayscale image using the coordinates returned by bwboundaries.

for k=1:10

11-17

11 Analyzing and Enhancing Images

b = boundaries{k}; plot(b(:,2),b(:,1),'g','LineWidth',3);

end

Choosing the First Step and Direction for Boundary Tracing

For certain objects, you must take care when selecting the border pixel you choose as the starting point and the direction you choose for the first step parameter (north, south, etc.).

For example, if an object contains a hole and you select a pixel on a thin part of the object as the starting pixel, you can trace the outside border of the object or the inside border of the hole, depending on the direction you choose for the first step. For filled objects, the direction you select for the first step parameter is not as important.

To illustrate, this figure shows the pixels traced when the starting pixel is on a thin part of the object and the first step is set to north and south. The connectivity is set to 8 (the default).

11-18

Analyzing Images

Impact of First Step and Direction Parameters on Boundary Tracing

11-19

Соседние файлы в папке 2read