RANSAC算法平面圆拟合_C++实现

输入一组点云,使用RANSAC算法进行平面圆拟合,得到圆心和半径

#include <iostream>
#include <random>
#include <cmath>


struct PointCloud3d
{
	double x,y,z;
}


bool RansacCircle(QList<PointCloud3d>& cloud, double* circlePara)
{
	int cloudSize = cloud.size();    
	int maxIterCnt = cloudSize / 2;  //最大迭代次数
	double maxErrorThreshold = 0.02; //最大误差阈值(其实就是点到圆周的距离阈值)
	int consensusCntThreshold = maxIterCnt;

	if (!cloudSize || cloudSize <= 3)
	{
		return false;
	}

	//在点云链表的下标取值范围内产生随机数,目的是可以随机选择一个点云
	std::default_random_engine rng;
	std::uniform_int_distribution <int> uniform(0, cloudSize - 1);
	rng.seed(777);  //seed函数可以接收任意整数作为实参
	
	QList<int> selectIndexs;            //随机选择的点云的索引
	QList<PointCloud3d> selectPoints;   //随机选择的点云对象
	QList<int> consensusIndexs;         //满足一致性条件的点云的索引

	double centerX = 0, centerY = 0, R = 0; //平面圆参数
	double modelMeanError = 0;              //平均误差
	int bestConsensusCnt = 0;               //满足一致性的点的个数
	int iter = 0;                           //迭代次数

	//开始迭代计算
	while (iter < maxIterCnt)
	{
		selectIndexs.clear();
		selectPoints.clear();
		//在原始点云中随机取出3个点,因为至少需要3个点才能确定一个平面圆
		for (int c = 0; c < 3; ++c)
		{
			selectPoints.append(cloud.at(uniform(rng)));
		}

		const double& x1 = selectPoints.at(0).x;
		const double& y1 = selectPoints.at(0).y;
	
		const double& x2 = selectPoints.at(1).x;
		const double& y2 = selectPoints.at(1).y;
		
		const double& x3 = selectPoints.at(2).x;
		const double& y3 = selectPoints.at(2).y;
		
		//利用三个点坐标计算平面圆的参数
		double xa = (x1 + x2) / 2.0, ya = (y1 + y2) / 2.0;
		double xb = (x1 + x3) / 2.0, yb = (y1 + y3) / 2.0;

		double ka = (x1 - x2) / (y2 - y1);
		double kb = (x1 - x3) / (y3 - y1);

		centerX = (yb - ya + ka * xa - kb * xb) / (ka - kb);
		centerY = ka * centerX - ka * xa + ya;
		R = sqrt((centerX - xa)*(centerX - xa) + (centerY - ya)*(centerY - ya));

		double meanError = 0;
		QList<int> tmpConsensusIndexs; //满足一致性条件的点索引集合,这只是临时索引集合,需要随时更新。
		for (int j = 0; j < cloudSize; ++j)
		{
			const PointCloud3d& point = cloud.at(j);
			double distance = abs(R - sqrt((point.x - centerX)*(point.x - centerX) + (point.y - centerY)*(point.y - centerY)));
			if (distance < maxErrorThreshold)
			{
				tmpConsensusIndexs.append(j);
			}
			meanError += distance;
		}

		if (tmpConsensusIndexs.size() >= bestConsensusCnt && tmpConsensusIndexs.size() >= consensusCntThreshold)
		{
			bestConsensusCnt = consensusIndexs.size();  // 更新一致性索引集合元素个数
			modelMeanError = meanError / cloudSize;
			consensusIndexs.clear();
			consensusIndexs = tmpConsensusIndexs;        // 更新一致性索引集合
			circlePara[0] = centerX;  //圆心X
			circlePara[1] = centerY;  //圆心Y
			circlePara[2] = R;        //半径
		}
		iter++;
	}
	return true;
}
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; } ``` 该代码通过随机选择三个点计算形参数,并统计在内的点数。重复此过程若干次后,选取点数最多的形作为最优形。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值