如何轻松检测2个ROI是否与OpenCv相交?

Pat*_*ryk 16 c++ opencv roi

我试图检测2个感兴趣的区域是否CvRect在OpenCV中相互交叉.我显然可以手动键入几个(或更确切地说是很多)条件来检查,但这不是一个很好的方法(imo).

谁能建议我任何其他解决方案?OpenCV中有一个现成的方法吗?

Sam*_*Sam 31

我不知道C接口(CvRect)的任何现成解决方案,但如果你使用C++方式(cv::Rect),你可以很容易地说

interesect  = r1 & r2;
Run Code Online (Sandbox Code Playgroud)

矩形的完整操作列表

// 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)
Run Code Online (Sandbox Code Playgroud)


rkd*_*ari 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;
}
Run Code Online (Sandbox Code Playgroud)

是.OpenCV中有一个ready方法,用于opencv/modules/stitching/src/util.cpp中的方法