当前位置: 首页 > news >正文

计算3D目标框的NMS

3D障碍物目标框(中心点坐标XYZ、长宽高lwh、朝向角theta)的非极大值抑制

#include <iostream>
#include <vector>
#include <algorithm>
#include <opencv2/opencv.hpp>// 定义3D目标框的结构体
struct BoundingBox3D
{double centerX, centerY, centerZ; // 中心点坐标double length, width, height;     // 长宽高double theta;                     // 朝向角double score;                     // 目标框得分BoundingBox3D(double x, double y, double z, double l, double w, double h, double t, double s): centerX(x), centerY(y), centerZ(z), length(l), width(w), height(h), theta(t), score(s) {}
};class NMS3D
{
public:// 构造函数,传入IoU阈值NMS3D(double iouThreshold) : iouThreshold_(iouThreshold) {}// 执行NMSstd::vector<BoundingBox3D> executeNMS(const std::vector<BoundingBox3D> &boxes){std::vector<BoundingBox3D> resultBoxes;// 按得分降序排序std::vector<BoundingBox3D> sortedBoxes = sortBoxesByScore(boxes);// 遍历排序后的框while (!sortedBoxes.empty()){// 保留得分最高的框BoundingBox3D topBox = sortedBoxes[0];resultBoxes.push_back(topBox);// 移除与当前框IoU大于阈值的框sortedBoxes.erase(sortedBoxes.begin());sortedBoxes = removeOverlappingBoxes(topBox, sortedBoxes);}return resultBoxes;}private:// 按得分降序排序std::vector<BoundingBox3D> sortBoxesByScore(const std::vector<BoundingBox3D> &boxes){std::vector<BoundingBox3D> sortedBoxes = boxes;std::sort(sortedBoxes.begin(), sortedBoxes.end(),[](const BoundingBox3D &a, const BoundingBox3D &b){return a.score > b.score;});return sortedBoxes;}// 移除与指定框IoU大于阈值的框std::vector<BoundingBox3D> removeOverlappingBoxes(const BoundingBox3D &box,const std::vector<BoundingBox3D> &boxes){std::vector<BoundingBox3D> filteredBoxes;for (const auto &b : boxes){if (calculateIoU(box, b) < iouThreshold_){filteredBoxes.push_back(b);}}return filteredBoxes;}// 计算两个框的IoU(Intersection over Union)double calculateIoU(const BoundingBox3D &box1, const BoundingBox3D &box2){// 计算两个框的相交部分的体积double intersectionVolume = calculateIntersectionVolume(box1, box2);// 计算两个框的并集部分的体积double unionVolume = box1.length * box1.width * box1.height +box2.length * box2.width * box2.height -intersectionVolume;// 计算IoUreturn intersectionVolume / unionVolume;}// 计算两个框的相交部分的体积double calculateIntersectionVolume(const BoundingBox3D &box1, const BoundingBox3D &box2){// 计算平面重叠面积double intersectArea = calIntersectionArea(box1, box2);double intersectHeight = calculateOverlap(box1.centerZ, box1.height, box2.centerZ, box2.height);// 计算相交部分的体积return intersectArea * intersectHeight;}cv::Point rotatePoint(const cv::Point &point, double angle){double rotatedX = point.x * cos(angle) - point.y * sin(angle);double rotatedY = point.x * sin(angle) + point.y * cos(angle);return cv::Point(rotatedX, rotatedY);}double calIntersectionArea(const BoundingBox3D &box1, const BoundingBox3D &box2){cv::RotatedRect rect1(cv::Point2f(box1.centerX,box1.centerY),cv::Size2f(box1.width,box1.height),box1.theta);cv::RotatedRect rect2(cv::Point2f(box2.centerX,box2.centerY),cv::Size2f(box2.width,box2.height),box2.theta);std::vector<cv::Point2f> intersection;cv::rotatedRectangleIntersection(rect1,rect2, intersection);// std::cout <<rect1.center<< " "<<rect2.center<<std::endl;// std::cout <<rect1.size<< " "<<rect2.size<<std::endl;// std::cout << "intersection area:"<<intersection.size()<<std::endl;double union_area = cv::contourArea(intersection);// std::cout << "intersection area:"<<union_area<<std::endl;return union_area;}// 计算两个轴上的重叠部分长度double calculateOverlap(double center1, double size1, double center2, double size2){double halfSize1 = size1 / 2;double halfSize2 = size2 / 2;double min1 = center1 - halfSize1;double max1 = center1 + halfSize1;double min2 = center2 - halfSize2;double max2 = center2 + halfSize2;// 计算重叠部分长度return std::max(0.0, std::min(max1, max2) - std::max(min1, min2));}double iouThreshold_; // IoU阈值
};int main()
{std::vector<BoundingBox3D> inputBoxes;inputBoxes.push_back(BoundingBox3D(0.0, 0.0, 0.0, 200.0,200.0, 200.0, 45, 0.9));inputBoxes.push_back(BoundingBox3D(100,100, 10, 200.0, 200.0, 200.0, -45, 0.8));//inputBoxes.push_back(BoundingBox3D(2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 0, 0.7));double iouThreshold = 0.5; // 可根据实际情况调整IoU阈值NMS3D nms(iouThreshold);std::vector<BoundingBox3D> resultBoxes = nms.executeNMS(inputBoxes);// 输出结果框for (const auto &box : resultBoxes){std::cout << "Center: (" << box.centerX << ", " << box.centerY << ", " << box.centerZ << "), "<< "Dimensions: (" << box.length << ", " << box.width << ", " << box.height << "), "<< "Theta: " << box.theta << ", "<< "Score: " << box.score << std::endl;}return 0;
}

关于cv::contourArea可能计算不准的问题,是由于传入的点没有按照一定的顺序排列(顺时针或逆时针)。参考解决博客

http://www.lryc.cn/news/232445.html

相关文章:

  • 【Java实现图书管理系统】
  • ROS 多机器人导航RVIZ环境的配置
  • UE4 / UE5 内存与性能优化
  • dotnet core程序部署到ubuntu
  • Antv/G2 柱状图添加自定义点击事件
  • Python---数据序列中的公共方法
  • 从0开始python学习-33.夹具@pytest.fixture(scope=““,params=““,autouse=““,ids=““,name=““)
  • vue3别名配置(vite)
  • 初学UE5 C++①
  • ElasticSearch6.8.1 常见错误
  • wx.canvasToTempFilePath生成图片保存到相册
  • HDU1276:士兵队列训练问题 ← STL queue
  • JS 读取excel文件内容 和 将json数据导出excel文件
  • ASP.NET限流器的简单实现
  • 汇编语言循环左移和循环右移如何实现的,详细的比喻一下
  • ChromeDriver 各版本下载地址
  • 计算机网络之物理层
  • 沉浸式航天vr科普馆VR太空主题馆展示
  • AI电话机器人能否代替人工?优缺点介绍
  • Java —— 多态
  • UI自动化测试(弹出框,多窗口)
  • Python爬虫程序网络请求及内容解析
  • C嘎嘎模板
  • 数据结构和算法八股与手撕
  • windiws docker 部署jar window部署docker 转载
  • 使用git上传代码至gitee入门(1)
  • 分类预测 | MATLAB实现基于Isomap降维算法与改进蜜獾算法IHBA的Adaboost-SVM集成多输入分类预测
  • 如何解决3d max渲染效果图全白这类异常问题?
  • 振南技术干货集:比萨斜塔要倒了,倾斜传感器快来!(2)
  • 图形学 -- Geometry几何