图像 ROI

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


图像 ROI - 微笑的艾米 - 微笑的艾米
 
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
2
3
4
5
6
7
8
9
10
11
12
13
14
/* 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, img2, NULL);
/* always reset the Region of Interest */
cvResetImageROI(img1);

2. Adding Two Images with Different Size

Listing 2: Adding two images with different size

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

3. Performing Template Matching in a Specific Area

Listing 3: Template Matching with Region of Interest defined

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
/* perform template matching */
cvMatchTemplate(img, tpl, res, CV_TM_SQDIFF);
/* find best matches location */
CvPoint    minloc, maxloc;
double    minval, maxval;
cvMinMaxLoc(res, &minval, &maxval, &minloc, &maxloc, 0);
/* draw rectangle */
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:

  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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 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);

  转自:http://blog.csdn.net/jackmacro/article/details/7026211  

  

posted @   微笑的艾米  阅读(1076)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示