![](/img/trans.png)
[英]Finding the area of intersection of multiple overlapping rectangles in Python
[英]Finding the union of multiple overlapping rectangles - OpenCV python
我有幾個重疊的邊界框,它們包含一個對象,但是它們在某些地方的重疊最小。 作為一個整體,它們包含整個對象,但 openCV 的 groupRectangles 函數不返回包含對象的框。 我的邊界框顯示為藍色,我想返回的邊界框顯示為紅色
我只想獲得重疊矩形的聯合,但不確定如何在不組合每個矩形的情況下遍歷列表。 我有如下所示的 union 和 intersect 函數,以及由 (xywh) 表示的矩形列表,其中 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")
這得到了大部分的方式,如下所示
還有一些嵌套的邊界框我不確定如何繼續迭代。
我沒有使用過 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:])
一切都是一樣的。
謝謝你,salparadise( https://stackoverflow.com/users/62138/salparadise )。 非常有助於找到出路。
但解決方案看起來矩形可以重復添加到 new_array 中。 例如,ABC 之間沒有交集,ABC 將分別添加兩次。 所以new_array 將包含ABACB 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
這太糟糕了,但經過一番折騰,我確實設法得到了我想要的結果
我在下面包含了我的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")
如果您需要一個最大框,則投票最多的答案將不起作用,但是上面的答案會起作用,但它有一個錯誤。 為某人發布正確的代碼
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
我遇到了類似的情況,將我的 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.