非极大值抑制(nms)算法功能及python实现

    有什么比一张图更能说明问题呢。nms广泛应用于边缘检测,人脸检测和目标检测等,用于消除冗余的框。也有专门研究它的论文。如下就Faster-RCNN_TF中nms的python源码进行注释。

图1 nms的一个功能示意图,图片来源
def nms(dets, thresh):
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    scores = dets[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1) #所有box面积
    print "all box aress: ", areas
    order = scores.argsort()[::-1] #降序排列得到scores的坐标索引

    keep = []
    while order.size > 0:
        i = order[0] #最大得分box的坐标索引
        keep.append(i)
        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:]]) #最高得分的boax与其他box的公共部分(交集)

        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1) #求高和宽,并使数值合法化
        inter = w * h #其他所有box的面积
        ovr = inter / (areas[i] + areas[order[1:]] - inter)  #IOU:交并比

        inds = np.where(ovr <= thresh)[0] #ovr小表示两个box交集少,可能是另一个物体的框,故需要保留
        order = order[inds + 1]  #iou小于阈值的框

    return keep
  • 测试用例
import cv2
import numpy as np
import random

img=np.zeros((300,400), np.uint8)
dets=np.array([[83,54,165,163,0.8], [67,48,118,132,0.5], [91,38,192,171,0.6]], np.float)

img_cp=img.copy()
for box in dets.tolist():	#显示待测试框及置信度
    x1,y1,x2,y2,score=int(box[0]),int(box[1]),int(box[2]),int(box[3]),box[-1]
    y_text=int(random.uniform(y1, y2))
    cv2.rectangle(img_cp, (x1,y1), (x2, y2), (255, 255, 255), 2)
    cv2.putText(img_cp, str(score), (x2-30, y_text), 2,1, (255, 255, 0))
cv2.imshow("ori_img", img_cp)

rtn_box=nms(dets, 0.3)	#0.3为faster-rcnn中配置文件的默认值
cls_dets=dets[rtn_box, :]
print "nms box:", cls_dets

img_cp=img.copy()
for box in cls_dets.tolist():
    x1,y1,x2,y2,score=int(box[0]),int(box[1]),int(box[2]),int(box[3]),box[-1]
    y_text=int(random.uniform(y1, y2))
    cv2.rectangle(img_cp, (x1,y1), (x2, y2), (255, 255, 255), 2)
    cv2.putText(img_cp, str(score), (x2-30, y_text), 2,1, (255, 255, 0))

    输出图像为


参考文献:

  1. 更详尽的文章参看https://www.pyimagesearch.com/2014/11/17/non-maximum-suppression-object-detection-python/
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
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`即为经过极大值抑制后的最终输出框。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值