简体   繁体   English

如何在OpenCV中设置ROI?

[英]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. 为此,我需要在第一个图像上设置ROI,将第二个图像复制到第一个图像上,然后重置ROI。

However I am using the C++ interface so I have no idea how to do this. 但是我正在使用C ++接口,所以我不知道该怎么做。 In CI could have used cvSetImageROI but this doesn't work on the C++ interface. 在CI中,本可以使用cvSetImageROI,但这在C ++接口上不起作用。

So basically whats the C++ alternative to cvSetImageROI? 那么,基本上什么是cvSetImageROI的C ++替代品?

//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. 如果第一个图像小于另一个图像,并且您想在第一个图像中复制第二个图像,则不需要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 . 我也写了这个,并在这里放了一些图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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