简体   繁体   中英

How to set ROI in OpenCV?

I have two images, the first one smaller than the other. I need to copy the second image on the first image. To do so, I need to set the ROI on the first one, copy the second image onto the first one and then reset the ROI.

However I am using the C++ interface so I have no idea how to do this. In CI could have used cvSetImageROI but this doesn't work on the C++ interface.

So basically whats the C++ alternative to cvSetImageROI?

//output is a pointer to the mat whom I want the second image (colourMiniBinMask) copied upon
Rect ROI (478, 359, 160, 120);

Mat imageROI (*output, ROI);

colourMiniBinMask.copyTo (imageROI);

imshow ("Gravity", *output);

I think you have something wrong. If the first one is smaller than the other one and you want to copy the second image in the first one, you don't need an ROI. You can just resize the second image in copy it into the first one.

However if you want to copy the first one in the second one, I think this code should work:

cv::Rect roi = cv::Rect((img2.cols - img1.cols)/2,(img2.rows - img1.rows)/2,img1.cols,img1.rows);

cv::Mat roiImg;
roiImg = img2(roi);

img1.copyTo(roiImg);

This is the code I used. I think the comments explain it.

/* ROI by creating mask for the parallelogram */
Mat mask = cvCreateMat(480, 640, CV_8UC1);
// Create black image with the same size as the original
for(int i=0; i<mask.cols; i++)
   for(int j=0; j<mask.rows; j++)
       mask.at<uchar>(Point(i,j)) = 0;

// Create Polygon from vertices
vector<Point> approxedRectangle;
approxPolyDP(rectangleVertices, approxedRectangle, 1.0, true);

// Fill polygon white
fillConvexPoly(mask, &approxedRectangle[0], approxedRectangle.size(), 255, 8, 0);                 

// Create new image for result storage
Mat imageDest = cvCreateMat(480, 640, CV_8UC3);

// Cut out ROI and store it in imageDest
image->copyTo(imageDest, mask);

I also wrote about this and put some pictures here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM