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
-
- IplImage *img1 = cvLoadImage("elvita.jpg", 1);
-
-
-
- cvSetImageROI(img1, cvRect(10, 15, 150, 250));
-
- Note that cvGetSize will return the width and the height of ROI */
- IplImage *img2 = cvCreateImage(cvGetSize(img1),
- img1->depth,
- img1->nChannels);
-
-
- cvCopy(img1, img2, NULL);
-
-
- cvResetImageROI(img1);
2. Adding Two Images with Different Size
Listing 2: Adding two images with different size
-
-
- IplImage *img1 = cvLoadImage("elvita.jpg", 1);
- IplImage *img2 = cvLoadImage("fifi.jpg", 1);
-
-
- CvRect rect = cvRect(25, 25, img2->width, img2->height);
-
-
- cvSetImageROI(img1, rect);
-
-
-
- cvAdd(img1, img2, img1, NULL);
-
-
- cvResetImageROI(img1);
3. Performing Template Matching in a Specific Area
Listing 3: Template Matching with Region of Interest defined
- IplImage *img = cvLoadImage("myphoto.jpg", 1);
- IplImage *tpl = cvLoadImage("eye.jpg", 1);
-
- CvRect rect = cvRect(25, 25, 120, 120);
-
- cvSetImageROI(img, rect);
-
- IplImage *res = cvCreateImage(cvSize(rect.width - tpl->width + 1,
- rect.height - tpl->height + 1),
- IPL_DEPTH_32F, 1);
-
-
- cvMatchTemplate(img, tpl, res, CV_TM_SQDIFF);
-
-
- CvPoint minloc, maxloc;
- double minval, maxval;
- cvMinMaxLoc(res, &minval, &maxval, &minloc, &maxloc, 0);
-
-
- cvRectangle(img,
- cvPoint(minloc.x, minloc.y),
- cvPoint(minloc.x + tpl->width, minloc.y + tpl->height),
- CV_RGB(255, 0, 0), 1, 0, 0 );
-
- 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:
- ROI segments object in an image.
- ROI respected by most of OpenCV functions.
- 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
-
-
-
- CvRect rect = cvRect(10, 20, 50, 60);
-
-
- IplImage* subimg;
-
-
- cvSetImageROI(img, rect);
- cvCopy(img, subimg, NULL);
- cvResetImageROI(img);
-
-
Or you can access the image directly using the ROI boundaries:
Listing 5: Accessing ROI pixels #2
-
-
-
- CvRect rect = cvRect(10, 20, 50, 60);
-
- cvSetImageROI(img, rect);
-
-
- 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);
原文:http://nashruddin.com/OpenCV_Region_of_Interest_%28ROI%29
(秩名) |