Filtering an ROI

Overview of ROI Filtering

Filtering a region of interest (ROI) is the process of applying a filter to a region in an image, where a binary mask defines the region. For example, you can apply an intensity adjustment filter to certain regions of an image.

To filter an ROI in an image, use the roifilt2 function. When you call roifilt2, you specify:

  • Input grayscale image to be filtered

  • Binary mask image that defines the ROI

  • Filter (either a 2-D filter or function)

roifilt2 filters the input image and returns an image that consists of filtered values for pixels where the binary mask contains 1s and unfiltered values for pixels where the binary mask contains 0s. This type of operation is called masked filtering.

roifilt2 is best suited for operations that return data in the same range as in the original image, because the output image takes some of its data directly from the input image. Certain filtering operations can result in values outside the normal image data range (i.e., [0,1] for images of class double, [0,255] for images of class uint8, and [0,65535] for images of class uint16). For more information, see the reference page for roifilt2.

Filtering a Region in an Image

This example uses masked filtering to increase the contrast of a specific region of an image:

  1. Read in the image.

    I = imread('pout.tif');
  2. Create the mask.

    This example uses the mask BW created by the createMask method in the section Creating a Binary Mask. The region of interest specified is the child's face.

  3. Use fspecial to create the filter:

    h = fspecial('unsharp');
  4. Call roifilt2, specifying the filter, the image to be filtered, and the mask:

    I2 = roifilt2(h,I,BW);
    figure, imshow(I2)

    Image Before and After Using an Unsharp Filter on the Region of Interest

Specifying the Filtering Operation

roifilt2 also enables you to specify your own function to operate on the ROI. This example uses the imadjust function to lighten parts of an image:

  1. Read in the image.

    I = imread('cameraman.tif');
  2. Create the mask. In this example, the mask is a binary image containing text. The mask image must be cropped to be the same size as the image to be filtered:

    BW = imread('text.png');
    mask = BW(1:256,1:256); 
  3. Create the function you want to use as a filter:

    f = @(x) imadjust(x,[],[],0.3);
  4. Call roifilt2, specifying the image to be filtered, the mask, and the filter. The resulting image, I2, has the text imprinted on it:

    I2 = roifilt2(I,mask,f);

    Image Brightened Using a Binary Mask Containing Text

Was this topic helpful?