Home >Backend Development >C++ >How to use Region of Interest (ROI) in OpenCV using C++?

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

WBOY
WBOYforward
2023-08-27 11:37:051541browse

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;
}

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete