# bwselect

Select objects in binary image

## Syntax

• ``BW2 = bwselect(BW,c,r,n)``
example
• ``BW2 = bwselect(BW,n)``
• ``````[BW2, idx] = bwselect(___)``````
• ``BW2 = bwselect(x,y,BW,xi,yi,n)``
• ``````[x,y,BW2,idx,xi,yi] = bwselect(___)``````

## Description

example

````BW2 = bwselect(BW,c,r,n)` returns a binary image containing the objects that overlap the pixel (`r,c`), where `n` specifies the connectivity. Objects are connected sets of `on` pixels, that is, pixels having a value of `1`. By default, `bwselect` looks for 4-connected objects.This function supports code generation (see ).```
````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. Press Backspace or Delete to remove the previously selected point. A shift-click, right-click, or double-click selects the final point; press Return to finish 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 `BW`. The arguments `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`.Code Generation support: Yes.MATLAB Function Block support: Yes.```

## Examples

collapse all

### Select Objects in Binary Image

Select objects in a binary image and create a new image containing only those objects.

Read binary image into the workspace.

```BW = imread('text.png'); ```

Specify the locations of objects in the image using row and column indices.

```c = [43 185 212]; r = [38 68 181]; ```

Create a new binary image containing only the selected objects. This example specifies 4-connected objects.

```BW2 = bwselect(BW,c,r,4); ```

Display the original image and the new image side-by-side.

```imshowpair(BW,BW2,'montage'); ```

## Input Arguments

collapse all

### `BW` — Input binary image2-D, nonsparse, logical or numeric matrix

Input binary image, specified as a 2-D, nonsparse, logical or numeric matrix. If you do not specify an output argument, `bwselect` displays the output image in a new figure.

Example: `BW = imread('text.png');`

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

### `c` — Column indexnumeric scalar or vector

Column index, specified as a numeric scalar or vector. If `c` and `r` are equal-length vectors, `BW2` contains the sets of objects overlapping with any of the pixels `(r(k),c(k))`.

Example: `c = [43 185 212];`

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

### `r` — Row indexnumeric scalar or vector

Row index, specified as a numeric scalar or vector. If `r` and `c` are equal-length vectors, `BW2` contains the sets of objects overlapping with any of the pixels `(r(k),c(k))`.

Example: `r = [38 68 181];`

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

### `n` — Connectivity`8` (default) | `4`

Connectivity, specified as either the value `4` or `8`.

ValueDescription
`4`4-connected objects
`8`8-connected objects

Example: `BW2 = bwselect(BW,c,r,4);`

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

### `x` — x coordinates of nondefault coordinate systemnumeric scalar or vector

x coordinates of nondefault coordinate system, specified as a numeric scalar or vector.

Example: `x = [19.5 23.5];`

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

### `y` — y coordinates of nondefault coordinate systemnumeric scalar or vector

y coordinates of nondefault coordinate system, specified as a numeric scalar or vector.

Example: `y = [8.0 12.0];`

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

### `xi` — x coordinates of locations in nondefault coordinate systemnumeric scalar or vector

x coordinates of locations in nondefault coordinate system, specified as a numeric scalar or vector.

Example: `x = [19.5 23.5];`

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

### `yi` — y coordinates of locations in nondefault coordinate systemnumeric scalar or vector

y coordinates of locations in nondefault coordinate system, specified as a numeric scalar or vector.

Example: `y = [8.0 12.0];`

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

## Output Arguments

collapse all

### `BW2` — Binary image containing objects that overlap the specified pixelslogical array

Binary image containing objects that overlap the specified pixels, returned as a logical array.

If you do not specify an output argument, `bwselect` displays the output image in a new figure.

### `idx` — Linear indices of the pixels belonging to the selected objectsnumeric vector

Linear indices of the pixels belonging to the selected objects, returned as a numeric vector.

expand all

### Code Generation

This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic `MATLAB 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, `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)`

In addition, the optional fourth input argument, `n`, must be a compile-time constant. .

### MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.