我正在尝试检测 2 个感兴趣区域 ( CvRect
s) 在 OpenCV 中是否相互交叉。我显然可以手动输入几个(或者说很多)要检查的条件,但这并不是一个很好的方法(imo)。
谁能建议我任何其他解决方案?OpenCV中是否有现成的方法?
我不知道C接口有什么现成的解决方案(CvRect
),但是如果你使用C++方式(cv::Rect
),你可以很容易地说
interesect = r1 & r2;
矩形操作的完整列表是
// 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)
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