当前位置:   article > 正文

c++实现NMS_nms c++

nms c++

目录

What is NMS?

Relevant Concepts

Example Logic

Code

References​​​​​​​

What is NMS?

NMS - Non-maximum suppression, 非极大值抑制

在图像识别目标检测任务中,通常用框框(Bounding Box)表示识别出来的对象,对于同一个对象可能有很多个有着不同重叠区域的Bounding Box。每个Bounding Box对应的属于该特征的概率也不尽相同(confidence,置信度)。

所以NMS是用来抑制 非 最大置信度 的识别结果,只留下来置信度高的(是该特征的概率最大)结果。

Relevant Concepts

  • Bounding Box
  • IoU - Intersection over Union, 交并比 : 两个Bounding Box的交集区域面积,除以并集区域面积

Example Logic

  1. input-Bounding Box List和IoU的阈值, output-Bounding List with NMS processing
  2. 按照confidence对Bounding Box从高到低排序
  3. 计算Bounding Box两两的IoU, 如果IoU大于阈值,从List删除当前两个BBox中confidence的低的那个
  4. 全部处理完,输出新的BBox List

Code

  1. //NMS.H
  2. #pragma once
  3. void nms_test();
  4. typedef struct BBox
  5. {
  6. int x; //(x,y) left-up position, for x, left is increasing
  7. int y; // for y, down is increasing
  8. int w; //width
  9. int h; //hight
  10. float confidence;
  11. }BBox;
  1. //NMS_test.cpp
  2. #include "NMS.h"
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iostream>
  6. float IOU(BBox box1, BBox box2)
  7. {
  8. float iou = 0;
  9. int x_over_l = std::max(box1.x, box2.x); //left x position of over area
  10. int x_over_r = std::min(box1.x + box1.w, box2.x + box2.w); //right position of over area
  11. int y_over_u = std::max(box1.y, box2.y);
  12. int y_over_d = std::min(box1.y + box1.h, box2.y + box2.h);
  13. int over_w = x_over_r - x_over_l;
  14. int over_h = y_over_d - y_over_u;
  15. if (over_w <= 0 || over_h <= 0)
  16. {
  17. iou = 0;
  18. }
  19. else {
  20. iou = (float)over_w * over_h / (box1.w * box1.h + box2.w * box2.h - over_w * over_h);
  21. }
  22. return iou;
  23. }
  24. std::vector<BBox> NMS(std::vector<BBox> &boxes, float threshold)
  25. {
  26. std::vector<BBox>resluts;
  27. sort(boxes.begin(), boxes.end(), [](BBox a, BBox b) {return a.confidence > b.confidence;});
  28. while (boxes.size() > 0)
  29. {
  30. resluts.push_back(boxes[0]);
  31. int index = 1;
  32. while (index < boxes.size()) {
  33. float iou_value = IOU(boxes[0], boxes[index]);
  34. std::cout << "iou_value=" << iou_value << '\n';
  35. if (iou_value > threshold) {
  36. boxes.erase(boxes.begin() + index);
  37. }
  38. else {
  39. index++;
  40. }
  41. }
  42. boxes.erase(boxes.begin());
  43. }
  44. return resluts;
  45. }
  46. void nms_test()
  47. {
  48. std::cout << "========NMS test begin======" << '\n';
  49. std::vector<BBox> input;
  50. BBox box1 = { 10,10,10,10,0.5 };
  51. BBox box2 = { 0,0,20,20,0.6 };
  52. input.push_back(box1);
  53. input.push_back(box2);
  54. std::vector<BBox> res;
  55. res = NMS(input, 0.15);
  56. for (int i = 0; i < res.size(); i++) {
  57. printf("%d %d %d %d %f", res[i].x, res[i].y, res[i].w, res[i].h, res[i].confidence);
  58. std::cout << '\n';
  59. }
  60. std::cout << "========NMS test end======" << '\n';
  61. }
  1. //main.cpp
  2. #include "NMS.h"
  3. int main()
  4. {
  5. nms_test();
  6. }

References

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

闽ICP备14008679号