2013-07-02 52 views
0

我想检查两个CvRect *变量之间是否有任何部分重叠。 是否有内置的opencv函数来执行此检查。我正在编写opencv的c版本。检查两个CvRect *变量之间是否有重叠

+0

http://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of - 两个矩形在这里查找算法 –

回答

3

OpenCV没有为此的C API。 C++的方法很简单,就是r1 & r2。为&=在OpenCV源

template<typename _Tp> static inline Rect_<_Tp>& operator &= (Rect_<_Tp>& a, const Rect_<_Tp>& b) 
{ 
    _Tp x1 = std::max(a.x, b.x), y1 = std::max(a.y, b.y); 
    a.width = std::min(a.x + a.width, b.x + b.width) - x1; 
    a.height = std::min(a.y + a.height, b.y + b.height) - y1; 
    a.x = x1; a.y = y1; 
    if(a.width <= 0 || a.height <= 0) 
     a = Rect(); 
    return a; 
} 

因此,您只需将其翻译为C:

CvRect rect_intersect(CvRect a, CvRect b) 
{ 
    CvRect r; 
    r.x = (a.x > b.x) ? a.x : b.x; 
    r.y = (a.y > b.y) ? a.y : b.y; 
    r.width = (a.x + a.width < b.x + b.width) ? 
     a.x + a.width - r.x : b.x + b.width - r.x; 
    r.height = (a.y + a.height < b.y + b.height) ? 
     a.y + a.height - r.y : b.y + b.height - r.y; 
    if(r.width <= 0 || r.height <= 0) 
     r = cvRect(0, 0, 0, 0); 

    return r; 
} 
相关问题