Non-Maximum Suppression for Object Detection in Python
2016-01-16 06:24
716 查看
之前看很多的nms 解释都没看懂,看了代码,注释比较好,就懂了,原文有更多解释,更快实现.
想法是只留下不重合的框,过程是任何一框,与其它任何一筐,重合超过一个值,去掉它。
http://www.pyimagesearch.com/2014/11/17/non-maximum-suppression-object-detection-python/?utm_source=tuicool&utm_medium=referral
想法是只留下不重合的框,过程是任何一框,与其它任何一筐,重合超过一个值,去掉它。
http://www.pyimagesearch.com/2014/11/17/non-maximum-suppression-object-detection-python/?utm_source=tuicool&utm_medium=referral
# import the necessary packages import numpy as np # Felzenszwalb et al. def non_max_suppression_slow(boxes, overlapThresh): # if there are no boxes, return an empty list if len(boxes) == 0: return [] # 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) # import the necessary packages import numpy as np # Felzenszwalb et al. def non_max_suppression_slow(boxes, overlapThresh): # if there are no boxes, return an empty list if len(boxes) == 0: return [] # 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, add the index # value to the list of picked indexes, then initialize # the suppression list (i.e. indexes that will be deleted) # using the last index last = len(idxs) - 1 i = idxs[last] pick.append(i) suppress = [last] # import the necessary packages import numpy as np # Felzenszwalb et al. def non_max_suppression_slow(boxes, overlapThresh): # if there are no boxes, return an empty list if len(boxes) == 0: return [] # 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, add the index # value to the list of picked indexes, then initialize # the suppression list (i.e. indexes that will be deleted) # using the last index last = len(idxs) - 1 i = idxs[last] pick.append(i) suppress = [last] # loop over all indexes in the indexes list for pos in xrange(0, last): # grab the current index j = idxs[pos] # 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 = max(x1[i], x1[j]) yy1 = max(y1[i], y1[j]) xx2 = min(x2[i], x2[j]) yy2 = min(y2[i], y2[j]) # compute the width and height of the bounding box w = max(0, xx2 - xx1 + 1) h = max(0, yy2 - yy1 + 1) # compute the ratio of overlap between the computed # bounding box and the bounding box in the area list overlap = float(w * h) / area[j] # if there is sufficient overlap, suppress the # current bounding box if overlap > overlapThresh: suppress.append(pos) # delete all indexes from the index list that are in the # suppression list idxs = np.delete(idxs, suppress) # return only the bounding boxes that were picked return boxes[pick]
# import the necessary packages from pyimagesearch.nms import non_max_suppression_slow import numpy as np import cv2 # construct a list containing the images that will be examined # along with their respective bounding boxes images = [ <span style="white-space:pre"> </span>("images/audrey.jpg", np.array([ <span style="white-space:pre"> </span>(12, 84, 140, 212), <span style="white-space:pre"> </span>(24, 84, 152, 212), <span style="white-space:pre"> </span>(36, 84, 164, 212), <span style="white-space:pre"> </span>(12, 96, 140, 224), <span style="white-space:pre"> </span>(24, 96, 152, 224), <span style="white-space:pre"> </span>(24, 108, 152, 236)])), <span style="white-space:pre"> </span>("images/bksomels.jpg", np.array([ <span style="white-space:pre"> </span>(114, 60, 178, 124), <span style="white-space:pre"> </span>(120, 60, 184, 124), <span style="white-space:pre"> </span>(114, 66, 178, 130)])), <span style="white-space:pre"> </span>("images/gpripe.jpg", np.array([ <span style="white-space:pre"> </span>(12, 30, 76, 94), <span style="white-space:pre"> </span>(12, 36, 76, 100), <span style="white-space:pre"> </span>(72, 36, 200, 164), <span style="white-space:pre"> </span>(84, 48, 212, 176)]))] # loop over the images for (imagePath, boundingBoxes) in images: <span style="white-space:pre"> </span># load the image and clone it <span style="white-space:pre"> </span>print "[x] %d initial bounding boxes" % (len(boundingBoxes)) <span style="white-space:pre"> </span>image = cv2.imread(imagePath) <span style="white-space:pre"> </span>orig = image.copy() <span style="white-space:pre"> </span># loop over the bounding boxes for each image and draw them <span style="white-space:pre"> </span>for (startX, startY, endX, endY) in boundingBoxes: <span style="white-space:pre"> </span>cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 0, 255), 2) <span style="white-space:pre"> </span># perform non-maximum suppression on the bounding boxes <span style="white-space:pre"> </span>pick = non_max_suppression_slow(boundingBoxes, 0.3) <span style="white-space:pre"> </span>print "[x] after applying non-maximum, %d bounding boxes" % (len(pick)) <span style="white-space:pre"> </span># loop over the picked bounding boxes and draw them <span style="white-space:pre"> </span>for (startX, startY, endX, endY) in pick: <span style="white-space:pre"> </span>cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2) <span style="white-space:pre"> </span># display the images <span style="white-space:pre"> </span>cv2.imshow("Original", orig) <span style="white-space:pre"> </span>cv2.imshow("After NMS", image) <span style="white-space:pre"> </span>cv2.waitKey(0)
相关文章推荐
- Java不可变对象(Immutable Objects)
- Android笔记--对Service、Object、Contentprovider、泛型的理解
- Java提高学习之Object(5)
- Java提高学习之Object(4)
- Java提高学习之Object(3)
- Object-C--->Swift之(五)数组和字典
- Objective-C是动态运行时语言的个人理解
- hibernate class cast exception from object to ...
- Java提高学习之Object(2)
- Java提高学习之Object类详解(1)
- Java Object...可变长度的参数(Varargs)详解
- Objective-C属性特性
- Objective-C KVC和KVO的使用
- Swift 懒加载(lazy) 和 Objective-C 懒加载的区别
- 浅谈Scala 2.8的包对象(package object)
- Object-C--->Swift之(四)强大的Switch
- xjc-beyond sliding windows-Object Localization by Efficient Subwindow Search 论文的算法和知识点
- json字符串转换为JSONObject和JSONArray
- Qt QObject
- 在Swift中,如何像Objective-C定义可选接口?