C++ 如何在 OpenCV 中设置 ROI?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8206466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set ROI in OpenCV?
提问by fdh
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. In C I could have used cvSetImageROI but this doesn't work on the C++ interface.
但是我使用的是 C++ 接口,所以我不知道如何做到这一点。在 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);
回答by Showpath
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.
我认为你有什么问题。如果第一个比另一个小,并且您想复制第一个图像中的第二个图像,则不需要 ROI。您可以调整第二张图像的大小并将其复制到第一个图像中。
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);
回答by Pieter-Jan
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.
我也写过这个,并在这里放了一些图片。