Select objects in binary image


BW2 = bwselect(BW,c,r,n)
BW2 = bwselect(BW,n)
[BW2,idx] = bwselect(...)
BW2 = bwselect(x,y,BW,xi,yi,n)
[x,y,BW2,idx,xi,yi] = bwselect(...)


BW2 = bwselect(BW,c,r,n) returns a binary image containing the objects that overlap the pixel (r,c). r and c can be scalars or equal-length vectors. If r and c are vectors, BW2 contains the sets of objects overlapping with any of the pixels (r(k),c(k)). n can have a value of either 4 or 8 (the default), where 4 specifies 4-connected objects and 8 specifies 8-connected objects. Objects are connected sets of on pixels (i.e., pixels having a value of 1).

BW2 = bwselect(BW,n) displays the image BW on the screen and lets you select the (r,c) coordinates using the mouse. If you omit BW, bwselect operates on the image in the current axes. Use normal button clicks to add points. Pressing Backspace or Delete removes the previously selected point. A shift-click, right-click, or double-click selects the final point; pressing Return finishes the selection without adding a point.

[BW2,idx] = bwselect(...) returns the linear indices of the pixels belonging to the selected objects.

BW2 = bwselect(x,y,BW,xi,yi,n) uses the vectors x and y to establish a nondefault spatial coordinate system for BW1. xi and yi are scalars or equal-length vectors that specify locations in this coordinate system.

[x,y,BW2,idx,xi,yi] = bwselect(...) returns the XData and YData in x and y, the output image in BW2, linear indices of all the pixels belonging to the selected objects in idx, and the specified spatial coordinates in xi and yi.

If bwselect is called with no output arguments, the resulting image is displayed in a new figure.

Code Generation

bwselect supports the generation of efficient, production-quality C/C++ code from MATLAB. When generating code, bwselect only supports the following syntaxes:

  • BW2 = bwselect(BW, c, r)

  • [BW2, idx] = bwselect(BW, c, r)

  • BW2 = bwselect(BW, c, r, n)

  • [BW2, idx] = bwselect(BW, c, r, n)

The optional fourth input argument, n, must be a compile-time constant. Generated code for this function uses a precompiled platform-specific shared library. To see a complete list of toolbox functions that support code generation, see List of Supported Functions with Usage Notes.

Class Support

The input image BW can be logical or numeric and must be 2-D and nonsparse. The output image BW2 is of class logical.


BW1 = imread('text.png');
c = [43 185 212];
r = [38 68 181];
BW2 = bwselect(BW1,c,r,4);
imshow(BW1), figure, imshow(BW2)

Was this topic helpful?