非极大抑制睔PYTHON实现

非极大抑制(Non-maximum suppression)python代码实现
原创Butertfly 发布于2018-11-20 18:48:57 阅读数 293 收藏
展开
定位一个物体,最后算法就找出了一堆的方框,我们需要判别哪些矩形框是没用的。非极大值抑制:先假设有6个矩形框,根据分类器类别分类概率做排序,从大到小分别属于物体的概率分别为A、B、C、D、E、F。

(1)从最大概率矩形框F开始,分别判断B~F与A的重叠度IOU是否大于某个设定的阈值;

(2)假设B、D与F的重叠度超过阈值,那么就扔掉B、D;并标记第一个矩形框A,是我们保留下来的。

(3)从剩下的矩形框C、E、F中,选择概率最大的C,然后判断C与E、F的重叠度,重叠度大于一定的阈值,那么就扔掉;并标记C是我们保留下来的第二个矩形框。

就这样一直重复,找到所有被保留下来的矩形框。
 

# import the necessary packages
import numpy as np


# Felzenszwalb et al.
def non_max_suppression_slow(boxes, thresh):
# 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]
scores = boxes[:, 4]

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

# 获取置信度,并降序排列,获取其在boxes中对应的索引
idxs = scores.argsort()[::-1]

while idxs.size > 0:
i = idxs[0]
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[1:]])
yy1 = np.maximum(y1[i], y1[idxs[1:]])
xx2 = np.minimum(x2[i], x2[idxs[1:]])
yy2 = np.minimum(y2[i], y2[idxs[1:]])

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

# compute the intersection over union(IOU) between the computed
# bounding box and the bounding box in the area list
inter = w * h
union = area[i] + area[idxs[1:]] - inter
IOU = inter / union

indexes = np.where(IOU < thresh)

idxs = idxs[indexes + 1]

# return only the bounding boxes that were picked
return boxes[pick]
 

References:

1.https://www.pyimagesearch.com/2014/11/17/non-maximum-suppression-object-detection-python/

2.https://blog.csdn.net/l_ml_m_lm_m/article/details/79881437

3.https://blog.csdn.net/u011534057/article/details/51235718

4.http://www.cnblogs.com/makefile/p/nms.html


————————————————
版权声明:本文为CSDN博主「Butertfly」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Butertfly/article/details/84307659

上一篇:《超强合集:OCR 文本检测干货汇总(含论文、源码、demo 等资源)》


下一篇:YOLOv3源码阅读之二:get_kmeans.py