非极大值抑制算法(NMS)及python实现

在目标检测中,常会利用非极大值抑制算法(NMS)对生成的大量候选框进行后处理,去除冗余的候选框,得到最具代表性的结果,以加快目标检测的效率。即如下图所示,消除多余的候选框,找到最佳的bbox。
这里写图片描述

NMS算法过程

根据候选框的类别分类概率做排序: A &lt; B &lt; C &lt; D &lt; E &lt; F A&lt;B&lt;C&lt;D&lt;E&lt;F A<B<C<D<E<F

  1. 先标记最大概率矩形框F是我们要保留下来的;
  2. 从最大概率矩形框F开始,分别判断A~E与F的重叠度IOU(两框的交并比)是否大于某个设定的阈值,假设B、D与F的重叠度超过阈值,那么就扔掉B、D;
  3. 从剩下的矩形框A、C、E中,选择概率最大的E,标记为要保留下来的,然后判读E与A、C的重叠度,扔掉重叠度超过设定阈值的矩形框

就这样一直重复下去,直到剩下的矩形框没有了,标记完所有要保留下来的矩形框

NMS算法的python实现

# python3
import numpy as np

def py_nms(dets, thresh):
    """Pure Python NMS baseline."""
    #x1、y1、x2、y2、以及score赋值
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    #每一个候选框的面积
    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    #order是按照score降序排序的
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        #计算当前概率最大矩形框与其他矩形框的相交框的坐标,会用到numpy的broadcast机制,得到的是向量
        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])

        #计算相交框的面积,注意矩形框不相交时w或h算出来会是负数,用0代替
        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        #计算重叠度IOU:重叠面积/(面积1+面积2-重叠面积)
        ovr = inter / (areas[i] + areas[order[1:]] - inter)

        #找到重叠度不高于阈值的矩形框索引
        inds = np.where(ovr <= thresh)[0]
        #将order序列更新,由于前面得到的矩形框索引要比矩形框在原order序列中的索引小1,所以要把这个1加回来
        order = order[inds + 1]
    return keep

# test
if __name__ == "__main__":
    dets = np.array([[30, 20, 230, 200, 1], 
                     [50, 50, 260, 220, 0.9],
                     [210, 30, 420, 5, 0.8],
                     [430, 280, 460, 360, 0.7]])
    thresh = 0.35
    keep_dets = py_nms(dets, thresh)
    print(keep_dets)
    print(dets[keep_dets])

测试结果:
在这里插入图片描述
只有第2个框与第1个框的IoU(0.38)超过了阈值,从结果看其被成功滤除掉了。NMS的大致原理和代码实现大致就是这样~

参考资料:

  1. 目标检测的几个名词
  2. faster rcnn 源码解读
  • 16
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Python中的非极大值抑制(Non-Maximum Suppression,简称NMS)是一种常用的目标检测算法,用于在重叠的候选框中选择最佳的目标框。它通常用于物体检测任务中,例如目标检测、人脸检测等。 NMS的基本思想是,在一组候选框中,首先选择具有最高置信度的框作为输出框,然后计算该框与其他候选框的重叠程度(如IoU),如果重叠程度高于一定阈值,则将该候选框剔除。这样可以确保输出的目标框之间没有太大的重叠,从而提高检测结果的准确性。 在Python中,可以使用以下步骤实现非极大值抑制: 1. 根据置信度对候选框进行排序,将置信度最高的框作为输出框。 2. 计算输出框与其他候选框的重叠程度(如IoU)。 3. 对于重叠程度高于设定阈值的候选框,将其从候选框列表中移除。 4. 重复步骤2和步骤3,直到所有候选框都被处理完毕。 5. 返回最终的输出框列表作为非极大值抑制的结果。 以下是一个简单的Python示例代码,演示了如何实现非极大值抑制: ```python def non_maximum_suppression(boxes, scores, threshold): # 根据置信度对候选框进行排序 sorted_indices = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True) selected_indices = [] while sorted_indices: # 选择置信度最高的框作为输出框 best_index = sorted_indices[0] selected_indices.append(best_index) # 计算输出框与其他候选框的重叠程度 best_box = boxes[best_index] other_indices = sorted_indices[1:] overlaps = [calculate_iou(best_box, boxes[i]) for i in other_indices] # 移除重叠程度高于阈值的候选框 filtered_indices = [i for i, overlap in zip(other_indices, overlaps) if overlap <= threshold] sorted_indices = filtered_indices return selected_indices # 计算两个框的重叠程度(IoU) def calculate_iou(box1, box2): # 计算两个框的相交区域 intersection = max(0, min(box1[2], box2[2]) - max(box1[0], box2[0])) * max(0, min(box1[3], box2[3]) - max(box1[1], box2[1])) # 计算两个框的并集区域 area1 = (box1[2] - box1[0]) * (box1[3] - box1[1]) area2 = (box2[2] - box2[0]) * (box2[3] - box2[1]) union = area1 + area2 - intersection # 计算IoU iou = intersection / union return iou # 示例用法 boxes = [[10, 10, 50, 50], [20, 20, 60, 60], [30, 30, 70, 70]] scores = [0.9, 0.8, 0.7] threshold = 0.5 selected_indices = non_maximum_suppression(boxes, scores, threshold) selected_boxes = [boxes[i] for i in selected_indices] print(selected_boxes) ``` 这段代码中,`boxes`表示候选框的坐标,`scores`表示候选框的置信度,`threshold`表示重叠程度的阈值。最后输出的`selected_boxes`即为经过非极大值抑制后的最终输出框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值