In using OpenCV for detection tasks I keep running into the problem of merging overlapping bounding boxes; that is, essentially finding the bounding box around the union of two overlapping bounding boxes. This comes up a lot in object detection when, for some reason, the object of interest gets fractured into a number of bounding boxes, rather than one all-encompassing one.
There are a few answers on StackOverflow for algorithmic solutions and helpful external libraries (e.g. this, this, this), and there's also the groupRectangles
function provided by OpenCV (and the litany of associated questions/bugs: this, this, etc).
I seem to find that the aforementioned solutions are a bit overly complex for the task I'm trying to perform. A lot of the algorithmic solutions solve this problem from a mathematical perspective (more like a thought experiment), while doing operations like rect1 | rect2
can get really slow when the number of rectangles is high (it's O(N^2) to handle everything) and groupRectangles
has some quirks that make it only partially effective. So I came up with a somewhat hackish solution that actually works pretty effectively. I thought I'd share it below for anyone else who needs a quick fix for this common problem.
Feel free to comment and critique it.