9
votes

I have several overlapping bounding boxes that encompass a single object, however they overlap minimally in some places. Taken as a whole, they encompass the entire object, but openCV's groupRectangles function does not return a box encompassing the object. The bounding boxes I have are shown in blue, and bounding boxes I would like to return are shown in red here

I would like to get the union of only the overlapping rectangles but am unsure about how to iterate through the list without combining every rectangle. I have union and intersect functions shown below, and a list of the rectangles represented by (x y w h), where x and y are the coordinates of the top left corner of the box.

def union(a,b):
  x = min(a[0], b[0])
  y = min(a[1], b[1])
  w = max(a[0]+a[2], b[0]+b[2]) - x
  h = max(a[1]+a[3], b[1]+b[3]) - y
  return (x, y, w, h)

def intersection(a,b):
  x = max(a[0], b[0])
  y = max(a[1], b[1])
  w = min(a[0]+a[2], b[0]+b[2]) - x
  h = min(a[1]+a[3], b[1]+b[3]) - y
  if w<0 or h<0: return () # or (0,0,0,0) ?
  return (x, y, w, h)

My function for combining is currently as follows:

def combine_boxes(boxes):
    noIntersect = False
    while noIntersect == False and len(boxes) > 1:
        a = boxes[0]
        print a
        listBoxes = boxes[1:]
        print listBoxes
        index = 0
        for b in listBoxes:
            if intersection(a, b):
                newBox = union(a,b)
                listBoxes[index] = newBox
                boxes = listBoxes
                noIntersect = False
                index = index + 1
                break
            noIntersect = True
            index = index + 1

    print boxes
    return boxes.astype("int")

This gets most of the way there, as shown here

there are still a few nested bounding boxes that I'm not sure how to continue iterating through.

5
is boxes just a numpy array? print(type(boxes))salparadise
@Zindarod, I was trying to use that previously, but unfortunately it gives a result similar to groupRectangles, in that it returns a small 'average' bounding box that doesn't cover my entire objectmechaddict
@salparadise boxes is an array of arrays containing the x y w h information, in the form [[x1 y1 w1 h1],[x2 y2 w2 h2],...]mechaddict

5 Answers

3
votes

I haven't worked with openCV, so the object may need more mangling, but maybe use itertools.combinations to make the combine_boxes function simpler:

import itertools
import numpy as np
def combine_boxes(boxes):
    new_array = []
    for boxa, boxb in itertools.combinations(boxes, 2):
        if intersection(boxa, boxb):
            new_array.append(union(boxa, boxb))
        else:
            new_array.append(boxa)
    return np.array(new_array).astype('int')

EDIT (you may actually need zip instead)

for boxa, boxb in zip(boxes, boxes[1:])

everything is the same.

2
votes

Thank you, salparadise (https://stackoverflow.com/users/62138/salparadise). Very helpful to find a way out.

But the solution looks rectangles could be repeated added into the new_array. e.g. A B C has no intersection to each other, A B C will be added twice respectively. So the new_array will contain A B A C B C. Please refer to the revised code. Hope it helps.

Had tested it on multiple test cases. It looks working fine.

    def merge_recs(rects):
        while (1):
            found = 0
            for ra, rb in itertools.combinations(rects, 2):
                if intersection(ra, rb):
                    if ra in rects:
                        rects.remove(ra)
                    if rb in rects:
                        rects.remove(rb)
                    rects.append((union(ra, rb)))
                    found = 1
                    break
            if found == 0:
                break

        return rects
0
votes

It's horribly janky, but after a bit of finagling I did manage to get the results I wanted

I have included my combine_boxes function below in case anyone is having a similar problem.

def combine_boxes(boxes):
     noIntersectLoop = False
     noIntersectMain = False
     posIndex = 0
     # keep looping until we have completed a full pass over each rectangle
     # and checked it does not overlap with any other rectangle
     while noIntersectMain == False:
         noIntersectMain = True
         posIndex = 0
         # start with the first rectangle in the list, once the first 
         # rectangle has been unioned with every other rectangle,
         # repeat for the second until done
         while posIndex < len(boxes):
             noIntersectLoop = False
            while noIntersectLoop == False and len(boxes) > 1:
                a = boxes[posIndex]
                listBoxes = np.delete(boxes, posIndex, 0)
                index = 0
                for b in listBoxes:
                    #if there is an intersection, the boxes overlap
                    if intersection(a, b): 
                        newBox = union(a,b)
                        listBoxes[index] = newBox
                        boxes = listBoxes
                        noIntersectLoop = False
                        noIntersectMain = False
                        index = index + 1
                        break
                    noIntersectLoop = True
                    index = index + 1
            posIndex = posIndex + 1

    return boxes.astype("int")
0
votes

I go into a similar situation to combine all the intersected rectangle found in each frame of my OpenCV project, after some time I finally come up with a solution and want to share it here for someone having a headache combining those rectangles. (This might not be the best solution but it's simple though)

import itertools

# my Rectangle = (x1, y1, x2, y2), a bit different from OP's x, y, w, h
def intersection(rectA, rectB): # check if rect A & B intersect
    a, b = rectA, rectB
    startX = max( min(a[0], a[2]), min(b[0], b[2]) )
    startY = max( min(a[1], a[3]), min(b[1], b[3]) )
    endX = min( max(a[0], a[2]), max(b[0], b[2]) )
    endY = min( max(a[1], a[3]), max(b[1], b[3]) )
    if startX < endX and startY < endY:
        return True
    else:
        return False

def combineRect(rectA, rectB): # create bounding box for rect A & B
    a, b = rectA, rectB
    startX = min( a[0], b[0] )
    startY = min( a[1], b[1] )
    endX = max( a[2], b[2] )
    endY = max( a[3], b[3] )
    return (startX, startY, endX, endY)

def checkIntersectAndCombine(rects):
    if rects is None:
        return None
    mainRects = rects
    noIntersect = False
    while noIntersect == False and len(mainRects) > 1:
        mainRects = list(set(mainRects))
        # get the unique list of rect, or the noIntersect will be 
        # always true if there are same rect in mainRects
        newRectsArray = []
        for rectA, rectB in itertools.combinations(mainRects, 2):
            newRect = []
            if intersection(rectA, rectB):
                newRect = combineRect(rectA, rectB)
                newRectsArray.append(newRect)
                noIntersect = False
                # delete the used rect from mainRects
                if rectA in mainRects:
                    mainRects.remove(rectA)
                if rectB in mainRects:
                    mainRects.remove(rectB)
        if len(newRectsArray) == 0:
            # if no newRect is created = no rect in mainRect intersect
            noIntersect = True
        else:
            # loop again the combined rect and those remaining rect in mainRects
            mainRects = mainRects + newRectsArray
    return mainRects

0
votes

The most voted answer will not work if you need a single maximum box, however the above one will work, but it has a bug. posting the correct code for someone

tImageZone = namedtuple('tImageZone', 'x y w h')

def merge_zone(z1, z2):
    if (z1.x == z2.x and z1.y == z2.y and z1.w == z2.w and z1.h == z2.h):
        return z1
    x = min(z1.x, z2.x)
    y = min(z1.y, z2.y)
    w = max(z1.x + z1.w, z2.x + z2.w) - x
    h = max(z1.y + z1.h, z2.y + z2.h) - y
    return tImageZone(x, y, w, h)

def is_zone_overlap(z1, z2):
    # If one rectangle is on left side of other
    if (z1.x > z2.x + z2.w or z1.x + z1.w < z2.x):
        return False
    # If one rectangle is above other
    if (z1.y > z2.y + z2.h or z1.y + z1.h < z2.y):
        return False
    return True


def combine_zones(zones):
    index = 0
    if zones is None: return zones
    while index < len(zones):
        no_Over_Lap = False
        while no_Over_Lap == False and len(zones) > 1 and index < len(zones):
            zone1 = zones[index]
            tmpZones = np.delete(zones, index, 0)
            tmpZones = [tImageZone(*a) for a in tmpZones]
            for i in range(0, len(tmpZones)):
                zone2 = tmpZones[i]
                if (is_zone_overlap(zone1, zone2)):
                    tmpZones[i] = merge_zone(zone1, zone2)
                    zones = tmpZones
                    no_Over_Lap = False
                    break
                no_Over_Lap = True
        index += 1
    return zones