Home > Backend Development > C++ > body text

How to use Region of Interest (ROI) in OpenCV using C++?

WBOY
Release: 2023-08-27 11:37:05
forward
1520 people have browsed it

To isolate a specific part from an image, we must first find the area. then we The area must be copied from the main image to another matrix. This is how ROI works OpenCV works.

In this example, two matrices are declared at the beginning. Afterwards, an image named 'image_name.jpg' is loaded into the 'image1' matrix. The next line 'image2=image1 (Rect(100, 100, 120, 120));' requires special attention. This line of code crops out the defined area of ​​the image and stores it in the 'image2' matrix.

How to use Region of Interest (ROI) in OpenCV using C++?

Illustration of what we do using the 'Rect(100,100,120,120)' code The basic form of this line of code is 'Rect(x, y,x1,y1)'. Here, x and y represent the starting point of the rectangle, and x1 and y1 represent the end point of the rectangle. By changing these values, we can change the size of the rectangle.

Example

The following program demonstrates how region of interest works in OpenCV:

#include
#include
#include
using namespace std;
using namespace cv;
int main() {
   Mat image1; //Declaring a matrix named 'image1'//
   Mat image2; //Declaring a matrix named 'image2'//
   image1 = imread("RGB.png"); //Loading an image name 'image_name.png into image1 matrix//
   image2 = image1(Rect(100, 100, 120, 120)); //imposing a rectangle on
   image1//
   namedWindow("Image_Window1"); //Declaring an window to show actual image//
   namedWindow("Image_Window2"); //Declaring an window to show ROI//
   imshow("Image_Window1", image1); //Showing actual image//
   imshow("Image_Window2", image2);
   waitKey(0);
   return 0;
}
Copy after login

OutputHow to use Region of Interest (ROI) in OpenCV using C++?

The above is the detailed content of How to use Region of Interest (ROI) in OpenCV using C++?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template