当前位置:   article > 正文

计算 矩形框IOU的两种方法_cv:rect 检测iou

cv:rect 检测iou

1 利用 algorithm 中的 min max 函数

2 利用opencv 中的  | 和 & 运算

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<opencv2/opencv.hpp>
  5. using namespace std;
  6. struct bbox
  7. {
  8. int m_left;
  9. int m_top;
  10. int m_width;
  11. int m_height;
  12. bbox() {}
  13. bbox(int left, int top, int width, int height)
  14. {
  15. m_left = left;
  16. m_top = top;
  17. m_width = width;
  18. m_height = height;
  19. }
  20. };
  21. float IOU(const bbox& b1, const bbox& b2)
  22. {
  23. float w = std::min(b1.m_left + b1.m_width, b2.m_left + b2.m_width)
  24. - std::max(b1.m_left, b2.m_left);
  25. float h = std::min(b1.m_top + b1.m_height, b2.m_top + b2.m_height)
  26. - std::max(b1.m_top, b2.m_top);
  27. if (w <= 0 || h <= 0)
  28. return 0;
  29. std::cout << "w :" << w << "h :" << h << std::endl;
  30. return (w * h) / ((b1.m_height * b1.m_width) + (b2.m_height * b2.m_width) - (w * h));
  31. }
  32. float IOU_cv(const cv::Rect & r1,const cv::Rect & r2)
  33. {
  34. cv::Rect and = r1 | r2;
  35. cv::Rect U = r1 & r2;
  36. return U.area()*1.0 / and.area();
  37. }
  38. int main()
  39. {
  40. bbox b1(0, 0, 40, 20);
  41. bbox b2(30,10,40,20);
  42. float iou = IOU(b1, b2);
  43. std::cout << "IOU :" << iou << std::endl;
  44. cv::Rect r1(0,0,40,20);
  45. cv::Rect r2(30,10,40,20);
  46. float iou_cv = IOU_cv(r1,r2);
  47. std::cout << "IOU_cv :" << iou << std::endl;
  48. system("pause");
  49. return 0;
  50. }

参考:
https://my.oschina.net/u/3800567/blog/1795889

https://blog.csdn.net/ifreewolf_csdn/article/details/89004662

 

 

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

闽ICP备14008679号