Least Squares Fit Cylindrical

insert image description here

This article was originally created by CSDN Dianyunxia, ​​and the original text link . If you didn't see this article in Dianyunxia's blog, then this is a shameless crawler.

1. Algorithm principle

  From the geometric properties of the cylindrical surface, the distance from the point on the cylindrical surface to its axis is always equal to the radius r 0 r_0r0,As shown in Figure 1.
insert image description here

  Among them, PPP is any point on the cylindrical surface,P 0 ( x 0 , y 0 , z 0 ) P_0( x_0, y_0, z_0)P0(x0y0z0) is a point on the cylinder axis,( a , b , c ) ( a, b, c)( a , b , c ) is the cylinder axis vector
,r 0 r_0r0is the radius of the base circle of the cylinder. The distance from any point on the cylindrical surface to its axis is the radius r 0 r_0r0,即:
( x − x 0 ) 2 + ( y − y 0 ) 2 + ( z − z 0 ) 2 − [ a ( x − x 0 ) + b ( y − y 0 ) + c ( z − z 0 ) ] 2 = r 0 2 (1) (x-x_0)^2+(y-y_0)^2+(z-z_0)^2-[a(x-x_0)+b(y-y_0)+c(z-z_0)]^2=r_0^2\tag{1} (xx0)2+(yy0)2+(zz0)2[a(xx0)+b(yy0)+c(zz0)]2=r02(1)

  Equation (1) is the cylindrical surface equation. According to the coordinates ( x , y , z ) of the point on the cylindrical surface ( x, y, z)( x , y , z ) , find a point on the cylinder axis( x 0 , y 0 , z 0 ) ( x_0, y_0, z_0)(x0y0z0) , cylinder axis vector( a , b , c ) ( a, b, c)( a , b , c ) , the radius of the cylinder bottom circler 0 r_0r0These seven parameters can uniquely determine a cylinder.

The detailed steps of the cylinder fitting method are as follows:
(1) Determine the initial values ​​of the cylinder model parameters. The estimation principle of the initial value of cylinder fitting is as follows:

  Search for several adjacent points of any point on the cylindrical surface, fit these points into a plane, and normalize the obtained plane normal vector, which is the unit normal vector of the point. Process each point on the cylindrical surface to obtain the unit normal vector of each point. Treat the unit normal vector of each point as a point, and fit these points into a plane to obtain the plane normal vector, that is, the initial value of the cylinder axis vector a 0 , b 0 , c 0 a_0, b_0, c_0a0b0c0, this step uses principal component analysis to solve.
  First perform coordinate transformation on the cylinder so that the cylinder axis vector ( a 0 , b 0 , c 0 ) (a_0, b_0, c_0)(a0b0c0) into parallelZZThe vector of the Z axis is( 0 , 0 , ( a 0 ) 2 + ( b 0 ) 2 + ( c 0 ) 2 ) ( 0, 0, \sqrt{( a_0 )^ 2 + ( b_0)^2 + ( c_0)^ 2} )(00(a0)2+(b0)2+(c0)2 ) . Let the rotation matrix beR 3 × 3 R_{3 ×3}3×3, to make:
insert image description here
insert image description here
  the rotation matrix with the coordinates ( x , y , z ) of a point on the cylinder (x,y,z)(x,y,z ) to get the new coordinates( x ′ , y ′ , z ′ ) (x',y',z')(x,y,z )x ′ , y ′ x',y'in the new coordinatesx,y Get the coordinates of the bottom surface of the cylinder in the new coordinate system, according to( x ′ , y ′ ) (x',y')(x,y )can fit a circle.
  The equation of a circle is( x − x 1 0 ′ ) 2 + ( y − y 1 0 ′ ) 2 = ( r 0 ) 2 (x-x_1^{0'})^2+(y-y_1^{0' })^2=(r_0)^2(xx10)2+(yy10)2=(r0)2If you know the coordinates of the center of the circlex 1 0 ′ , y 1 0 ′ x_1^{0'},y_1^{0'}x10,y10, radius r 0 r_0r0These three parameters can uniquely determine a circular surface.
  First, the circular coordinates ( x ′ , y ′ ) (x',y')(x,y )is ready; secondly, find the error equation, expand the equation of the circle to getx 2 + ( x 1 0 ′ ) 2 + y 2 + ( y 1 0 ′ ) 2 − 2 yy 1 0 ′ − ( r 0 ) 2 = 0 x^2+(x_1^{0'})^2+y^2+(y_1^{0'})^2-2yy_1^{0'}-(r_0)^2=0x2+(x10)2+y2+(y10)22 y y10(r0)2=0,即2 x ( x 1 0 ′ ) + 2 y ( y 1 0 ′ ) + ( r 0 ) 2 − ( x 1 0 ′ ) 2 − ( y 1 0 ′ ) 2 − ( x 2 + y 2 ) = 0 2x(x_1^{0'})+2y(y_1^{0'})+(r_0)^2-(x_1^{0'})^2-(y_1^{0'})^2- (x^2+y^2)=02x(x10)+2 y ( y10)+(r0)2(x10)2(y10)2(x2+y2)=0。可以设 x 1 0 ′ 、 y 1 0 ′ 、 ( r 0 ) 2 − ( x 1 0 ′ ) 2 − ( y 1 0 ′ ) 2 x_1^{0'}、y_1^{0'}、(r_0)^2-(x_1^{0'})^2-(y_1^{0'})^2 x10y10(r0)2(x10)2(y10)2 is the parameter to be requested. Then the error equation is:V = 2 x ( x 1 0 ′ ) + 2 y ( y 1 0 ′ ) + ( r 0 ) 2 − ( x 1 0 ′ ) 2 − ( y 1 0 ′ ) 2 − ( x 2 + y 2 ) V=2x(x_1^{0'})+2y(y_1^{0'})+(r_0)^2-(x_1^{0'})^2-(y_1^{0'} )^2-(x^2+y^2)V=2x(x10)+2 y ( y10)+(r0)2(x10)2(y10)2(x2+y2 ), written in the form of matrix:

insert image description here
insert image description here

  Assume that the obtained points of the circle are of equal precision, that is, of equal weight. According to the principle of linear least squares, that is, VTV = min V^TV=minVTV=min , get the position parameterXXX
X = ( B T B ) − 1 B T L (5) X=(B^TB)^{-1}B^TL\tag{5} X=(BTB)1BTL( 5 ) Calculate the initial value x 1 0 ′, y 1 0 ′ x_1^{0'},y_1^{0'}
  of a point on the axis of the cylinder in the new coordinate systemx10,y10And the initial value of the radius of the bottom circle r 0 r_0r0, and then according to the formula: x 1 0 ′ , y 1 0 ′ x_1^{0'},y_1^{0'}
insert image description here
  in the new coordinate systemx10,y10Convert to coordinates x 1 0 , y 1 0 x_1^{0},y_1^{0} in the old coordinate systemx10,y10. Whether in the new coordinate system or in the old coordinate system, the initial value of the radius of the bottom circle r 0 r_0r0No change. In order to minimize the error, the initial value of a point on the cylinder axis z 1 0 z_1^{0}z10The value of is set to the cylinder point zz in the original coordinate systemHalf of the sum of the maximum and minimum values ​​of z . Then calculate the initial values ​​of the four parametersx 1 0 , y 1 0 , z 1 0 , r 0 x_1^{0},y_1^{0},z_1^{0}, r_0x10,y10,z10,r0.
  (2) Establish an improved error equation to solve the parameter values. The equation of the cylindrical surface is:
( x − x 1 ) 2 + ( y − y 1 ) 2 + ( z − z 1 ) 2 − [ a ( x − x 1 ) + b ( y − y 1 ) + c ( z − z 1 ) ] 2 = r 2 (7) (x-x_1)^2+(y-y_1)^2+(z-z_1)^2-[a(x-x_1)+b(y-y_1)+ c(z-z_1)]^2=r^2\tag{7}(xx1)2+(yy1)2+(zz1)2[a(xx1)+b(yy1)+c(zz1)]2=r2(7)
传统方法,令
f = ( x − x 1 ) 2 + ( y − y 1 ) 2 + ( z − z 1 ) 2 − [ a ( x − x 1 ) + b ( y − y 1 ) + c ( z − z 1 ) ] 2 − r 2 (8) f=(x-x_1)^2+(y-y_1)^2+(z-z_1)^2-[a(x-x_1)+b(y-y_1)+c(z-z_1)]^2-r^2\tag{8} f=(xx1)2+(yy1)2+(zz1)2[a(xx1)+b(yy1)+c(zz1)]2r2( 8 )
tofff is linearized
insert image description here
  like this, the error equation can be listed as:
insert image description here
where
insert image description here
insert image description here
insert image description here
insert image description here
V n × 1 = B n × 7 X 7 × 1 − L n × 1 (23) V_{n\times 1}=B_{n\times 7}X_ {7\times 1}-L_{n\times 1}\tag{23}Vn×1=Bn×7X7×1Ln×1(23)

  Assume that the obtained points on the cylindrical surface are of equal precision, that is, the weights are equal. According to the principle of least squares, the parameter
X = ( BTB ) − 1 BTL (24) X=(B^TB)^{-1}B^TL\tag{24}X=(BTB)1BTL( 24 )
  This is a cyclic iterative process, and the initial value substituted in each iterative calculation process is equal to the previous initial value plus the obtainedXXThe correction value of X , whenXXThe iteration is launched when the value of X is small enough to meet the required accuracy.

2. Code implementation

insert image description here

Guess you like

Origin blog.csdn.net/qq_36686437/article/details/132351302