In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you how Python merges overlapping rectangular boxes, I hope you have something to gain after reading this article, let's discuss it together!
Requirements:
IOU correlation in NMS is the selection of the largest or most credible frame to retain.
Now we are trying to merge overlapping boxes into one big box, so we cannot use the above box directly.
And OpenCV groupRanges in Python I really don't understand, and it will not overlap the box directly deleted.
Principle:
Loop + recursion, in turn to determine whether there is overlap between the two boxes.
Effect:
Reference Code:
import cv2import numpy as npdef checkOverlap(boxa, boxb): x1, y1, w1, h2 = boxa x2, y2, w2, h3 = boxb if (x1 > x2 + w2): return 0 if (y1 > y2 + h3): return 0 if (x1 + w1
< x2): return 0 if (y1 + h2 < y2): return 0 colInt = abs(min(x1 + w1, x2 + w2) - max(x1, x2)) rowInt = abs(min(y1 + h2, y2 + h3) - max(y1, y2)) overlap_area = colInt * rowInt area1 = w1 * h2 area2 = w2 * h3 return overlap_area / (area1 + area2 - overlap_area)def unionBox(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 intersectionBox(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 () return [x, y, w, h]def rectMerge_sxf(rects: []): # rects =>[[x1, y1, w1, h2], [x2, y2, w2, h3], ...] ''' When rects coordinates are found by connectedComponentsWithStats, Note that the first two coordinates represent the entire graph and need to be removed, otherwise there will be only one large box, Before executing this function, do something similar to the following. rectList = sorted(rectList)[2:] ''' rectList = rects.copy() rectList.sort() new_array = [] complete = 1 #Use while, not forEach, because rectList content will change i = 0 while i < len(rectList): #Select the back, the front has been judged, do not need to repeat the operation j = i + 1 succees_once = 0 while j < len(rectList): boxa = rectList[i] boxb = rectList[j] #Determine whether there is overlap, pay attention to only horizontal + vertical cases, angular rotation is not allowed if checkOverlap(boxa, boxb): # intersectionBox(boxa, boxb) complete = 0 #Add the merged matrix to the candidate area new_array.append(unionBox(boxa, boxb)) succees_once = 1 #removed from the original list, because the two have been merged, not deleted will lead to double counting rectList.remove(boxa) rectList.remove(boxb) break j += 1 if succees_once: #Successfully merged once, in this case i does not need +1, because the above operation removes (boxb) continue i += 1 #The rest is non-overlapping, just add it directly new_array.extend(rectList) # 0: There may be unmerged, recursive call; #1: There is no merger item this time, which means that all are separated and can be terminated and exited. if complete == 0: complete, new_array = rectMerge_sxf(new_array) return complete, new_arraybox = [[20, 20, 20, 20], [100, 100, 100, 100], [60, 60, 50, 50], [50, 50, 50, 50]]_, res = rectMerge_sxf(box)print(res)print(box)img = np.ones([256, 256, 3], np.uint8)for x,y,w,h in box: img = cv2.rectangle(img, (x,y), (x+w,y+h), (0, 255, 0), 2)cv2.imshow('origin', img)img = np.ones([256, 256, 3], np.uint8)for x,y,w,h in res: img = cv2.rectangle(img, (x,y), (x+w,y+h), (0, 0, 255), 2)cv2.imshow ('after ', img)cv2.waitKey(0) After reading this article, I believe you have a certain understanding of "Python how to merge overlapping rectangular boxes". If you want to know more about it, please pay attention to the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.