使用OpenCV拟合点云投影椭圆 - C++代码示例
cv::Point2f convertPoint(const pcl::PointXYZRGB& point)\n{\n return cv::Point2f(point.x, point.y);\n}\n\n// 使用OpenCV拟合椭圆\ncv::RotatedRect fitEllipse(const pcl::PointCloudpcl::PointXYZRGB::Ptr& cloud)\n{\n // 将PointCloud转换为Point2f的向量\n std::vectorcv::Point2f points;\n for (const auto& point : cloud->points) \n {\n points.push_back(convertPoint(point));\n }\n\n // 使用OpenCV拟合椭圆\n return cv::fitEllipse(points);\n}\npcl::ProjectInlierspcl::PointXYZRGB proj; \nproj.setModelType(pcl::SACMODEL_PLANE);\nproj.setInputCloud(Ptrr);\nproj.setModelCoefficients(coefficients);\nproj.filter(*touy);\n\n// 可视化\n//boost::shared_ptrpcl::visualization::PCLVisualizer viewer(new pcl::visualization::PCLVisualizer("cloud show"));\n//int v1 = 0;\n//int v2 = 1;\n//viewer->createViewPort(0, 0, 0.5, 1, v1);\n//viewer->createViewPort(0.5, 0, 1, 1, v2);\n//viewer->setBackgroundColor(1, 1, 1, v1);\n//viewer->setBackgroundColor(1, 1, 1, v2);\n//// 原始点云绿色\n//pcl::visualization::PointCloudColorHandlerCustompcl::PointXYZRGB src_h(Ptrr, 0, 255, 0);\n//// 投影后的点云\n//pcl::visualization::PointCloudColorHandlerCustompcl::PointXYZRGB pro_cloud(touy, 0, 0, 255);\n////viewer->setBackgroundColor(255, 255, 255);\n//viewer->addPointCloud(Ptrr, src_h, "cloud", v1);\n//viewer->addPointCloud(touy, pro_cloud, "pro_cloud", v2);\n//while (!viewer->wasStopped())\n//{\n// viewer->spinOnce(100);\n// boost::this_thread::sleep(boost::posix_time::microseconds(10000));\n//}\n\n// 根据投影的点云数据拟合椭圆\n// 拟合椭圆\ncv::RotatedRect ellipse = fitEllipse(touy);\n\n// 输出长轴和短轴\nstd::cout << "Major axis: " << ellipse.size.width << std::endl;\nstd::cout << "Minor axis: " << ellipse.size.height << std::endl;
原文地址: https://www.cveoy.top/t/topic/p1fA 著作权归作者所有。请勿转载和采集!