2

After researching, I came across few questions similar to this:OpenCV groupRectangles - getting grouped and ungrouped rectangles (most are in c++). However, none of them are solid. I want to combine the overlapping rectangles into a single one. Image

My progress:

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)

I only posted piece of my code.I was hoping groupRectangle would do what I wanted, but did nothing and instead gives me an error

rectList,weights = cv2.groupRectangles(mec,3,0.2) TypeError: rectList Blockquote

Community
  • 1
  • 1
skyrocket
  • 41
  • 1
  • 5

2 Answers2

0

Here is the piece of code which worked for me

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

`

senthil
  • 21
  • 4
-1

There is an algorithm called **Non max suppression**. The function takes the rectangle array as input, and output the maximum rectangle. Here is the code (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")

Hope it can help you.

eric
  • 7,142
  • 12
  • 72
  • 138
VICTOR
  • 1,894
  • 5
  • 25
  • 54
  • thanks for your comment. Suppose, I've an array [[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 coordinates], how would I call the non_max_suppression_fast function? – skyrocket Jun 20 '16 at 15:41
  • @skyrocket Convert it to a list – VICTOR Jun 21 '16 at 10:19
  • Im getting TypeError: non_max_suppression_fast() takes 2 positional arguments but 3 were given –  Jul 23 '17 at 10:23
  • i used it like pick = self.non_max_suppression_fast(bound_rect, 0.3) –  Jul 23 '17 at 10:24