寻找多个重叠矩形的并集 - OpenCV Python

11

我有几个包围同一物体的重叠边界框,但在某些地方它们仅略微重叠。作为一个整体,它们覆盖整个物体,但 openCV 的 groupRectangles 函数不返回完全包含物体的框。这里显示了我拥有的边界框为蓝色,我想要返回的边界框为红色。

我想获取仅重叠矩形的并集,但不确定如何遍历列表而不组合每个矩形。我已经展示了 union 和 intersect 函数,以及一个由 (x y w h) 表示的矩形列表,其中 x 和 y 是框左上角的坐标。

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)

目前我的合并函数如下:

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")

这已经完成了大部分工作,如下所示:

但是还有一些嵌套的边界框我不确定该如何继续迭代。


1
http://www.pyimagesearch.com/2014/11/17/non-maximum-suppression-object-detection-python - zindarod
“boxes”只是一个numpy数组吗?print(type(boxes)) - salparadise
@Zindarod,我之前试图使用它,但不幸的是它给出了类似groupRectangles的结果,即它返回一个小的“平均”边界框,不能覆盖我的整个对象。 - mechaddict
@salparadise boxes 是一个包含 x y w h 信息的数组,形式为 [[x1 y1 w1 h1],[x2 y2 w2 h2],...] 的数组。 - mechaddict
5个回答

3

我没有使用过openCV,因此该对象可能需要更多的处理,但是也许可以使用itertools.combinations来简化combine_boxes函数:

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')

编辑(实际上您可能需要使用zip

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

一切都一样。


我之前不熟悉itertools.combinations()函数,但实际上它看起来比我之前写的要好得多。我会尝试使用它,因为它似乎更整洁/更快。 - mechaddict
@mechaddict 再次查看您的问题后添加了 zip。 - salparadise

2
感谢您,salparadise(https://stackoverflow.com/users/62138/salparadise),非常有帮助,让我找到了一种解决方法。
但是解决方案似乎会将矩形重复添加到new_array中。例如,A B C彼此没有交集,则分别添加两次A B C。因此,new_array将包含A B A C B C。 请参考修改后的代码。希望它有所帮助。
已在多个测试用例上进行了测试,看起来工作正常。
    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

1

我遇到了类似的情况,在我的OpenCV项目中找到了每个帧中所有相交矩形的组合,经过一段时间的努力,我最终想在这里分享我的解决方案,希望能够帮助那些头痛于组合这些矩形的人。(这可能不是最好的解决方案,但它很简单)

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

虽然有点糟糕,但经过一番调整,我终于成功地得到了我想要的结果。

如果有人遇到类似的问题,我在下面附上了我的combine_boxes函数。

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

如果你需要一个单一的最大盒子,则得票最高的答案将不起作用,然而上面的代码将起作用,但它有一个 bug。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接