# roifilt2

Filter region of interest (ROI) in grayscale image

## Syntax

``J = roifilt2(h,I,BW)``
``J = roifilt2(I,BW,fun)``

## Description

example

````J = roifilt2(h,I,BW)` filters regions of interest (ROIs) in the 2-D grayscale image `I` using the 2-D linear filter `h`. The binary mask `BW` defines the ROIs in `I`. `roifilt2` returns an image that consists of filtered values for pixels in locations where `BW` contains `1`s, and unfiltered values for pixels in locations where `BW` contains `0`s.```
````J = roifilt2(I,BW,fun)` processes the data in ROIs of `I` using the function `fun`.```

## Examples

collapse all

Read an image into the workspace.

`I = imread('eight.tif');`

Define the vertices of the mask polygon.

```c = [222 272 300 270 221 194]; r = [21 21 75 121 121 75];```

`BW = roipoly(I,c,r);`

Filter the region of the image `I` specified by the mask `BW`.

```H = fspecial('unsharp'); J = roifilt2(H,I,BW);```

Display the original image and the filtered image.

`imshow(I)`

```figure imshow(J)```

## Input Arguments

collapse all

Linear filter, specified as a 2-D numeric matrix.

Data Types: `double`

Grayscale image, specified as a 2-D numeric matrix.

• If you specify a filter, `h`, then `I` can be any of the listed data types.

• If you specify a function handle, `fun`, then `I` can be any data type supported by `fun`.

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

Mask, specified as a 2-D logical matrix or a 2-D numeric matrix of the same size as `I`. For numeric input, any nonzero pixels are considered to be `1` (`true`).

## Output Arguments

collapse all

Filtered image, returned as a 2-D matrix.

• If you specify a filter, `h`, then `J` is of the same data type as the input image, `I`.

• If you specify a function handle, `fun`, then the data type of `J` is determined by `fun`.

## Algorithms

If you specify a filter, `h`, then `roifilt2` calls `imfilter` to implement the filter.

## Version History

Introduced before R2006a

expand all