【问题标题】:Combine overlapping rectangles (python)合并重叠的矩形(python)
【发布时间】:2016-10-17 07:20:15
【问题描述】:

经过研究,我遇到了几个类似这样的问题: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) 类型错误:rectList 块引用

【问题讨论】:

    标签: python-2.7 opencv opencv3.0


    【解决方案1】:

    有一种算法叫做**Non max suppression**。该函数以矩形数组为输入,输出最大矩形。这是代码(from pyimagesearch):

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

    希望对你有帮助。

    【讨论】:

    • 感谢您的评论。假设,我有一个数组 [[351, 544, 9, 5],[514, 540, 8, 6],[467, 539, 8, 7],[409, 538, 13, 11],[201 , 538, 17, 8],[64, 538, 15, 11],[314, 537, 23, 10], [398, 534, 3, 9].... 256个坐标],我怎么称呼non_max_suppression_fast 函数?
    • @skyrocket 将其转换为列表
    • 我收到 TypeError:non_max_suppression_fast() 需要 2 个位置参数,但给出了 3 个
    • 我像pick = self.non_max_suppression_fast(bound_rect, 0.3)一样使用它
    【解决方案2】:

    这是对我有用的一段代码

    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
    

    `

    【讨论】:

    • 这里的 tImageZone 是什么?
    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 2014-05-13
    • 2023-04-01
    相关资源
    最近更新 更多