【Hand tear cylinder fitting】

Cylindrical fitting


1. Background

This article is used to solve the cylinder equation by measuring some three-dimensional points on the cylinder to establish an equation. It originated from the python library py_cylinder_fitting, met GTE, and finally "Least Squares Fitting of Data by Linear or Quadratic Structures"
.

Second, the expression of the cylinder

An infinite cylinder is specified by an axis containing the point C and passing through the point with a direction W of unit length. The radius of the cylinder is r > 0. Let the right-hand orthonormal set { U , V , W }, namely U , V , W are perpendicular to each other, the vector product is expressed as U × V = W , V × W = U , W × U = V , the unit vector U U = V · V = 1, then any point X can be expressed as where, R is a rotation matrix whose columns are U , V and W , where Y
Please add a picture description
is a column vector whose rows are y0, y1, and y2. on a cylinder, then

Please add a picture description

Please add a picture description

Get the radius
Please add a picture description
on a finite cylinderPlease add a picture description

2. Least squares loss function

Another set of input points is { X i }, and the cylindrical error function obtained based on formula 74 is

Please add a picture description
There are 6 parameters to be found in the formula, which are 1 radius r, 3 coordinates of point C and two direction vectors of W

To improve the robustness of numerical calculations and reduce numerical calculation errors, we subtract the sample mean from the sample, a method often referred to as "zero-meaning" or "centering". Next, let
Please add a picture description
the matrix P here represent the projection on the W plane, so P^2 = P only depends on the direction W. Radius ri^2 depends on center C and direction W. The error function is succinctly written as
Please add a picture description

3. Radius Equation

Setting the partial derivative of the error function with respect to the squared radius to zero, we have the constraint that the squared radius is the mean of the squared distances of the projections of Xi −
Please add a picture description
C Please add a picture description
on a plane containing C and having a normal W , depending on in C and W. _
If the following equation holds
Please add a picture description
thenPlease add a picture description

4. Central Equation

Set the partial derivatives of the error function with respect to the center to zero

Please add a picture description

Combined with Equation 78, we have the constraint

Please add a picture description
Multiplying Equation 80 by the sum of P X i , using Equation 81 givesPlease add a picture description

C + t W is the effective center of all t , and it is sufficient to compute a center with no component in the direction of w; that is, we can construct a point where C = P C . It is sufficient to solveequation (82) for P C written as a linear system A(P C ) = B /2

Please add a picture description
The projection matrix is ​​symmetrical P = P T , introduce P = P 2 , then X i T P X i = X i T P 2 X i = X i T P T P**X i**

The matrix A is singular because the projection matrix P is singular, so we cannot directly invert A to solve the equation. The items involved in the linear system only exist on the plane perpendicular to W, so in fact the linear system is reduced to an equation of two unknowns in the projective space, and it is solvable as long as the coefficient matrix is ​​invertible.

Choose U and V such that { U , V , W } is a right-hand orthonormal set; then P Xi = µi U + νi V and P C = k0 U + k1 V , where µi = U Xi , νi = V · Xi , k0 = U · P C and k1 = V · P C . Matrix A becomes, vector B becomes, and vector A ( P C ) becomes Please add a picture description

Please add a picture description
Please add a picture description
Equaling it to B/2 and grouping the coefficients of U and V yields a linear system. The coefficient matrix is ​​the covariance matrix of the projection of Please add a picture description
the samples on a plane perpendicular to W. Intuitively, this matrix is ​​invertible as long as the projections are not on a straight line. If the matrix is ​​singular (or numerically nearly singular), then the original samples are on a plane (or numerically nearly on one plane). They don't fit well with a cylinder, or rather they don't fit with a cylinder of infinite radius.

The solution of the matrix system of equation (87) Please add a picture description
is This gives the center of the cylinder P C = k0 U + k1 V ; substitute this for C in equation (74) .

While the solution seems to depend on the choice of U and V , it doesn't. Let W = (w0, w1, w2), and define a skew symmetric matrix
Please add a picture description
. According to the definition of skew symmetry, S T =− S . This matrix represents the cross-product operation for any vector ξ : S ξ = W × ξ . Since { U , V , W } is a right-orthonormal set, it follows that S U = V and S V =− U . It can also be shown that S = VU TUV T . Define the matrix A ˆ as In practice, this generates a 2×2 matrix that represents A Please add a picture description
A 2×2 matrix of helpers. It has the property that Please add a picture description
the locus of a matrix is ​​the sum of diagonal entries. Observe that tracking § = 2, since |W| 2 = 1. By tracing A ˆ A = δP , we get 2 δ = Trace( A ˆ A ). The cylinder center is obtained by multiplying A ( P C ) = B /2 by A ˆ , using the definition in Equation (83) and Equation (91),
Please add a picture description
where the last equation uses A ˆ P = A ˆ , Because S T P = S T . Equation ( 92) is independent of U and V , but dependent on W.

5. Direction Equation

Let the orientation be parameterized as W ( s ) = ( w 0 ( s ), w 1 ( s ), w 2 ( s )), where s is a two-dimensional parameter. For example, spherical coordinates are such a parameterization: W = (cos s 0 sin s 1 ,sin s 0 sin s 1 ,cos s 1 ), s 0 ∈ [0,2π) and s 1 ∈ [0,π/2 ], where w 2 ( s 0 , s 1 )≥0. The partial derivative of E is
Please add a picture description

Solving for ∂E/∂sk=0 in closed form is intractable. It is possible to generate a system of polynomial equations in the components of W, use elimination theory to get the polynomial in one variable, and then find its roots. This approach is often tedious and not numerically robust.

Alternatively, we can skip root finding for ∂E/∂sk=0 and instead feed equations (80) and (92) directly into the error function E/n = 1 n P ni=1(ri 2−r 2)2 , to get a non-negative function,

Please add a picture description

A numerical algorithm for locating the minimum of G can be employed. Alternatively, as shown in the example code, the domain of (s0, s1) can be divided into samples that are used to compute G. The sample that yields the smallest G value determines a plausible direction w. The center C and the squared radius r2 are inherent in the evaluation of G, so in the end we have a fitted cylinder. Computation of G is expensive for large numbers of samples: equation (94) involves a sum of terms, then squared, then another sum.

A few algebraic manipulations result in packed sums, allowing us to precompute sums and represent G(W) as rational polynomials of w components. This approach improves performance on the CPU. It also allows efficient implementation of massively parallel performance on the GPU - there is one GPU thread for one direction vector sampled from one hemisphere. The projection matrix P is determined by its upper triangular element p = (p00, p01, p02, p11, p12, p22). We can write below, where p is represented as a 6×1 vector, Please add a picture description
followed by 6×1 vectors ξi, µ, and δi (written as 6-tuples). As a 3-tuple, let W = (W, 1, sub)
Please add a picture description
Please add a picture description
The last equation is true, since P nj = 1 Xj = 0 implies that P nj = 1 XjµT = 0. Clarify Please add a picture description
where Q and F 0 are 3×3, F 1 is 3×6, and F 2 is 6×6; then the precomputation of Please add a picture description
the input sample {Y i } n i=1 is calculated sequentially as follows. These steps are independent of the direction vector W.

Please add a picture description

For each specified direction W, the following steps are performed in sequence.Please add a picture description

Six, the overall process

First find the direction vector W, then find the point P, and finally find r 2 ,

The preprocessing pseudocode is as follows:
Please add a picture description
the solution is as follows:
Please add a picture description

Guess you like

Origin blog.csdn.net/qq_37249793/article/details/131857674