OpenCV Region of Interest (ROI)

Mar 13, 2009 | Tags: OpenCV | del.icio.us del.icio.us | digg Digg

In this article, I will explain about Region of Interest (ROI) in OpenCV and some usage examples.

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

An image with Region of Interest defined
Fig 1. An image with Region of Interest defined

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

  1. /* load image */
  2. IplImage *img1 = cvLoadImage("elvita.jpg", 1);
  3.  
  4. /* sets the Region of Interest
  5.    Note that the rectangle area has to be __INSIDE__ the image */
  6. cvSetImageROI(img1, cvRect(10, 15, 150, 250));
  7.  
  8. /* create destination image
  9.    Note that cvGetSize will return the width and the height of ROI */
  10. IplImage *img2 = cvCreateImage(cvGetSize(img1),
  11.                                img1->depth,
  12.                                img1->nChannels);
  13.  
  14. /* copy subimage */
  15. cvCopy(img1, img2, NULL);
  16.  
  17. /* always reset the Region of Interest */
  18. cvResetImageROI(img1);

2. Adding Two Images with Different Size

Listing 2: Adding two images with different size

  1. /* load images
  2.    Note that both images have different width & height */
  3. IplImage *img1 = cvLoadImage("elvita.jpg", 1);  /* bigger image  */
  4. IplImage *img2 = cvLoadImage("fifi.jpg", 1);    /* smaller image */
  5.  
  6. /* define rectangle for ROI */
  7. CvRect rect = cvRect(25, 25, img2->width, img2->height);
  8.  
  9. /* sets Region of Interest */
  10. cvSetImageROI(img1, rect);
  11.  
  12. /* Add both images
  13.    Note that now both images have 'same' width & height */
  14. cvAdd(img1, img2, img1, NULL);
  15.  
  16. /* always reset the region of interest */
  17. cvResetImageROI(img1);

3. Performing Template Matching in a Specific Area

Listing 3: Template Matching with Region of Interest defined

  1. IplImage *img = cvLoadImage("myphoto.jpg", 1);
  2. IplImage *tpl = cvLoadImage("eye.jpg", 1);
  3.  
  4. CvRect rect = cvRect(25, 25, 120, 120);
  5.  
  6. cvSetImageROI(img, rect);
  7.  
  8. IplImage *res = cvCreateImage(cvSize(rect.width  - tpl->width  + 1,
  9.                                      rect.height - tpl->height + 1),
  10.                               IPL_DEPTH_32F, 1);
  11.  
  12. /* perform template matching */
  13. cvMatchTemplate(img, tpl, res, CV_TM_SQDIFF);
  14.  
  15. /* find best matches location */
  16. CvPoint    minloc, maxloc;
  17. double    minval, maxval;
  18. cvMinMaxLoc(res, &minval, &maxval, &minloc, &maxloc, 0);
  19.  
  20. /* draw rectangle */
  21. cvRectangle(img,
  22.             cvPoint(minloc.x, minloc.y),
  23.             cvPoint(minloc.x + tpl->width, minloc.y + tpl->height),
  24.             CV_RGB(255, 0, 0), 1, 0, 0 );
  25.  
  26. cvResetImageROI(img);

In the example above, we define Region of Interest before performing Template Matching. This will increase the speed since computation only performed on small area. For more information about template matching, read my tutorial about Template Matching in OpenCV.

In this article, I have explained Region of Interest in OpenCV and provided some usage examples. There are many more problems in Image Processing where we find ROI is useful.

The morals:

  1. ROI segments object in an image.
  2. ROI respected by most of OpenCV functions.
  3. Always reset Region of Interest when it is no longer needed. This will saves you from hours of debugging.

Update on October 26, 2009:

Some readers asked me about accessing the image pixels in the ROI. The simplest method would be copying the subimage to another image, then you can access this new image as usual.

Listing 4: Accessing ROI pixels #1

/* assuming that you have 8-bit 3-channels img*/

/* here's the ROI */
CvRect rect = cvRect(10, 20, 50, 60);

/* dst image */
IplImage* subimg;

/* copy ROI to subimg */
cvSetImageROI(img, rect);
cvCopy(img, subimg, NULL);
cvResetImageROI(img);

/* now you have the ROI in subimg. access the pixels as usual */

Or you can access the image directly using the ROI boundaries:

Listing 5: Accessing ROI pixels #2

/* assuming that you have 8-bit 3-channels img*/

/* here's the ROI */
CvRect rect = cvRect(10, 20, 50, 60);

cvSetImageROI(img, rect);

/* say you want to set all pixels in the ROI to 0 */
for (i = rect.y; i < (rect.y + rect.height); i++) {
    for (j = rect.x; j < (rect.x + rect.width); j++) {
        ((uchar*)(img->imageData + i * img->widthStep))[j*3] = 0;
        ((uchar*)(img->imageData + i * img->widthStep))[j*3+1] = 0;
        ((uchar*)(img->imageData + i * img->widthStep))[j*3+2] = 0;
    }
}

cvResetImageROI(img);

Related Articles

Recommended Books

11 Comments

Yi Xie on Nov 11, 2009:

example in Listing 4: Accessing ROI pixels #1 fails to work for the cvcopy copy the whole image including the ROI information. When we read the subimg, we still get the pixels of the whole image.

momin on Mar 1, 2010:

Dear Sir,

Hope you are fine. I am doing a project on detection of surface defects for CITRON. In this connection I am trying to use opencv for image processing though I am totally new in opencv. For my research,Please help me regarding following questions. I appreciate it.

a) how can I calculate RGB value at different X,Y position of an image?

b)for analysing, how can I detect only the target objects (actually the circular ROI shape,for CITRON)from the source image.

Thanking you and I look forward to hear from you.

Pinky on Mar 5, 2010:

I am also interest on this OpenCV.
I have a same question as mr. momin
please,i look forword to hear from both of you.

Thanks.

Skass on May 3, 2010:

Hi!
I have a question: how can I do to save the ROI into a image file?

Thanks!

Nash on May 4, 2010:

cvSaveImage

donny on May 10, 2010:

Hello

I am trying to implement moments on an image. the image here is of a solar wafer. though i see functions to calculate the moments i dont know how i can apply moments to an image. could you please give me your suggestions on this .

thanks for your help.

donny

didi on Jul 2, 2010:

Nice. Thank you for the information.

haleer_you on Aug 1, 2010:

Is it possible to combine Phase correlation and Region of Interest?

Sam on Aug 5, 2010:

Great stuff, used the cropping approach in an OpenCVSharp Project and worked like a charm.

If anyone is using the OpenCVSharp C# port, cvRect is defined as a Struct data type and properties are set as:

rect.X, rect.Y, rect.width, rect.height.

Thanks again.

Sam.

Gordon on Nov 9, 2010:

Hi,

I wish to create a sub image to perform blob detection on. I am doing a cvClone of the image when the ROI is set to the region I wish. However, this seems to fail, because the blob detection still applies to the whole image. Is there any way to create a new image completely independent of the whole image prior to setting the ROI?

Thanks.

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000