2012-01-03 12 views
15

Estoy tratando de detectar si 2 Regiones de Interés (CvRect s) se cruzan entre sí en OpenCV. Obviamente, puedo escribir manualmente varias (o más bien, muchas) condiciones para verificar, pero esa no sería una buena manera de hacerlo (imo).¿Cómo se puede detectar fácilmente si 2 ROI se cruzan en OpenCv?

¿Alguien me puede sugerir alguna otra solución? ¿Hay un método listo en OpenCV para eso?

Respuesta

28

No sé de cualquier solución lista para la interfaz C (CvRect), pero si se utiliza la forma en C++ (cv::Rect), se puede decir fácilmente

interesect = r1 & r2; 

El complete list de las operaciones en rectángulos es

// 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

Hmmm ... ¿quizás sabes cómo puedo convertir CvRect en cv :: Rect? – Patryk

+0

'Rect r = myCvRect;' ¿Es difícil? – Sam

+0

He intentado el operador & y acabo de recibir un error de compilación: 'error: uso no válido del miembro (¿Olvidaste el '&'?)' Cuando claramente tengo un &. – 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; 
} 

Sí. Hay un método listo en OpenCV para eso en opencv/modules/stitching/src/util.cpp

Cuestiones relacionadas