[ZT] ROI (region of interest) regions of interest opencv

Source: Internet
Author: User

When we previously introduced the iplimage structure, there was an important parameter-Roi. ROI is the region of interest. In fact, it is an instance of iplroi of the IPL/IPP (these two are inter libraries) structure. Iplroi includes xoffset, yoffset, height, width, and COI member variables. COI represents the channel of interest (Channel of interest ). The idea of ROI is: Once the ROI is set, a group of functions used for the entire image will only operate on the sub-images represented by the ROI. If COI is set to a non-zero value, the operation on the image only applies to the specified channel. The COI variable can be set to 1, 2, 3, and 4 channels, and the value 0 is reserved to make COI invalid (a bit like ignore ). However, many opencv functions ignore the COI parameter.

ROI plays an important role in actual work. In many cases, using it can improve the execution speed of Computer Vision code. This is because they allow operations on a part of the image, rather than the entire image. In opencv, Roi is generally supported, and function operations are limited to areas of interest. To set or cancel the ROI, use the cvsetimageroi () and cvresetimageroi () functions. If you set the ROI, you can use the cvsetimageroi () function and pass it an image pointer and a rectangle. To cancel the ROI, you only need to pass an image pointer for the cvresetimageroi () function.

12 Void cvsetimageroi (iplimage * image, cvrect rect); void cvresetimageroi (iplimage * image );

The following uses an example to describe how to use the ROI. In the example, we load an image and modify some regions-set the values of X, Y, width, and height of the ROI, and add an integer to the pixels in the ROI Area. The Code is as follows:

1234567891011121314151617181920212223242526272829 # Include <opencv2/highgui. HPP> # include <opencv2/imgproc. HPP> # include <stdio. h> using namespace CV; using namespace STD; int main (INT argc, char ** argv) {iplimage * SRC; src = cvloadimage ("E: \ opencv \ images \ cat.jpg "); int x = 90; int y = 110; int width = 180; int Height = 180; int add = 150; cvsetimageroi (SRC, cvrect (X, Y, width, height); cvadds (SRC, cvscalar (ADD), Src); cvresetimageroi (SRC); cvnamedwindow ("roi_add", 1 ); cvshowimage ("roi_add", Src); cvwaitkey (); Return 0 ;}

The above Code focuses the ROI on the face of a cat and increases its blue channel by 150. Final Effect

There are two points to note:

1. cvadds () function, which is used to add an array and a scalar element. In this example, the cvscalar () constructor sets the first component of cvscalar to the value represented by Add. The other values are 0 by default, and then add them to the elements in SRC. Why does the blue channel Increase by 150 instead of the red channel? The order of RGB Images in memory is bgra .... Therefore, the first channel is actually B (blue ). In fact, this function has the fourth parameter (the default value is null). For the meaning, see the opencv document.

2. The "cvresetimageroi (SRC);" code before the image is displayed must not be less. If this line of code is not available, the subsequent display of the image will only show the ROI Area. As we have already said, after the ROI region is set, the opencv function will only be effective for the ROI region. The program running result after the sentence is commented out is as follows:

[ZT] ROI (region of interest) regions of interest opencv

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.