Region of Interest is a rectangular area in an image, to segment object for further processing. The ilustration is shown in Figure 1 below.

In the image above, a Region of Interest is defined at near top left of the image. Once the ROI defined, most OpenCV functions will performed only on that particular location. This is useful, for example when we want to crop an object from an image, or when we want to perform template matching within subimage. Note that the Region of Interest has to be inside the image.
To define Region of Interest, use the function:
cvSetImageROI( IplImage* img, CvRect rect )
Where img
is the source image and rect
is the area within the source image. To reset Region of Interest, use the function:
cvResetImageROI( IplImage* img )
Below are some samples where ROI is useful.
1. Crop an Object
Listing 1: Crop an object and save to new image
-
/* load image */
-
IplImage *img1 = cvLoadImage ( "elvita.jpg" , 1 ) ;
-
/* sets the Region of Interest
-
Note that the rectangle area has to be __INSIDE__ the image */
-
cvSetImageROI (img1 , cvRect ( 10 , 15 , 150 , 250 ) ) ;
-
/* create destination image
-
Note that cvGetSize will return the width and the height of ROI */
-
IplImage *img2 = cvCreateImage (cvGetSize (img1 ) ,
-
img1 ->depth ,
-
img1 ->nChannels ) ;
-
/* copy subimage */
-
cvCopy (img1 , i