非极大值抑制(Non-Maximum Suppression)
创始人
2024-05-16 15:23:08
0

文章目录

    • 一、什么是非极大值抑制
    • 二、为什么要用非极大值抑制
    • 三、 如何使用非极大值抑制
    • 四、代码段

一、什么是非极大值抑制

非极大值抑制,简称为NMS算法,英文为Non-Maximum Suppression。其思想是搜素局部最大值,抑制非极大值。NMS算法在不同应用中的具体实现不太一样,但思想是一样的。非极大值抑制,在计算机视觉任务中得到了广泛的应用,例如边缘检测、人脸检测、目标检测(DPM,YOLO,SSD,Faster R-CNN)等。


二、为什么要用非极大值抑制

以目标检测为例:目标检测的过程中在同一目标的位置上会产生大量的候选框,这些候选框相互之间可能会有重叠,此时我们需要利用非极大值抑制找到最佳的目标边界框,消除冗余的边界框。Demo如下图:
在这里插入图片描述

三、 如何使用非极大值抑制

前提:目标边界框列表及其对应的置信度得分列表,设定阈值,阈值用来删除重叠较大的边界框。
IoU:intersection-over-union,即两个边界框的交集部分除以它们的并集。

非极大值抑制的流程如下:

  • 根据置信度得分进行排序
  • 选择置信度最高的比边界框添加到最终输出列表中,将其从边界框列表中删除
  • 计算所有边界框的面积,计算置信度最高的边界框与其它候选框的IoU。
  • 删除IoU大于阈值的边界框
  • 重复上述过程,直至边界框列表为空。

在这里插入图片描述

其实本质上的思想内涵是在一个区域当中找到置信度(confidence score)最高的那个边界框,搜索这个区域中的局部最大值,抑制非极大值

四、代码段

#!/usr/bin/env python
# _*_ coding: utf-8 _*_import cv2
import numpy as np"""Non-max Suppression Algorithm@param list  Object candidate bounding boxes@param list  Confidence score of bounding boxes@param float IoU threshold@return Rest boxes after nms operation
"""
def nms(bounding_boxes, confidence_score, threshold):# If no bounding boxes, return empty listif len(bounding_boxes) == 0:return [], []# Bounding boxesboxes = np.array(bounding_boxes)# coordinates of bounding boxesstart_x = boxes[:, 0]start_y = boxes[:, 1]end_x = boxes[:, 2]end_y = boxes[:, 3]# Confidence scores of bounding boxesscore = np.array(confidence_score)# Picked bounding boxespicked_boxes = []picked_score = []# Compute areas of bounding boxesareas = (end_x - start_x + 1) * (end_y - start_y + 1)# Sort by confidence score of bounding boxesorder = np.argsort(score)# Iterate bounding boxeswhile order.size > 0:# The index of largest confidence scoreindex = order[-1]# Pick the bounding box with largest confidence scorepicked_boxes.append(bounding_boxes[index])picked_score.append(confidence_score[index])# Compute ordinates of intersection-over-union(IOU)x1 = np.maximum(start_x[index], start_x[order[:-1]])x2 = np.minimum(end_x[index], end_x[order[:-1]])y1 = np.maximum(start_y[index], start_y[order[:-1]])y2 = np.minimum(end_y[index], end_y[order[:-1]])# Compute areas of intersection-over-unionw = np.maximum(0.0, x2 - x1 + 1)h = np.maximum(0.0, y2 - y1 + 1)intersection = w * h# Compute the ratio between intersection and unionratio = intersection / (areas[index] + areas[order[:-1]] - intersection)left = np.where(ratio < threshold)order = order[left]return picked_boxes, picked_score# Image name
image_name = 'nms.jpg'# Bounding boxes
bounding_boxes = [(187, 82, 337, 317), (150, 67, 305, 282), (246, 121, 368, 304)]
confidence_score = [0.9, 0.75, 0.8]# Read image
image = cv2.imread(image_name)# Copy image as original
org = image.copy()# Draw parameters
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
thickness = 2# IoU threshold
threshold = 0.4# Draw bounding boxes and confidence score
for (start_x, start_y, end_x, end_y), confidence in zip(bounding_boxes, confidence_score):(w, h), baseline = cv2.getTextSize(str(confidence), font, font_scale, thickness)cv2.rectangle(org, (start_x, start_y - (2 * baseline + 5)), (start_x + w, start_y), (0, 255, 255), -1)cv2.rectangle(org, (start_x, start_y), (end_x, end_y), (0, 255, 255), 2)cv2.putText(org, str(confidence), (start_x, start_y), font, font_scale, (0, 0, 0), thickness)# Run non-max suppression algorithm
picked_boxes, picked_score = nms(bounding_boxes, confidence_score, threshold)# Draw bounding boxes and confidence score after non-maximum supression
for (start_x, start_y, end_x, end_y), confidence in zip(picked_boxes, picked_score):(w, h), baseline = cv2.getTextSize(str(confidence), font, font_scale, thickness)cv2.rectangle(image, (start_x, start_y - (2 * baseline + 5)), (start_x + w, start_y), (0, 255, 255), -1)cv2.rectangle(image, (start_x, start_y), (end_x, end_y), (0, 255, 255), 2)cv2.putText(image, str(confidence), (start_x, start_y), font, font_scale, (0, 0, 0), thickness)# Show image
cv2.imshow('Original', org)
cv2.imshow('NMS', image)
cv2.waitKey(0)

这是一个链接 非极大值抑制(Non-Maximum Suppression),很能给人启发。

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...