Take the 2-minute tour ×

I have been testing background subtraction using gaussian state model. I am using opencv 2.1.0. I can generate binary image of foreground of the scene. Now all I want to do is Draw countour bounding rectangle to highlight the moving object. I have used cvCountourBoundingRect to obtain the rectangle covering countour. The issue I am facing is in case of multiple countour, sometime nearby rectangle overlaps. Here, can anyone suggest me to prevent overlapping of rectangle? In ideal case, two rectangle should not be overlapped. It rather should be draw a bigger rectagle which covers all two rectangles.

Any suggetion will be greatful.

share|improve this question
If you have a function, which checks if two rects are intersecting and one to generate a rect that encloses two or more given rects, you just have to find the overlapping ones in your list and merge them, or did I misunderstand your problem? –  Dobi Dec 12 '12 at 15:10
@Dobi you undestande my problem very well. The logic you have explained here is ideal logic. I am looking for smarter way to achieve the same result what I can achieve from the logic you have mentioned. The problem with this is I need to go through the iterations. I though if there is any better alternative. Thanks anyway. –  sam18 Dec 13 '12 at 4:30
add comment

1 Answer

up vote 1 down vote accepted

There's no ready possibility to do this in OpenCV. But actually the algorithm is very easy:

  1. Cycle through all rectangles and check if two rectangles overlap each other. This topic will be useful: Determine if two rectangles overlap each other?
  2. For every overlapped pair of rectangles create rectangle that contains both of them. To do this you should select one corner from first rectangle and another corner from second rectangle and these two corners will create rectangle for you. I don't think that it's a hard task - just simple math.
share|improve this answer
Thanks for the link. I will prefer e.James's answer from the link you mentioned. The approch of finding overlapping rectangle is the obvious logic. The basic problem with this implementation is I need to first store all countour bounding rect and then iterate N X N times to resolve rectangle overlapping issue. I thought if there is any better approch to handle this. I tried one more logic, called double countoring, in which I took a black image and then draw each contour bounding rect with -1 border size to fill it with white color and again find contour on that image. but not accurate. –  sam18 Dec 13 '12 at 4:40
Complexity is increased when one rectangle is overlapping multiple rectangle. Need to care to manage this scenario also. All I am thinking is to maintain two rectangles' collections. say, vector<CvRect> inRectColl and vector<CvRect> outRectColl. each rect from inRectColl need to be compared with outRectColl using the logic you have mentioned. outRectColl will grow when we found rectangle which is not overlapping to any CvRect from outRectColl. Overlapping rect will need to be treated specially to decide whether it will be mereged or will make new entry in outRectColl. –  sam18 Dec 13 '12 at 5:28
add comment

Your Answer


