Main Content

ROI-Based Processing

Define and operate on regions of interest (ROI)

A region of interest (ROI) is a portion of an image that you want to filter or operate on in some way. The toolbox supports a set of ROI objects that you can use to create ROIs of many shapes, such circles, ellipses, polygons, rectangles, and hand-drawn shapes. After creation, you can use ROI object properties to customize their appearance and functioning. In addition, the ROI objects support object functions and events that you can use to implement interactive behavior. For example, using events, your application can execute custom code whenever the ROI changes position. As a convenience, the toolbox includes a parallel set of convenience functions for ROI creation. For example, to create a rectangular ROI, you can use images.roi.Rectangle or the corresponding convenience function drawrectangle. For more information about ROIs, see ROI Creation Overview.

A common use of an ROI is to create a binary mask image. In the mask image, pixels that belong to the ROI are set to 1 and pixels outside the ROI are set to 0. To create a mask, use the createMask object functions supported by most of the ROI objects. You can also create a mask without requiring an image using the poly2mask function.

Starting in R2018b, a new set of ROI objects replaced the previous set of ROI objects. The new objects provide better performance and more functional capabilities, such as face color transparency. With the new objects, you can also receive notification of interactions with the object, such as clicks or movement, using events. Although there are no plans to remove the old ROI objects at this time, switch to the new ROIs to take advantage of the additional capabilities and flexibility. For more information on migrating to the new ROIs, see ROI Migration.


expand all

AssistedFreehandAssisted freehand region of interest
CircleCircular region of interest
CrosshairCrosshair region of interest
CuboidCuboidal region of interest
EllipseElliptical region of interest
FreehandFreehand region of interest
LineLine region of interest
PointPoint region of interest
PolygonPolygonal region of interest
PolylinePolyline region of interest
RectangleRectangular region of interest
drawBegin drawing ROI interactively
drawassistedCreate freehand ROI on image with assistance from image edges
drawcircleCreate customizable circular ROI
drawcrosshairCreate customizable crosshair ROI
drawcuboidCreate customizable cuboidal ROI
drawellipseCreate customizable elliptical ROI
drawfreehandCreate customizable freehand ROI
drawlineCreate customizable linear ROI
drawpointCreate customizable point ROI
drawpolygonCreate customizable polygonal ROI
drawpolylineCreate customizable polyline ROI
drawrectangleCreate customizable rectangular ROI
reduceReduce density of points in ROI
beginDrawingFromPointBegin drawing ROI from specified point
inROIQuery if points are located in ROI
bringToFrontBring ROI to front of Axes stacking order
waitBlock MATLAB command line until ROI operation is finished
createMaskCreate binary mask image from ROI
roipolySpecify polygonal region of interest (ROI)
poly2maskConvert region of interest (ROI) polygon to region mask
regionfillFill in specified regions in image using inward interpolation
inpaintCoherentRestore specific image regions using coherence transport based image inpainting
inpaintExemplarRestore specific image regions using exemplar-based image inpainting
roicolorSelect region of interest (ROI) based on color
roifilt2Filter region of interest (ROI) in image
reducepolyReduce density of points in ROI using Ramer–Douglas–Peucker algorithm


Create ROIs

ROI Creation Overview

You can create ROI objects that represent ROIs of various shapes, including circles, ellipses, polygons, lines, polylines, rectangles, and hand-drawn shapes.

ROI Migration

Starting in R2018b, a new set of ROI objects replaced the previous set of ROI objects.

Filter ROIs and Create Masks

Overview of ROI Filtering

To filter a region of interest (ROI), first define a mask to separate the ROI from the background, then apply the filter to the ROI only.

Create a Binary Mask

A binary mask classifies image pixels as belonging to either the region of interest or the background.

Sharpen Region of Interest in an Image

This example shows how to use masked filtering to increase the sharpness of a specific region of interest.

Apply Custom Filter to Region of Interest in Image

This example shows how to define your own function and mask to filter a region of interest.

Fill Region of Interest in an Image

The process of filling a region of interest involves replacing all pixels in the region by interpolating inward from the boundary of the region.

Create Interactive ROIs Using Events and Callback Functions

Subsample or Simplify a Freehand ROI

This example shows how to subsample or reduce the number of points in a Freehand ROI object.

Rotate Image Interactively Using Rectangle ROI

This example shows how to rotate an image by using a Rectangle ROI with a callback function that calls imrotate when you move the ROI.

Use Polyline to Create Angle Measurement Tool

This example shows how to create an interactive tool that displays the angle between three vertices in a polyline ROI.

Measure Distances in an Image

This example shows how to use line ROIs to measure distances in an image.

Use Wait Function After Drawing ROI

This example shows how to define a custom wait function that blocks the MATLAB® command line until you finish positioning a rectangle.

Featured Examples