bwboundaries

来源:互联网 发布:南京市行知基地 编辑:程序博客网 时间:2024/05/29 13:09

bwboundaries

Trace region boundaries in binary image


collapse all in page


Syntax

  • B = bwboundaries(BW)
    example
  • B = bwboundaries(BW,conn)
  • B = bwboundaries(BW,conn,options)
    example
  • [B,L]= bwboundaries(___)
    example
  • [B,L,N,A] = bwboundaries(___)
    example

Description

example

B = bwboundaries(BW) traces the exterior boundaries of objects, as well as boundaries of holes inside these objects, in the binary image BWbwboundaries also descends into the outermost objects (parents) and traces their children (objects completely enclosed by the parents). 

Returns B, a cell array of boundary pixel locations.

B = bwboundaries(BW,conn) traces the exterior boundaries of objects, where conn specifies the connectivity to use when tracing parent and child boundaries.

example

B = bwboundaries(BW,conn,options) traces the exterior boundaries of objects, where optionsis either the string 'holes' or 'noholes', specifying whether you want to include the boundaries of holes inside other objects.

example

[B,L]= bwboundaries(___) returns a label matrix L where objects and holes are labeled.

example

[B,L,N,A] = bwboundaries(___) returns N, the number of objects found, and A, an adjacency matrix.

Code Generation support: Yes.

MATLAB® Function Block support: No.

Examples

collapse all

Overlay Region Boundaries on Image

Open This Example

Read grayscale image into the workspace.

I = imread('rice.png');

Convert grayscale image to binary image using local adaptive thresholding.

BW = imbinarize(I);

Calculate boundaries of regions in image and overlay the boundaries on the image.

[B,L] = bwboundaries(BW,'noholes');imshow(label2rgb(L, @jet, [.5 .5 .5]))hold onfor k = 1:length(B)   boundary = B{k};   plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)end

Overlay Region Boundaries on Image and Annotate with Region Numbers

Open This Example

Read binary image into the workspace.

BW = imread('blobs.png');

Calculate boundaries of regions in the image.

[B,L,N,A] = bwboundaries(BW);

Display the image with the boundaries overlaid. Add the region number next to every boundary (based on the label matrix). Use the zoom tool to read individual labels.

imshow(BW); hold on;colors=['b' 'g' 'r' 'c' 'm' 'y'];for k=1:length(B),  boundary = B{k};  cidx = mod(k,length(colors))+1;  plot(boundary(:,2), boundary(:,1),...       colors(cidx),'LineWidth',2);  %randomize text position for better visibility  rndRow = ceil(length(boundary)/(mod(rand*k,7)+1));  col = boundary(rndRow,2); row = boundary(rndRow,1);  h = text(col+1, row-1, num2str(L(row,col)));  set(h,'Color',colors(cidx),'FontSize',14,'FontWeight','bold');end

Display the adjacency matrix using the spy function.

figurespy(A);

Display Object Boundaries in Red and Hole Boundaries in Green

Open This Example

Read binary image into workspace.

BW = imread('blobs.png');

Calculate boundaries.

[B,L,N] = bwboundaries(BW);

Display object boundaries in red and hole boundaries in green.

imshow(BW); hold on;for k=1:length(B),   boundary = B{k};   if(k > N)     plot(boundary(:,2), boundary(:,1), 'g','LineWidth',2);   else     plot(boundary(:,2), boundary(:,1), 'r','LineWidth',2);   endend

Display Parent Boundaries in Red and Holes in Green

Open This Example

Read image into workspace.

BW = imread('blobs.png');

Display parent boundaries in red and their holes in green.

[B,L,N,A] = bwboundaries(BW);figure; imshow(BW); hold on;% Loop through object boundariesfor k = 1:N    % Boundary k is the parent of a hole if the k-th column    % of the adjacency matrix A contains a non-zero element    if (nnz(A(:,k)) > 0)        boundary = B{k};        plot(boundary(:,2),...            boundary(:,1),'r','LineWidth',2);        % Loop through the children of boundary k        for l = find(A(:,k))'            boundary = B{l};            plot(boundary(:,2),...                boundary(:,1),'g','LineWidth',2);        end    endend

Input Arguments

collapse all

BW — Input binary imagereal, nonsparse, 2-D logical or numeric array

Binary input image, specified as a real, nonsparse, 2-D logical or numeric array. BW must be a binary image where nonzero pixels belong to an object and 0 pixels constitute the background. The following figure illustrates these components.

Example: B = bwboundaries(BW);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

conn — Connectivity8 (default) | 4

Connectivity, specified as either of the following scalar values:

Value

Meaning

4

4-connected neighborhood

8

8-connected neighborhood. This is the default.

Example: B = bwboundaries(BW,4);

Data Types: double

options — Determine whether to search for both parent and child boundaries'holes' (default) | 'noholes'

Determine whether to search for both parent and child boundaries, specified as either of the following strings:

String

Meaning

'holes'

Search for both object and hole boundaries. This is the default.

'noholes'

Search only for object (parent and child) boundaries. This can provide better performance.

Example: B = bwboundaries(BW,'noholes',4);

Data Types: char

Output Arguments

collapse all

B — Row and column coordinates of boundary pixelsP-by-1 cell array

Row and column coordinates of boundary pixels, returned as a P-by-1 cell array, where P is the number of objects and holes. Each cell in the cell array contains a Q-by-2 matrix. Each row in the matrix contains the row and column coordinates of a boundary pixel. Q is the number of boundary pixels for the corresponding region.

L — Label matrix of contiguous regions2-D array of nonnegative integers

Label matrix of contiguous regions, returned as a 2-D array of nonnegative integers of class double. The kth region includes all elements in L that have value k. The number of objects and holes represented by L is equal to max(L(:)). The zero-valued elements of L make up the background.

N — Number of objects foundnumeric scalar

Number of objects found, returned as a numeric scalar of class double.

A — Parent-child dependencies between boundaries and holessquare, sparse, logical matrix

Parent-child dependencies between boundaries and holes, returned as a square, sparse, logical matrix of classdouble with side of length max(L(:)). The rows and columns of A correspond to the positions of boundaries stored in B. The first N cells in B are object boundaries. A(i,j)=1 means that object i is a child of object j. .The boundaries that enclose or are enclosed by the k-th boundary can be found using A as follows:

enclosing_boundary  = find(A(m,:));enclosed_boundaries = find(A(:,m));

More About

collapse all

Code Generation

This function supports the generation of C code using MATLAB Coder™. Note that if you choose the genericMATLAB Host Computer target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

When generating code, note the following:

  • The parameter conn must be a compile-time constant.

  • The parameter options must be a compile-time constant.

  • The return value A can only be a full matrix, not a sparse matrix.

Algorithms

The bwboundaries function implements the Moore-Neighbor tracing algorithm modified by Jacob's stopping criteria. This function is based on the boundaries function presented in the first edition of Digital Image Processing Using MATLAB, by Gonzalez, R. C., R. E. Woods, and S. L. Eddins, New Jersey, Pearson Prentice Hall, 2004.

原创粉丝点击