Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cv::Rect has union / intersection operators:

union : rect1 | rect2

intersection: rect1 & rect2

here's an example:

Rect A(20,20,80,80); Rect B(60,60,60,60);

Rect C = A & B; Rect D = A | B;

cerr << "A" << A << endl; cerr << "B" << B << endl; cerr << "C" << C << endl; cerr << "D" << D << endl;

Mat draw(200,200,CV_8UC3,Scalar::all(0)); rectangle(draw,A,Scalar(200,0,0),2); rectangle(draw,B,Scalar(200,0,0),2); rectangle(draw,C,Scalar(0,0,200),1); rectangle(draw,D,Scalar(0,200,0),1);

A[80 x 80 from (20, 20)] B[60 x 60 from (60, 60)] C[40 x 40 from (60, 60)] D[100 x 100 from (20, 20)]

cv::Rect has union / intersection operators:

union : rect1 | rect2

intersection: rect1 & rect2

here's an example:

Rect A(20,20,80,80);
Rect B(60,60,60,60);

B(60,60,60,60);

Rect C = A & B; Rect D = A | B;

B;

cerr << "A" << A << endl; cerr << "B" << B << endl; cerr << "C" << C << endl; cerr << "D" << D << endl;

endl;

Mat draw(200,200,CV_8UC3,Scalar::all(0)); rectangle(draw,A,Scalar(200,0,0),2); rectangle(draw,B,Scalar(200,0,0),2); rectangle(draw,C,Scalar(0,0,200),1); rectangle(draw,D,Scalar(0,200,0),1);

rectangle(draw,D,Scalar(0,200,0),1);


A[80 x 80 from (20, 20)]
B[60 x 60 from (60, 60)]
C[40 x 40 from (60, 60)]
D[100 x 100 from (20, 20)]20)]

image description

cv::Rect has union / intersection operators:

union : rect1 | rect2

intersection: rect1 & rect2

here's an example:

Rect A(20,20,80,80);
A(20,20,80,80); // blue
Rect B(60,60,60,60);
B(60,60,60,60); // blue

Rect C = A & B;
B;   // red
Rect D = A | B;
B;   // green

cerr << "A" << A << endl;
cerr << "B" << B << endl;
cerr << "C" << C << endl;
cerr << "D" << D << endl;

Mat draw(200,200,CV_8UC3,Scalar::all(0));
rectangle(draw,A,Scalar(200,0,0),2);
rectangle(draw,B,Scalar(200,0,0),2);
rectangle(draw,C,Scalar(0,0,200),1);
rectangle(draw,D,Scalar(0,200,0),1);

A[80 x 80 from (20, 20)]
B[60 x 60 from (60, 60)]
C[40 x 40 from (60, 60)]
D[100 x 100 from (20, 20)]

image description

cv::Rect has union / intersection operators:

union : rect1 | rect2

intersection: rect1 & rect2

here's an example:

Rect A(20,20,80,80); // blue
Rect B(60,60,60,60); // blue

Rect C = A & B;   // red
Rect D = A | B;   // green

cerr << "A" << A << endl;
cerr << "B" << B << endl;
cerr << "C" << C << endl;
cerr << "D" << D << endl;

Mat draw(200,200,CV_8UC3,Scalar::all(0));
rectangle(draw,A,Scalar(200,0,0),2);
rectangle(draw,B,Scalar(200,0,0),2);
rectangle(draw,C,Scalar(0,0,200),1);
rectangle(draw,D,Scalar(0,200,0),1);

A[80 x 80 from (20, 20)]
B[60 x 60 from (60, 60)]
C[40 x 40 from (60, 60)]
D[100 x 100 from (20, 20)]

image description

now, to find out, if 2 Rects overlap, just check the area of the intersection:

bool intersects = ((A & B).area() > 0);