当前位置:   article > 正文

CV-NMS_nws在cv中代表什么

nws在cv中代表什么

        对于目标检测来说, 非极大值抑制的含义就是对于重叠度较高的一部分同类候选框来说, 去掉那些置信度较低的框, 只保留置信度最大的那一个进行后面的流程, 这里的重叠度高低与否是通过 iou 阈值来判断的.

        算法逻辑:

        输入: n 行 5 列的候选框数组,每行依次为:左上角横坐标,左上角纵坐标,右下角横坐标,右下角纵坐标,置信度

        输出: m 行 5 列的候选框数组, 每行含义同输入

        算法流程:

        计算 n 个候选框的面积大小

        对置信度进行排序, 获取排序后的下标序号, 即采用argsort

        将当前置信度最大的框加入返回值列表中

        获取当前置信度最大的候选框与其他任意候选框的相交面积

        利用相交的面积和两个框自身的面积计算框的交并比, 将交并比大于阈值的框删除.

        对剩余的框重复以上过程

        代码实现:

        1、面试简单版本(不包含iou实现,iou实现可以参考本笔记本其他笔记,注意排序的实现方法)

  1. def nms(bboxs, threshold):
  2. """
  3. 计算nms
  4. 不包含根据得分预先过滤的功能
  5. :param bboxs: 列表[[xt1, yt1, xb1, yb1, score1], .....]
  6. :param threshold: iou过滤阈值
  7. :return:
  8. """
  9. if len(bboxs) == 0:
  10. return bboxs
  11. # 按得分排序
  12. def take_score(c):
  13. return c[4]
  14. bboxs.sort(key=take_score, revere=True)
  15. result = []
  16. for i in range(len(bboxs) - 1):
  17. if bboxs[i][4] > 0:
  18. result.append(bboxs[i])
  19. for j in range(i, len(bboxs)):
  20. if iou(bboxs[i][:4], bboxs[j][:4]) > threshold:
  21. bboxs[j][4] = 0
  22. return result

        2、numpy实现(高效且包含iou计算)

  1. mport numpy as np
  2. def nms(bboxs, threshold):
  3. """
  4. 计算nms
  5. 不包含根据得分预先过滤的功能
  6. :param bboxs: numpy 矩阵[[xt1, yt1, xb1, yb1, score1], .....]
  7. :param threshold: iou过滤阈值
  8. :return:
  9. """
  10. if len(bboxs) == 0:
  11. return bboxs
  12. xt = bboxs[:, 0]
  13. yt = bboxs[:, 1]
  14. xb = bboxs[:, 2]
  15. yb = bboxs[:, 3]
  16. score = bboxs[:, 4]
  17. areas = (xb - xt + 1) * (yb - yt + 1)
  18. result = []
  19. # 对置信度进行排序, 获取排序后的下标序号, argsort 默认从小到大排序
  20. index_sorted = np.argsort(score)
  21. while index_sorted.size > 0:
  22. index_current = index_sorted[-1]
  23. # 将当前置信度最大的框加入返回值列表中
  24. result.append(bboxs[index_current])
  25. # 获取当前置信度最大的候选框与其他任意候选框的相交面积
  26. x_min = np.maximum(xt[index_sorted[:-1]], xt[index_current])
  27. x_max = np.minimum(xb[index_sorted[:-1]], xb[index_current])
  28. y_min = np.maximum(yt[index_sorted[:-1]], yt[index_current])
  29. y_max = np.minimum(yb[index_sorted[:-1]], yb[index_current])
  30. w = np.maximum(0, x_max - x_min + 1)
  31. h = np.maximum(0, y_max - y_min + 1)
  32. area_i = w * h
  33. iou = area_i / (areas[index_sorted[:-1]] + areas[index_current] - area_i)
  34. index_sorted = index_sorted(np.where(iou < threshold))
  35. return result

参考连接:

1、https://hellozhaozheng.github.io/z_post/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89-NMS-Implementation/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/698712
推荐阅读
相关标签
  

闽ICP备14008679号