如何在 OpenCv 中轻松检测 2 个 ROI 是不是相交?
Posted
技术标签:
【中文标题】如何在 OpenCv 中轻松检测 2 个 ROI 是不是相交?【英文标题】:How can one easily detect whether 2 ROIs intersects in OpenCv?如何在 OpenCv 中轻松检测 2 个 ROI 是否相交? 【发布时间】:2012-01-03 14:55:10 【问题描述】:我正在尝试检测 2 个感兴趣区域 (CvRect
s) 在 OpenCV 中是否相互交叉。我显然可以手动输入几个(或者说很多)要检查的条件,但这并不是一个很好的方法(imo)。
谁能建议我任何其他解决方案? OpenCV中是否有现成的方法?
【问题讨论】:
【参考方案1】:我不知道有什么现成的C接口解决方案(CvRect
),但是如果你使用C++的方式(cv::Rect
),你可以轻松说
interesect = r1 & r2;
矩形操作的complete list是
// In addition to the class members, the following operations
// on rectangles are implemented:
// (shifting a rectangle by a certain offset)
// (expanding or shrinking a rectangle by a certain amount)
rect += point, rect -= point, rect += size, rect -= size (augmenting operations)
rect = rect1 & rect2 (rectangle intersection)
rect = rect1 | rect2 (minimum area rectangle containing rect2 and rect3 )
rect &= rect1, rect |= rect1 (and the corresponding augmenting operations)
rect == rect1, rect != rect1 (rectangle comparison)
【讨论】:
嗯...你知道如何将 CvRect 转换为 cv::Rect 吗?Rect r = myCvRect;
难吗?
我已经尝试了 & 运算符,但我只是得到一个编译错误:error: invalid use of member (did you forget the ‘&’ ?)
显然我有一个 &。【参考方案2】:
bool cv::overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi)
int x_tl = max(tl1.x, tl2.x);
int y_tl = max(tl1.y, tl2.y);
int x_br = min(tl1.x + sz1.width, tl2.x + sz2.width);
int y_br = min(tl1.y + sz1.height, tl2.y + sz2.height);
if (x_tl < x_br && y_tl < y_br)
roi = Rect(x_tl, y_tl, x_br - x_tl, y_br - y_tl);
return true;
return false;
是的。 OpenCV中有一个现成的方法,在opencv/modules/stitching/src/util.cpp
【讨论】:
以上是关于如何在 OpenCv 中轻松检测 2 个 ROI 是不是相交?的主要内容,如果未能解决你的问题,请参考以下文章