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.

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.
Listing 1: Crop an object and save to new image
Listing 2: Adding two images with different size
Listing 3: Template Matching with Region of Interest defined
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:
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
Or you can access the image directly using the ROI boundaries:
Listing 5: Accessing ROI pixels #2
Learning OpenCV: Computer Vision with the OpenCV Library
By: Gary Bradski, Adrian Kaehler
This book is the "de facto" OpenCV User's Manual. It provides a practical, pragmatic, accessible book on computer vision, with algorithmic explanation and concrete example code snippets. Written by the creators of OpenCV, no doubt you should obtain a copy.
Machine Vision: Theory, Algorithms, Practicalities (Signal Processing and its Applications)
By: E. R. Davies
This book provides a solid and concrete foundation to computer vision from engineering point of view. Use Learning OpenCV from Gary Bradsky to learn how to use OpenCV, and use this book to understand how OpenCV works behind the screen.
momin on Mar 1, 2010:
Pinky on Mar 5, 2010:
Nash on Mar 5, 2010:
Skass on May 3, 2010:
Nash on May 4, 2010:
donny on May 10, 2010:
didi on Jul 2, 2010:
| Tel. | +62 31 8662872 +62 856 338 6017 |
| ICQ | 489571630 |
| Skype | dede_bl4ckheart |
| Yahoo | dede_bl4ckheart |
| nashruddin.amin |
Yi Xie on Nov 11, 2009: