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