PointCloudLib RANSAC算法 分割空间中的多个圆 C++版本

测试效果

简介

没有

测试代码

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    cloud = PCL_Common::K_ReadPcdData("D:\\1_Kita\\Circle3DCloud.pcd");
    PCL_Common::K_ShowCloud(cloud, 2);

    // -------------------------------定义所需容器--------------------------------
    std::vector<int> totalInners;
    std::vector<int> indices(cloud->points.size());
    std::iota(std::begin(indices), std::end(indices), (int)0);

    float dist = 0.1;         // 距离阈值
    float minPointNum = 10; // 最小点数
    in
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
RANSACRandom Sample Consensus)算法是一种基于统计原理的模型拟合方法,可以用于拟合形模型。以下是一个用C++实现RANSAC算法拟合形模型的示例代码: ```cpp #include <iostream> #include <vector> #include <random> #include <cmath> // 定义形结构体 struct Circle { double x; // 心x坐标 double y; // 心y坐标 double r; // 半径 }; // 计算两点之间的距离 double distance(double x1, double y1, double x2, double y2) { return std::sqrt(std::pow(x2 - x1, 2) + std::pow(y2 - y1, 2)); } // 计算点到心的距离 double distance(double x, double y, Circle circle) { return std::sqrt(std::pow(x - circle.x, 2) + std::pow(y - circle.y, 2)); } // RANSAC算法拟合形模型 Circle ransac(std::vector<std::pair<double, double>> points, int iterations, double threshold) { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, points.size() - 1); Circle best_circle = {0, 0, 0}; int best_count = 0; for (int i = 0; i < iterations; i++) { // 随机选择三个点 int idx1 = dis(gen); int idx2 = dis(gen); int idx3 = dis(gen); // 计算形参数 double x1 = points[idx1].first; double y1 = points[idx1].second; double x2 = points[idx2].first; double y2 = points[idx2].second; double x3 = points[idx3].first; double y3 = points[idx3].second; double a = x1 - x2; double b = y1 - y2; double c = x1 - x3; double d = y1 - y3; double e = (std::pow(x1, 2) - std::pow(x2, 2)) + (std::pow(y1, 2) - std::pow(y2, 2)); double f = (std::pow(x1, 2) - std::pow(x3, 2)) + (std::pow(y1, 2) - std::pow(y3, 2)); double delta = a * d - b * c; if (delta == 0) { continue; } double x0 = (d * e - b * f) / (2 * delta); double y0 = (a * f - c * e) / (2 * delta); double r = distance(x0, y0, x1, y1); // 统计点数 int count = 0; for (std::pair<double, double> point : points) { if (distance(point.first, point.second, x0, y0) <= threshold) { count++; } } // 更新最优形 if (count > best_count) { best_circle.x = x0; best_circle.y = y0; best_circle.r = r; best_count = count; } } return best_circle; } int main() { // 生成随机点 std::vector<std::pair<double, double>> points; std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dis(-10, 10); for (int i = 0; i < 100; i++) { double x = dis(gen); double y = dis(gen); if (std::pow(x, 2) + std::pow(y, 2) <= 25) { // 在内的点 points.push_back({x, y}); } } // RANSAC拟合形模型 Circle circle = ransac(points, 1000, 1); // 输出结果 std::cout << "Best circle: (" << circle.x << ", " << circle.y << "), r = " << circle.r << std::endl; return 0; } ``` 该代码通过随机选择三个点计算形参数,并统计在内的点数。重复此过程若干次后,选取点数最多的形作为最优形。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄晓魚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值