如何在OpenCv中轻松检测2个感兴趣区域(ROI)是否相交?

16

我正在尝试检测OpenCV中是否有两个感兴趣区域(CvRect)相交。 我可以显然手动输入需要检查的多个条件,但这并不是一个好方法(在我看来)。

有人能否建议我其他解决方案? 是否在OpenCV中有现成的方法可以实现?

2个回答

31

我不知道是否有现成的解决方案来处理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)

嗯...你知道如何将CvRect转换为cv :: Rect吗? - Patryk
Rect r = myCvRect; 这个难吗? - Sam
我尝试了&运算符,但是我只得到了一个编译错误:error: invalid use of member (did you forget the ‘&’ ?),明明我已经使用了&。 - achow

5
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中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接