【问题标题】:How can one easily detect whether 2 ROIs intersects in OpenCv?如何在 OpenCv 中轻松检测 2 个 ROI 是否相交?
【发布时间】:2012-01-03 14:55:10
【问题描述】:

我正在尝试检测 2 个感兴趣区域 (CvRects) 在 OpenCV 中是否相互交叉。我显然可以手动输入几个(或者说很多)要检查的条件,但这并不是一个很好的方法(imo)。

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

【问题讨论】:

    标签: c++ opencv roi


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多