C++ 了解 openCV 2.4 中的兴趣区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12705817/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:33:12  来源:igfitidea点击:

Understanding region of interest in openCV 2.4

c++opencv

提问by StuckInPhD

I know that in OpenCV 2.1 we had a function to set ROI: cvSetImageROI(), but such a function does not exist in 2.4 (or at least I cant find it in its manuals and help section.)

我知道在 OpenCV 2.1 中我们有一个函数来设置 ROI:cvSetImageROI(),但是这样的函数在 2.4 中不存在(或者至少我在它的手册和帮助部分找不到它。)

however here is the only helpful code I could find which uses opencv 2.4 for mage ROI, but I am having trouble understanding it:

然而,这是我能找到的唯一有用的代码,它使用 opencv 2.4 进行法师 ROI,但我无法理解它:

// define image ROI
cv::Mat imageROI;
imageROI= image(cv::Rect(385,270,logo.cols,logo.rows));
// add logo to image 
cv::addWeighted(imageROI,1.0,logo,0.3,0.,imageROI);

Here they want to add a very small log to a big image at the bottom right of the original image.

在这里,他们想在原始图像右下角的大图像中添加一个非常小的日志。

So what I understand from here is that another matrix is created to hold the ROI. Its dimensions given using the rect function, and size is given equal to that of the small logo they want to add.

所以我从这里了解到的是,创建了另一个矩阵来保存 ROI。它的尺寸使用 rect 函数给出,大小等于他们想要添加的小徽标的尺寸。

Then thsi is what confuses me: cv::addWeighted(imageROI,1.0,logo,0.3,0.,imageROI);here the source 1 of addWeighted is the ROI dimensions set, source 2 is the logo and the destination is also the ROI dimensions set. Is this correct? or am I missing something?

然后这让我感到困惑:cv::addWeighted(imageROI,1.0,logo,0.3,0.,imageROI);这里 addWeighted 的源 1 是 ROI 维度集,源 2 是徽标,目标也是 ROI 维度集。这样对吗?或者我错过了什么?

After this the result is shown with the logo added to the big image. Where in these commands was the big image included.

在此之后,结果显示为带有添加到大图像的徽标。这些命令中包含大图像的位置。

Also before asking here I wanted to try the code myself to maybe help clarify the situation. but I get this error, as the image() is not recognized: 'image': identifier not found

同样在问这里之前,我想自己尝试代码以帮助澄清情况。但我收到此错误,因为无法识别 image():'image': identifier not found

int _tmain(int argc, _TCHAR* argv[])
{
Mat src1, imageROI, logo;

logo = imread("c:\car1.jpg", -1);

imageROI= image(Rect(385,270,logo.cols,logo.rows));

addWeighted(imageROI,1.0,logo,0.3,0.,imageROI);


namedWindow("meh", CV_WINDOW_AUTOSIZE);
imshow("meh", imageROI);
waitKey(0);


return 0;

}

}

回答by remi

cv::Mat imageROI;
imageROI= image(cv::Rect(385,270,logo.cols,logo.rows));

The cv::Mat constructor wich takes a rectangle as a parameter:

cv::Mat 构造函数以矩形为参数:

Mat::Mat(const Mat& m, const Rect& roi)

returns a matrix that is pointing to the ROI of the original image, located at the place specified by the rectangle. so imageROI is really the Region of Interest (or subimage/submatrix) of the original image "image". If you modify imageROI it will consequently modify the original, larger matrix.

返回一个矩阵,该矩阵指向原始图像的 ROI,位于矩形指定的位置。所以 imageROI 实际上是原始图像“图像”的感兴趣区域(或子图像/子矩阵)。如果您修改 imageROI,它将因此修改原始的、更大的矩阵。

As for your example, the problem is that you are calling the constructor from an object which does not exists (image). You should replace:

至于你的例子,问题是你从一个不存在的对象(图像)调用构造函数。你应该更换:

imageROI= image(Rect(385,270,logo.cols,logo.rows));

by:

经过:

imageROI= src1(Rect(385,270,logo.cols,logo.rows));

assuming that src1 is your "big image" that you want to insert the logo into (the logo being car1.jpg). You should not forget to first read your big image, by the way!

假设 src1 是您要插入徽标的“大图像”(徽标为 car1.jpg)。顺便说一句,您不应该忘记先阅读您的大图像!