• 【CV基石】Soft-NMS


    1. import numpy as np
    2. # 定义一个nms函数
    3. def soft_nms(dets, thresh=0.3, sigma=0.5): # score大于thresh的才能存留下来,当设定的thresh过低,存留下来的框就很多,所以要根据实际情况调参
    4. '''
    5. input:
    6. dets: dets是(n,5)的ndarray,第0维度的每个元素代码一个框:[x1, y1, x2, y2, score]
    7. thresh: float
    8. sigma: flaot
    9. output:
    10. index
    11. '''
    12. x1 = dets[:, 0] # dets:(n,5) x1:(n,) dets是ndarray, x1是ndarray
    13. y1 = dets[:, 1]
    14. x2 = dets[:, 2]
    15. y2 = dets[:, 3]
    16. scores = dets[:, 4] # scores是ndarray
    17. # 每一个候选框的面积
    18. areas = (x2 - x1 + 1) * (y2 - y1 + 1) # areas:(n,)
    19. # order是按照score降序排序的
    20. order = scores.argsort()[::-1] # order:(n,) 降序下标 order是ndarray
    21. keep = []
    22. while order.size > 0:
    23. i = order[0] # i 是当下分数最高的框的下标
    24. # print(i)
    25. keep.append(i)
    26. # 计算当前概率最大矩形框与其他矩形框的相交框的坐标,会用到numpy的broadcast机制,得到的是向量
    27. # 当order只有一个值的时候,order[1]会报错说index out of range,而order[1:]会是[],不报错,[]也可以作为x1的索引,x1[[]]为[]
    28. xx1 = np.maximum(x1[i], x1[order[1:]]) # xx1:(n-1,)的ndarray x1[i]:numpy_64浮点数一个,x1[order[1:]]是个ndarray,可以是空的ndarray,如果是空ndarray那么xx1为空ndarray,如果非空,那么x1[order[1:]]有多少个元素,xx1就是有多少个元素的ndarray。x1[]是不是ndarray看中括号内的是不是ndarray,看中括号内的是不是ndarray看中括号内的order[]的中括号内有没有冒号,有冒号的是ndarray,没有的是一个数。
    29. yy1 = np.maximum(y1[i], y1[order[1:]])
    30. xx2 = np.minimum(x2[i], x2[order[1:]])
    31. yy2 = np.minimum(y2[i], y2[order[1:]])
    32. # 计算相交框的面积,注意矩形框不相交时w或h算出来会是负数,用0代替
    33. w = np.maximum(0.0, xx2 - xx1 + 1) # xx2-xx1是(n-1,)的ndarray,w是(n-1,)的ndarray, n会逐渐减小至1
    34. # 当xx2和xx1是空的,那w是空的
    35. h = np.maximum(0.0, yy2 - yy1 + 1)
    36. inter = w * h # inter是(n,)的ndarray
    37. # 当w和h是空的,inter是空的
    38. # 计算重叠度IOU:重叠面积/(面积1+面积2-重叠面积)
    39. eps = np.finfo(areas.dtype).eps # 除法考虑分母为0的情况,np.finfo(dtype).eps,np.finfo(dtype)是个类,它封装了机器极限浮点类型的数,比如eps,episilon的缩写,表示小正数。
    40. ovr = inter / np.maximum(eps, areas[i] + areas[order[1:]] - inter) # n-1 #一旦(面积1+面积2-重叠面积)为0,就用eps进行替换
    41. # 当inter为空,areas[i]无论inter空不空都是有值的,那么ovr也为空
    42. # 更新分数
    43. weight = np.exp(-ovr*ovr/sigma)
    44. scores[order[1:]] *= weight
    45. # 更新order
    46. score_order = scores[order[1:]].argsort()[::-1] + 1
    47. order = order[score_order]
    48. keep_ids = np.where(scores[order]>thresh)[0]
    49. order = order[keep_ids]
    50. return keep
    51. import numpy as np
    52. import cv2
    53. # 读入图片,录入原始人框([x1, y1, x2, y2, score])
    54. image = cv2.imread('w.jpg')
    55. boxes = np.array([
    56. [5, 52, 171, 270, 0.9999],
    57. [13, 1, 179, 268, 0.9998],
    58. [20, 7, 176, 262, 0.8998],
    59. [7, 5, 169, 272, 0.9687],
    60. [3, 43, 162, 256, 0.9786],
    61. [10, 56, 167, 266, 0.8988]
    62. ])
    63. # 将框绘制在图像上
    64. image_for_nms_box = image.copy()
    65. for box in boxes:
    66. x1, y1, x2, y2, score = int(box[0]), int(box[1]), int(box[2]), int(box[3]), box[4] # x:col y:row
    67. image_for_nms_box = cv2.rectangle(image_for_nms_box, (x1, y1), (x2, y2), (0,255,0), 2)
    68. cv2.imwrite("w_all.jpg", image_for_nms_box)
    69. cv2.imshow('w_all', image_for_nms_box)
    70. # 使用soft_nms对框进行筛选
    71. keep = soft_nms(boxes)
    72. soft_nms_boxs = boxes[keep]
    73. # 将筛选过后的框绘制在图像上
    74. image_for_nms_box = image.copy()
    75. for box in soft_nms_boxs:
    76. x1, y1, x2, y2, score = int(box[0]), int(box[1]), int(box[2]), int(box[3]), box[4]
    77. image_for_nms_box = cv2.rectangle(image_for_nms_box, (x1, y1), (x2, y2), (0,255,0), 2)
    78. # Syntax: cv2.imwrite(filename, image)
    79. cv2.imwrite("w_soft_nms.jpg", image_for_nms_box)
    80. cv2.imshow('w_soft_nms', image_for_nms_box)
    81. cv2.waitKey()
    82. cv2.destroyAllWindows()

    https://blog.csdn.net/AliceH1226/article/details/123429849

  • 相关阅读:
    JS | 通过委派事件的多个Table之间的切换
    数据结构 实验 1
    presto框架【博学谷学习记录】
    Python数据分析与机器学习4-Matplotlib
    六、数学建模之插值与拟合
    深入剖析:HTML页面从用户请求到完整呈现的多阶段加载与渲染全流程详解
    强化学习从基础到进阶-案例与实践[4]:深度Q网络-DQN、double DQN、经验回放、rainbow、分布式DQN
    swin-transformer初步理解
    基于STM32F4系列的ETH IAP在线升级程序
    51单片机学习:DS18B20温度传感器实验
  • 原文地址:https://blog.csdn.net/AaronYKing/article/details/126104939