最小二乘圆柱拟合(高斯牛顿法)

53 篇文章 6 订阅
37 篇文章 0 订阅

欢迎关注更多精彩
关注我,学习常用算法与数据结构,一题多解,降维打击。

本期话题:最小二乘圆柱拟合

相关背景资料
点击前往

在这里插入图片描述

圆柱拟合输入和输出要求

输入

  1. 8到50个点,全部采样自圆柱上。
  2. 每个点3个坐标,坐标精确到小数点后面20位。
  3. 坐标单位是mm, 范围[-500mm, 500mm]。

tips
输入虽然是严格来自圆柱,但是由于浮点表示,记录的值和真实值始终是有微小的误差,点云到拟合圆柱的距离不可能完全为0。

输出

  1. 1点X0表示 圆柱中心轴上1点,用三个坐标表示。
  2. 法向A代表圆柱中心轴法向。
  3. 圆半径r。

精度要求

  1. X0点到标准圆柱中心轴距离不能超过0.0001mm。
  2. 法向A与圆柱法向夹角不能超过0.0000001rad。
  3. r与标准半径的差不能超过0.0001mm。

圆柱优化标函数

根据论文,圆柱拟合转化成数学表示如下:

圆柱参数化表示

  1. 圆柱中心轴上1点X0 = (x0, y0, z0)。
  2. 法向A=(a,b,c)。
  3. 半径r。

圆柱方程 u 2 + v 2 + w 2 a 2 + b 2 + c 2 = r 2 u i = c ( y − y 0 ) − b ( z − z 0 ) v i = a ( z − z 0 ) − c ( x − x 0 ) w i = b ( x − x 0 ) − a ( y − y 0 )   圆柱方程\begin {array}{l}\frac {u^2+v^2+w^2} {a^2+b^2+c^2}=r^2 \\ u_i =c(y-y_0)-b(z-z_0)\\ v_i=a(z-z_0)-c(x-x_0)\\ w_i=b(x-x_0)-a(y-y_0)\ \end {array} 圆柱方程a2+b2+c2u2+v2+w2=r2ui=c(yy0)b(zz0)vi=a(zz0)c(xx0)wi=b(xx0)a(yy0) 

能量方程

第i个点 pi(xi, yi, zi)。

距离函数

d i = r i − r d_i = r_i-r di=rir

r i = u 2 + v 2 + w 2 u i = c ( y − y 0 ) − b ( z − z 0 ) v i = a ( z − z 0 ) − c ( x − x 0 ) w i = b ( x − x 0 ) − a ( y − y 0 ) \begin {array}{l}r_i = \sqrt{u^2+v^2+w^2} \\ u_i =c(y-y_0)-b(z-z_0)\\ v_i=a(z-z_0)-c(x-x_0)\\ w_i=b(x-x_0)-a(y-y_0) \end {array} ri=u2+v2+w2 ui=c(yy0)b(zz0)vi=a(zz0)c(xx0)wi=b(xx0)a(yy0)

根据定义得到能量方程

E = ∑ 1 n d i 2 E=\displaystyle \sum _1^n {d_i^2} E=1ndi2

最小二乘优化能量方程

能量方程 E = f ( X 0 , A , r ) = ∑ 1 n d i 2 E=f(X0, A, r)=\displaystyle \sum _1^n {d_i^2} E=f(X0,A,r)=1ndi2

上式是一个7元二次函数中,X0, A, r是未知量,拟合圆柱的过程也可以理解为优化X0, A, r使得方程E最小。

上述方程直接求导不好解,可以使用高斯牛顿迭代法。

高斯牛顿迭代法

用于解非线性最小二乘问题。同时,高斯牛顿法需要比较可靠的初值,所以寻找目标函数的初值也是一个比较关键的步骤。

基本原理

设 a = ( a 0 , a 1 , . . . , a n ) 是待求解向量, a ^ 是初始给定值, a = a ^ + Δ a , Δ a 是我们每次迭代后移动的量 设 a=(a_0, a_1,...,a_n)是待求解向量,\widehat {a} 是初始给定值,a = \widehat {a} +\Delta a, \Delta a 是我们每次迭代后移动的量 a=(a0,a1,...,an)是待求解向量,a 是初始给定值,a=a +Δa,Δa是我们每次迭代后移动的量

定义距离函数为 F ( x , a ) , d i = F ( x i , a ) , 进行泰勒 1 阶展开, F ( x , a ) = F ( x , a ^ ) + ∂ F ∂ a ^ Δ a = F ( x , a ^ ) + J Δ a 定义距离函数为 F(x, a), d_i = F(x_i, a), 进行泰勒1阶展开, F(x, a) = F(x, \widehat a) + \frac {\partial F}{\partial \widehat a}\Delta a = F(x, \widehat a) + J\Delta a 定义距离函数为F(x,a),di=F(xi,a),进行泰勒1阶展开,F(x,a)F(x,a )+a FΔa=F(x,a )+JΔa

每次迭代,其实就是希望通过调整 Δ a 使得 J Δ a = − F ( x , a ^ ) 每次迭代,其实就是希望通过调整\Delta a 使得 J\Delta a = -F(x, \widehat a) 每次迭代,其实就是希望通过调整Δa使得JΔaF(x,a )

J = [ ∂ F ( x 0 , a ) ∂ a 0 ∂ F ( x 0 , a ) ∂ a 1 . . . ∂ F ( x 0 , a ) ∂ a n ∂ F ( x 1 , a ) ∂ a 0 ∂ F ( x 1 , a ) ∂ a 1 . . . ∂ F ( x 1 , a ) ∂ a n . . . . . . . . . . . . ∂ F ( x n , a ) ∂ a 0 ∂ F ( x n , a ) ∂ a 1 . . . ∂ F ( x n , a ) ∂ a n ] J = \begin {bmatrix} \frac {\partial F(x_0, a)} {\partial a_0} & \frac {\partial F(x_0, a)} {\partial a_1} & ...& \frac {\partial F(x_0, a)} {\partial a_n} \\ \\ \frac {\partial F(x_1, a)} {\partial a_0} & \frac {\partial F(x_1, a)} {\partial a_1} & ...& \frac {\partial F(x_1, a)} {\partial a_n} \\\\ ... & ... & ...& ... \\ \\ \frac {\partial F(x_n, a)} {\partial a_0} & \frac {\partial F(x_n, a)} {\partial a_1} & ...& \frac {\partial F(x_n, a)} {\partial a_n} \end {bmatrix} J= a0F(x0,a)a0F(x1,a)...a0F(xn,a)a1F(x0,a)a1F(x1,a)...a1F(xn,a)............anF(x0,a)anF(x1,a)...anF(xn,a)

F ( x , a ^ ) = [ d 1 d 2 . . . d m ] F(x, \widehat a) = \begin {bmatrix} d_1 \\ d_2 \\... \\ d_m \end {bmatrix} F(x,a )= d1d2...dm

J Δ a = − F ( x , a ^ ) , 解出 Δ a , 更新 a = a ^ + Δ a , 持续迭代直到 Δ a 足够小 J\Delta a = -F(x,\widehat a), 解出\Delta a ,更新 a = \widehat {a} +\Delta a, 持续迭代直到\Delta a足够小 JΔa=F(x,a ),解出Δa,更新a=a +Δa,持续迭代直到Δa足够小

用4个数表示直线法向

如果直接拿6个参数表示直线去做迭代,1是比较麻烦,会出现比较难解的方向,2是法向长度不固定,结果不唯一。

当直线与Z轴偏差比较小的时候可以使用4个参数来表示直线。

在这里插入图片描述
如上图,绿线为Z轴,橙色线为XOY平面。

由于法向与Z轴比较相近,可以设法向为(a, b, 1), a,b 是比较小的量。

规定直线上1点需要在以(a, b, 1)为法向,过0点的平面上。

则有 ax0+by0 + z0=0, 只要知道x0, y0 可知 z0 = -ax0-by0。

模型简化

为了使用上述方法,当得到一个拟合初值后,需要先将中心线旋转致Z轴,把圆心移致0点。

此时,
x0=y0=z0=0.
a=b=0, c=1.

r i = ( x i 2 + y i 2 ) \begin {array}{l}r_i=\sqrt{(x_i^2+y_i^2)}\end {array} ri=(xi2+yi2)

算法描述

我们希望di0。
可以对di分别求偏导

J, D的计算。

J = ∂ d 1 ∂ x 0 ∂ d 1 ∂ y 0 ∂ d 1 ∂ a ∂ d 1 ∂ b ∂ d 1 ∂ r ∂ d 2 ∂ x 0 ∂ d 2 ∂ y 0 ∂ d 2 ∂ a ∂ d 2 ∂ b ∂ d 2 ∂ r . . . . . . . . . . . . . . . ∂ d n ∂ x 0 ∂ d n ∂ y 0 ∂ d n ∂ a ∂ d n ∂ b ∂ d n ∂ r ,   D = d 1 d 2 . . . d n J= \begin{array}{l} \frac {\partial d_1}{\partial x_0}& \frac {\partial d_1}{\partial y_0}& \frac {\partial d_1}{\partial a}& \frac {\partial d_1}{\partial b}& \frac {\partial d_1}{\partial r} \\ \frac {\partial d_2}{\partial x_0}& \frac {\partial d_2}{\partial y_0}& \frac {\partial d_2}{\partial a}& \frac {\partial d_2}{\partial b}& \frac {\partial d_2}{\partial r}\\...&...&...&...&...\\\frac {\partial d_n}{\partial x_0}& \frac {\partial d_n}{\partial y_0}& \frac {\partial d_n}{\partial a}& \frac {\partial d_n}{\partial b}& \frac {\partial d_n}{\partial r}\\ \end {array}, \ D= \begin{array}{l} d_1\\d_2\\...\\d_n\end {array} J=x0d1x0d2...x0dny0d1y0d2...y0dnad1ad2...adnbd1bd2...bdnrd1rd2...rdn, D=d1d2...dn

5个未知分别对d_i求导结果如下:

回顾一下

u i = c ( y i − y 0 ) − b ( z i − z 0 ) v i = a ( z i − z 0 ) − c ( x i − x 0 ) w i = b ( x i − x 0 ) − a ( y i − y 0 ) \begin {array}{l}u_i =c(y_i-y_0)-b(z_i-z_0)\\ v_i=a(z_i-z_0)-c(x_i-x_0)\\ w_i=b(x_i-x_0)-a(y_i-y_0)\end {array} ui=c(yiy0)b(ziz0)vi=a(ziz0)c(xix0)wi=b(xix0)a(yiy0)

∂ d i ∂ x 0 = u i 2 + v i 2 + w i 2 − r ∂ x 0 = ( x i − x 0 ) 2 ∂ x 0 2 u i 2 + v i 2 + w i 2 = − x i x i 2 + y i 2 \frac {\partial d_i} {\partial x_0}=\frac {\sqrt{u_i^2+v_i^2+w_i^2}-r} {\partial x_0} \\ =\frac {\frac {(x_i-x_0)^2}{\partial x_0}}{2\sqrt{u_i^2+v_i^2+w_i^2}}\\ =\frac {-x_i}{\sqrt{x_i^2+y_i^2}} x0di=x0ui2+vi2+wi2 r=2ui2+vi2+wi2 x0(xix0)2=xi2+yi2 xi

∂ d i ∂ y 0 = u i 2 + v i 2 + w i 2 − r ∂ y 0 = ( y i − y 0 ) 2 ∂ y 0 2 u i 2 + v i 2 + w i 2 = − y i x i 2 + y i 2 \frac {\partial d_i} {\partial y_0}=\frac {\sqrt{u_i^2+v_i^2+w_i^2}-r} {\partial y_0} \\ =\frac {\frac {(y_i-y_0)^2}{\partial y_0}}{2\sqrt{u_i^2+v_i^2+w_i^2}}\\ =\frac {-y_i}{\sqrt{x_i^2+y_i^2}} y0di=y0ui2+vi2+wi2 r=2ui2+vi2+wi2 y0(yiy0)2=xi2+yi2 yi

∂ d i ∂ a = u i 2 + v i 2 + w i 2 − r ∂ a = [ a ( z i − z 0 ) − c ( x i − x 0 ) ] 2 ∂ a 2 u i 2 + v i 2 + w i 2 = − x i z i x i 2 + y i 2 \frac {\partial d_i} {\partial a}=\frac {\sqrt{u_i^2+v_i^2+w_i^2}-r} {\partial a}\\ =\frac {\frac {[a(z_i-z_0)-c(x_i-x_0)]^2}{\partial a}}{2\sqrt{u_i^2+v_i^2+w_i^2}}\\ =\frac {-x_iz_i}{\sqrt{x_i^2+y_i^2}} adi=aui2+vi2+wi2 r=2ui2+vi2+wi2 a[a(ziz0)c(xix0)]2=xi2+yi2 xizi

∂ d i ∂ b = u i 2 + v i 2 + w i 2 − r ∂ b = [ c ( y i − y 0 ) − b ( z i − z 0 ) ] 2 ∂ b 2 u i 2 + v i 2 + w i 2 = − y i z i x i 2 + y i 2 \frac {\partial d_i} {\partial b}=\frac {\sqrt{u_i^2+v_i^2+w_i^2}-r} {\partial b}\\ =\frac {\frac {[c(y_i-y_0)-b(z_i-z_0)]^2}{\partial b}}{2\sqrt{u_i^2+v_i^2+w_i^2}}\\ =\frac {-y_iz_i}{\sqrt{x_i^2+y_i^2}} bdi=bui2+vi2+wi2 r=2ui2+vi2+wi2 b[c(yiy0)b(ziz0)]2=xi2+yi2 yizi

∂ d i ∂ r = u i 2 + v i 2 + w i 2 − r ∂ r = − 1 \frac {\partial d_i} {\partial r}=\frac {\sqrt{u_i^2+v_i^2+w_i^2}-r} {\partial r} = -1 rdi=rui2+vi2+wi2 r=1

  1. 确定圆初值

  2. 将中轴通过刚体变换U至Z轴,U的构建可以参考代码
    [ x i y i z i ] = U ⋅ ( [ x i y i z i ] − [ x 0 y 0 z 0 ] ) \begin {bmatrix}x_i \\ y_i \\ z_i \end {bmatrix} = U \cdot \left (\begin {bmatrix}x_i \\ y_i \\ z_i \end {bmatrix}- \begin{bmatrix}x_0 \\ y_0 \\ z_0 \end {bmatrix}\right ) xiyizi =U xiyizi x0y0z0

  3. 根据上述公式构建 J ⋅ ( [ p x 0 p y 0 p a p b p r ] ) = − D J \cdot \left(\begin {bmatrix}p_{x_0} \\ p_{y_0}\\p_a\\p_b\\p_{r} \end {bmatrix} \right)=-D J px0py0papbpr =D

  4. 求解 Δ p \Delta p Δp

  5. 更新解
    [ x 0 y 0 z 0 ] = [ x 0 y 0 z 0 ] + U T ⋅ [ p x 0 p y 0 − p a p x 0 − p b p y 0 ] [ a b c ] = U T ⋅ [ p a p b 1 ] . n o r m a l i z e ( ) r = r + p r \begin {array}{l}\\ \begin {bmatrix}x_0 \\ y_0 \\ z_0 \end {bmatrix} = \begin {bmatrix}x_0 \\ y_0 \\ z_0 \end {bmatrix} + U^T \cdot \begin{bmatrix}p_{x_0} \\ p_{y_0}\\ -p_ap_{x_0}-p_bp_{y_0}\end {bmatrix} \\ \\\begin {bmatrix}a \\ b \\ c \end {bmatrix} = U^T \cdot \begin{bmatrix}p_a \\ p_b \\ 1 \end {bmatrix}.normalize() \\\\ r=r+p_r \end {array} x0y0z0 = x0y0z0 +UT px0py0papx0pbpy0 abc =UT papb1 .normalize()r=r+pr

  6. 重复2直到收敛

初值确定

可以枚举法向(间隔1度),把所有点投影到该法向的任意平面上,求出平面上的圆。以误差最小的圆作为半径,圆心作为中轴上一点。可以并行加速。

代码实现

代码链接:https://gitcode.com/chenbb1989/3DAlgorithm/tree/master/CBB3DAlgorithm/Fitting

#include "CylinderFitter.h"
#include "CircleFitter.h"
#include <corecrt_math_defines.h>
#include <Eigen/Dense>
#include<iostream>


namespace Gauss {
	double F(Fitting::Cylinder cylinder, const Point& p)
	{
		double di = (p - cylinder.center).cross(cylinder.orient).norm() - cylinder.r;
		return di;
	}
	double GetError(Fitting::Cylinder cylinder, const std::vector<Eigen::Vector3d>& points)
	{
		double err = 0;
		for (auto& p : points) {
			double d = F(cylinder, p);
			err += d * d;
		}
		err /= points.size();
		return err;
	}
	Fitting::Matrix CylinderFitter::Jacobi(const std::vector<Eigen::Vector3d>& points)
	{
		int n = points.size();
		Fitting::Matrix J(n, 5);
		for (int i = 0; i < n; ++i) {
			auto& p = points[i];
			double ri = Eigen::Vector2d(p.x(), p.y()).norm();

			//di求导
			J(i, 0) = -p.x() / ri;
			J(i, 1) = -p.y() / ri;
			J(i, 2) = -p.x() * p.z() / ri;
			J(i, 3) = -p.y() * p.z() / ri;
			J(i, 4) = -1;
		}
		return J;
	}

	void CylinderFitter::beforHook(const std::vector<Eigen::Vector3d>& points)
	{
		U = Fitting::getRotationByOrient(cylinder.orient);
		for (int i = 0; i < points.size(); ++i) {
			transPoints[i] = U * (points[i] - cylinder.center);
		}
	}
	void CylinderFitter::afterHook(const Eigen::VectorXd& xp)
	{
		cylinder.center += U.transpose() * Eigen::Vector3d(xp(0), xp(1), -xp(2)*xp(0)-xp(3)*xp(1));
		cylinder.orient = U.transpose() * Eigen::Vector3d(xp(2), xp(3), 1).normalized();
		cylinder.r += xp(4);
	}
	Eigen::VectorXd CylinderFitter::getDArray(const std::vector<Eigen::Vector3d>& points)
	{
		int n = points.size();
		Eigen::VectorXd D(points.size());
		for (int i = 0; i < points.size(); ++i) D(i) = Eigen::Vector2d(points[i].x(), points[i].y()).norm() - cylinder.r;
		return D;
	}
	bool CylinderFitter::GetInitFit(const std::vector<Eigen::Vector3d>& points)
	{
		if (points.size() < 5)return false;
		double cylinerErr = -1;
		transPoints.resize(points.size());
		Point center = Fitting::getCenter(points);
		// 拟合平面
		Fitting::FittingBase* fb = new CircleFitter();
		Fitting::Circle cir;
		int cnt = 0;
		for (double i = 0; i < 180; ++i) {
			double theta = i / 180 * M_PI;
			for (double j = 0; j < 180; ++j) {
				double alpha = j / 180 * M_PI;
				Point orient(cos(theta) , sin(theta)*cos(alpha),sin(theta)*sin(alpha));
				// 投影平面
				for (int k = 0; k < points.size(); ++k) {
					double d = (center-points[k]).dot(orient);
					transPoints[k] = points[k] + d * orient;
				}

				// 拟合圆
				double err = fb->Fitting(transPoints, &cir);
				if (err>0 && (cylinerErr < 0 || err < cylinerErr)) {
					//std::cout << "error : "<< err << std::endl;
					cylinerErr = err;
					cylinder.center = cir.center;
					cylinder.orient = cir.orient;
					cylinder.r = cir.r;
				}
				//cnt++;
				//if(cnt%100==0) std::cout << cnt << std::endl;

			}
			if (cylinerErr < 1e-4)break;
		}
		delete fb;
		return true;
	}
	double CylinderFitter::F(const Eigen::Vector3d& p)
	{
		return Gauss::F(cylinder, p);
	}
	double CylinderFitter::GetError(const std::vector<Eigen::Vector3d>& points)
	{
		return Gauss::GetError(cylinder, points);
	}
	void CylinderFitter::Copy(void* ele)
	{
		memcpy(ele, &cylinder, sizeof(Fitting::Cylinder));
	}
}


测试代码

#include "TestCylinder.h"
#include "CylinderFitter.h"
#include <iostream>

namespace Gauss {

	double TestCylinder::Fitting() {
		Fitting::FittingBase* fb = new Gauss::CylinderFitter();
		auto err = fb->Fitting(points, &fitResult);
		return err;
	}
	bool TestCylinder::JudgeAnswer(FILE* fp) {
		ReadAnswer();
		if (!lineCmp(ans.orient, ans.center, fitResult.center))return false;
		if (!orientationCmp(ans.orient, fitResult.orient))return false;
		if (!radiusCmp(ans.r, fitResult.r))return false;
		return true;
	}
	void TestCylinder::ReadAnswer() {
		vector<double> nums;
		if (PointCloud::readNums((suffixName + "/answer/" + fileName + ".txt"), nums)) {
			for (int i = 0; i < 3; ++i) ans.center[i] = nums[i];
			for (int i = 0; i < 3; ++i) ans.orient[i] = nums[i+3];
			ans.r = nums[6];
		}
		else {
			std::cout << "read answer error" << std::endl;
		}
	}
	void TestCylinder::SaveAnswer(FILE* fp) {
		writePoint(fp, fitResult.center);
		writePoint(fp, fitResult.orient);
		writeNumber(fp, fitResult.r);
	}
}

测试结果

https://gitcode.com/chenbb1989/3DAlgorithm/blob/master/CBB3DAlgorithm/Fitting/gauss/fitting_result/result.txt

b16 : CYLINDER : pass
b17 : CYLINDER : pass
b18 : CYLINDER : pass
b19 : CYLINDER : pass
b20 : CYLINDER : pass
b21 : CYLINDER : pass
b22 : CYLINDER : pass
b23 : CYLINDER : pass
b24 : CYLINDER : pass
b25 : CYLINDER : pass

本人码农,希望通过自己的分享,让大家更容易学懂计算机知识。创作不易,帮忙点击公众号的链接。

  • 24
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
对于二维平面上的一组数据点,可以使用最小二乘拟合来找到一个圆,使得所有数据点到这个圆的距离之和最小。这个方法也可以扩展到三维空间中的圆柱拟合。下面是一个使用Python实现最小二乘圆柱拟合的示例代码: ```python import numpy as np from scipy.optimize import least_squares def fun(params, x, y, z): xc, yc, r, h = params return (x - xc)**2 + (y - yc)**2 - r**2 + (z / h)**2 def solve_cylinder_least_squares(x, y, z): x0 = np.array([np.mean(x), np.mean(y), np.std(x+y)/2, np.std(z)]) res = least_squares(fun, x0, args=(x, y, z)) xc, yc, r, h = res.x return xc, yc, r, h ``` 其中,函数`fun`定义了最小二乘圆柱拟合的目标函数。`params`是一个包含圆柱体参数的数组,`x`、`y`、`z`分别是数据点的三个坐标。函数`solve_cylinder_least_squares`使用`least_squares`函数来求解最小二乘圆柱拟合,返回圆柱体的中心坐标`(xc, yc)`,半径`r`和高度`h`。 使用上面的函数,我们可以对一组随机生成的数据点进行最小二乘圆柱拟合: ```python # 生成随机数据点 n = 100 x = np.random.rand(n) * 10 - 5 y = np.random.rand(n) * 10 - 5 z = np.random.rand(n) * 10 - 5 # 最小二乘圆柱拟合 xc, yc, r, h = solve_cylinder_least_squares(x, y, z) print('圆柱体参数:') print('center: ({:.3f}, {:.3f})'.format(xc, yc)) print('radius:', r) print('height:', h) ``` 输出结果如下: ``` 圆柱体参数: center: (-0.007, -0.035) radius: 2.589 height: 9.847 ``` 可以看到,我们成功地拟合出了一个圆柱体,并得到了它的参数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值