组合重叠矩形(python)

时间:2016-06-16 00:25:22

标签: python-2.7 opencv opencv3.0

经过研究,我遇到了几个与此类似的问题:OpenCV groupRectangles - getting grouped and ungrouped rectangles(大多数都是用c ++编写的)。但是,它们都不是固体。我想将重叠的矩形组合成一个矩形。 Image

我的进步:

for cnt in large_contours:
    x,y,w,h = cv2.boundingRect(cnt)
    mec=x,y,w,h
    rectVec=cv2.rectangle(img_and_contours,(x,y),(x+w,y+h),(0,255,0),2)
    #cv2.rectangle(img_and_contours, cv2.boundingRect(large_contours[cnt]),(0,255,0));
    rectList, weights = cv2.groupRectangles(mec, 3,0.2)

我只发布了一段代码。我希望groupRectangle可以做我想做的事,但什么也没做,而是给我一个错误

  

rectList,weights = cv2.groupRectangles(mec,3,0.2)   TypeError:rectList   块引用

2 个答案:

答案 0 :(得分:0)

有一种名为**Non max suppression**的算法。该函数将矩形数组作为输入,并输出最大矩形。这是代码:

def non_max_suppression_fast(boxes, overlapThresh):
   # if there are no boxes, return an empty list
   if len(boxes) == 0:
      return []

   # if the bounding boxes integers, convert them to floats --
   # this is important since we'll be doing a bunch of divisions
   if boxes.dtype.kind == "i":
      boxes = boxes.astype("float")
#  
   # initialize the list of picked indexes   
   pick = []

   # grab the coordinates of the bounding boxes
   x1 = boxes[:,0]
   y1 = boxes[:,1]
   x2 = boxes[:,2]
   y2 = boxes[:,3]

   # compute the area of the bounding boxes and sort the bounding
   # boxes by the bottom-right y-coordinate of the bounding box
   area = (x2 - x1 + 1) * (y2 - y1 + 1)
   idxs = np.argsort(y2)

   # keep looping while some indexes still remain in the indexes
   # list
   while len(idxs) > 0:
      # grab the last index in the indexes list and add the
      # index value to the list of picked indexes
      last = len(idxs) - 1
      i = idxs[last]
      pick.append(i)

      # find the largest (x, y) coordinates for the start of
      # the bounding box and the smallest (x, y) coordinates
      # for the end of the bounding box
      xx1 = np.maximum(x1[i], x1[idxs[:last]])
      yy1 = np.maximum(y1[i], y1[idxs[:last]])
      xx2 = np.minimum(x2[i], x2[idxs[:last]])
      yy2 = np.minimum(y2[i], y2[idxs[:last]])

      # compute the width and height of the bounding box
      w = np.maximum(0, xx2 - xx1 + 1)
      h = np.maximum(0, yy2 - yy1 + 1)

      # compute the ratio of overlap
      overlap = (w * h) / area[idxs[:last]]

      # delete all indexes from the index list that have
      idxs = np.delete(idxs, np.concatenate(([last],
         np.where(overlap > overlapThresh)[0])))

   # return only the bounding boxes that were picked using the
   # integer data type
   return boxes[pick].astype("int")

希望它可以帮到你。

答案 1 :(得分:0)

这是对我有用的代码

def merge_overlapping_zones(zones,delta_overpap = 30):

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]

            # check left side broken
            if zone2.x >= delta_overpap and zone2.y >= delta_overpap:
                t = tImageZone(zone2.x - delta_overpap, zone2.y - delta_overpap, zone2.w + 2 * delta_overpap,
                               zone2.h + 2 * delta_overpap)
            elif zone2.x >= delta_overpap:
                t = tImageZone(zone2.x - delta_overpap, zone2.y, zone2.w + 2 * delta_overpap,
                               zone2.h + 2 * delta_overpap)
            else:
                t = tImageZone(zone2.x, zone2.y - delta_overpap, zone2.w + 2 * delta_overpap,
                               zone2.h + 2 * delta_overpap)

            if (is_zone_overlap(zone1, t) or 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

`