• 单阶段目标检测--NMS


    目录

    一、概念:

    二、算法过程

    三、代码实现


    一、概念:

            在目标检测的初始结果中,同一个物体,可能对应有多个边界框 (bounding box,bb),这些边界框通常相互重叠。如何从中选择一个最合适 的(也就是与真实目标框最接近的)呢?通常采用的做法是NMS(Nonmaximum suppression),即非极大值抑制。

    二、算法过程

             非极大值抑制的大体思路就像其名一样,对于多个边界框,以置信度 (class score)最大的那个框为准,其他与之重合度高的框,则认为它们检测 的是同一个物体,将其他框除掉,也就是抑制掉。给定一系列候选框,针对每 个类别,分别执行NMS,具体流程如下:

            1. 找到置信度最大的框,该框肯定是目标,得到第1个框;

            2. 依次计算其他相同类别框与第1个框的重合度(IOU值),如果大于一 定阈值,抑制掉;

            3. 剩下的框中,同样找置信度最大的框,为第2个框,抑制掉重合的框;

            4. 反复执行上述步骤,直到剩下最后一个框,此亦为目标框

    三、代码实现

    1. #需求
    2. #使用nms实现图像阈值参数的提取
    3. """
    4. 1导包
    5. 2,定义nms对象候选边框
    6.   2.1 边界框
    7.   2.2 边框坐标
    8.   2.3 边界框的置信度
    9.   2.4 选择边界框,   2.5 计算边界框的面积
    10. 3, 根据边界框置信度排序
    11.   3.1   迭代边界框
    12.   3.2 边界框信心指数得分最大
    13.   3.3 选择信心得分最大
    14. 4 ,计算相交过并并坐标(IOU)
    15.   4.1 计算交集-过并域
    16.   4.2 计算交集和并集之间的比率
    17. 5,图像设置--->名称
    18.   5.1 边界框
    19.   5.2读取图像文件
    20.   5.3复制图像作为原始
    21.   5.4参数设置
    22.   5.5IOU阈值
    23.   5.6绘制边界框和信心得分
    24.   5.7执行非max抑制算法
    25.   5.8绘制非最大值抑制后的边界框和置信度得分
    26.   5.9显示图像
    27. """
    28. import cv2
    29. import numpy as np
    30. """
    31.   Non-max Suppression Algorithm
    32.   @param list Object candidate bounding boxes
    33.   @param list Confidence score of bounding boxes
    34.   @param float IoU threshold
    35.   @return Rest boxes after nms operation
    36. """
    37. #定义nms对象候选边框
    38. def nms(bounding_boxes,confidence_srore,threashold): #定义nms
    39.   if len(bounding_boxes) == 0:
    40.       return [],[]
    41.   boxes = np.array(bounding_boxes)
    42.   # coordinates of bounding boxes 边框坐标
    43.   start_x = boxes[:,0]
    44.   start_y = boxes[:,1]   end_x = boxes[:,2]
    45.   end_y = boxes[:,3]
    46.   # Bounding boxes 边界框
    47.   boxes = np.array(bounding_boxes)
    48.   # Confidence scores of bounding boxes 边界框的置信度
    49.   score = np.array(confidence_score)
    50.   # Picked bounding boxes 选择边界框
    51.   picked_boxes = []
    52.   picked_score = []
    53.   # Compute areas of bounding boxes 计算边界框的面积
    54.   areas = (end_x - start_x + 1) * (end_y - start_y + 1)
    55.   # Sort by confidence score of bounding boxes 根据边界框置
    56. 信度排序
    57.   order = np.argsort(score)
    58.   # Iterate bounding boxes 迭代边界框
    59.   while order.size > 0:
    60.       # The index of largest confidence score 边界框信心指数
    61. 得分最大
    62.       index = order[-1] #未匹配到元素
    63.       # Pick the bounding box with largest confidence score
    64. 选择信心得分最大的区域
    65.       picked_boxes.append(bounding_boxes[index])
    66.       picked_score.append(confidence_score[index])
    67.       # Compute ordinates of intersection-over-union(IOU) 计
    68. 算相交过并并坐标(IOU)
    69.       x1 = np.maximum(start_x[index], start_x[order[:-1]])
    70.       x2 = np.minimum(end_x[index], end_x[order[:-1]])
    71.       y1 = np.maximum(start_y[index], start_y[order[:-1]])
    72.       y2 = np.minimum(end_y[index], end_y[order[:-1]])
    73.       # Compute areas of intersection-over-union 计算交集-过
    74. 并域
    75.       w = np.maximum(0.0, x2 - x1 + 1)
    76.       h = np.maximum(0.0, y2 - y1 + 1)
    77.       intersection = w * h       # Compute the ratio between intersection and union 计
    78. 算交集和并集之间的比率
    79.       ratio = intersection / (areas[index] +
    80. areas[order[:-1]] - intersection)
    81.       left = np.where(ratio < threshold)
    82.       order = order[left]
    83.   return picked_boxes, picked_score
    84. # Image name
    85. image_name = '1.png'
    86. # Bounding boxes 边界框
    87. bounding_boxes = [(187, 82, 337, 317), (150, 67, 305, 282),
    88. (246, 121, 368, 304)]
    89. confidence_score = [0.9, 0.75, 0.8]
    90. # Read image 读取图像文件
    91. image = cv2.imread(image_name)
    92. # Copy image as original 复制图像作为原始
    93. org = image.copy()
    94. # Draw parameters 参数设置
    95. font = cv2.FONT_HERSHEY_SIMPLEX
    96. font_scale = 1
    97. thickness = 2
    98. # IoU threshold IOU阈值
    99. threshold = 0.4
    100. # Draw bounding boxes and confidence score 绘制边界框和信心得分
    101. for (start_x, start_y, end_x, end_y), confidence in
    102. zip(bounding_boxes, confidence_score):
    103.   (w, h), baseline = cv2.getTextSize(str(confidence), font,
    104. font_scale, thickness)
    105.   cv2.rectangle(org, (start_x, start_y - (2 * baseline +
    106. 5)), (start_x + w, start_y), (0, 255, 255), -1)
    107.   cv2.rectangle(org, (start_x, start_y), (end_x, end_y),
    108. (0, 255, 255), 2) 第十一章, YOLO系列概述
    109. 1.深度学习经典检测方法
    110.  
    111. (1) tow-stage(两阶段):Faster-rcnn Mask-rcnn系列:增加了区域建议网
    112. 络(RPN),即预选框
    113. 特点
    114. 速度通常较慢(5FPS),但是效果通常不错
    115.   cv2.putText(org, str(confidence), (start_x, start_y),
    116. font, font_scale, (0, 0, 0), thickness)
    117. # Run non-max suppression algorithm 执行非max抑制算法
    118. picked_boxes, picked_score = nms(bounding_boxes,
    119. confidence_score, threshold)
    120. # Draw bounding boxes and confidence score after non-maximum
    121. supression 绘制非最大值抑制后的边界框和置信度得分
    122. for (start_x, start_y, end_x, end_y), confidence in
    123. zip(picked_boxes, picked_score):
    124.   (w, h), baseline = cv2.getTextSize(str(confidence), font,
    125. font_scale, thickness)
    126.   cv2.rectangle(image, (start_x, start_y - (2 * baseline +
    127. 5)), (start_x + w, start_y), (0, 255, 255), -1)
    128.   cv2.rectangle(image, (start_x, start_y), (end_x, end_y),
    129. (0, 255, 255), 2)
    130.   cv2.putText(image, str(confidence), (start_x, start_y),
    131. font, font_scale, (0, 0, 0), thickness)
    132. # Show image 显示图像
    133. cv2.imshow('Original', org)
    134. cv2.imshow('NMS', image)
    135. cv2.waitKey(0)

  • 相关阅读:
    H - XYZZY(spfa最大路径,判断正环)
    cell2location jupyter python界面
    在uniapp中为自定义组件绑定点击事件点击后没有效果
    组件之前的传值——中央事件总线bus
    图像修复:使用pytorch实现context encoders
    C++ 智能指针
    postgresql教程
    【限流与Sentinel超详细分析】
    随手记录: 快乐终端 ohmyzsh macOS/Ubuntu 共用
    微搭低代码中实现增删改查
  • 原文地址:https://blog.csdn.net/lbr15660656263/article/details/139822686