2012-01-03 112 views
15

我試圖檢測2個感興趣區域(CvRect s)是否在OpenCV中相互交叉。我顯然可以手動輸入幾個(或更多)條件進行檢查,但這並不是一個好辦法(imo)。如何輕鬆檢測2個ROI是否與OpenCv相交?

任何人都可以建議我任何其他解決方案嗎? OpenCV中有沒有現成的方法?

回答

28

我不知道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) 
+0

嗯......你也許知道我該如何將CvRect轉換成cv :: Rect? – Patryk 2012-01-03 15:44:43

+0

'Rect r = myCvRect;'難麼? – Sam 2012-01-03 19:30:16

+0

我試過&運算符,我只是得到一個編譯錯誤:'錯誤:成員的無效使用(你忘了'&'?)'當我清楚我有一個&。 – achow 2012-02-23 21:03:16

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中使用。

相關問題