# 非极大值抑制(Non-Maximum Suppression)

## 3. 如何使用非极大值抑制

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

Python代码如下：

```#!/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 list
if len(bounding_boxes) == 0:
return [], []

# Bounding boxes
boxes = np.array(bounding_boxes)

# coordinates of bounding boxes
start_x = boxes[:, 0]
start_y = boxes[:, 1]
end_x = boxes[:, 2]
end_y = boxes[:, 3]

# Confidence scores of bounding boxes
score = np.array(confidence_score)

# Picked bounding boxes
picked_boxes = []
picked_score = []

# Compute areas of bounding boxes
areas = (end_x - start_x + 1) * (end_y - start_y + 1)

# Sort by confidence score of bounding boxes
order = np.argsort(score)

# Iterate bounding boxes
while order.size > 0:
# The index of largest confidence score
index = order[-1]

# Pick the bounding box with largest confidence score
picked_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-union
w = np.maximum(0.0, x2 - x1 + 1)
h = np.maximum(0.0, y2 - y1 + 1)
intersection = w * h

# Compute the ratio between intersection and union
ratio = 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)```

• 阈值为0.6
• 阈值为0.5
• 阈值为0.4

54 篇文章35 人订阅

0 条评论

## 相关文章

### 线性规划之单纯形法【超详解+图解】

1.作用     单纯形法是解决线性规划问题的一个有效的算法。线性规划就是在一组线性约束条件下，求解目标函数最优解的问题。 2.线性规划的一般形式     在约...

1.2K5

1867

853

### 每个AI程序员都应该知道的基础数论

-欢迎 这篇文章讨论了数论中每个程序员都应该知道的几个重要概念。本文的内容既不是对数论的入门介绍，也不是针对数论中任何特定算法的讨论，而只是想要做为数论的一篇参...

2017

### OpenCV角点检测源代码分析（Harris和ShiTomasi角点）

OpenCV中常用的角点检测为Harris角点和ShiTomasi角点。 以OpenCV源代码文件 .\opencv\sources\samples\cpp\t...

4636

792

### OpenCV 学习日记（三）--- 常见数据类型

OpenCV基本数据类型： CvPoint，这些结构中最简单的一个，包含两个整型变量x和y。 CvPoint还有两个兄弟：CvPoint2D32f 和 CvPo...

2070

2777

1581

2805