I generate a lot of bounding box in an image. How can I merge the overlapping bounding box in an image?
For example,
_________________ ________________________
| | | |
| —————————|———— | |
| | | | | |
———————|—————————— | ——> | |
| | | |
| ————————|—————— | |
—————|———————— | | |
| | | |
| | | |
———————————————— ________________________
I know use the rectangle 1 | rectangle 2
to generate a new rectangle.
It can detected and merged them in the approach as follow.(From Efficient way to combine intersecting bounding rectangles)
if((rect1 & rect2) == rect1) ... // rect1 is completely inside rect2; do nothing.
else if((rect1 & rect2).area() > 0) // they intersect; merge them.
newrect = rect1 | rect2;
... // remove rect1 and rect2 from list and insert newrect.
But I mean how to judge which rectangle are overlapped when three or four rectangles overlapped. I though I could use the overlapping area to judge whether they are overlapped.
Is that anything else efficient approach? thank you so much.