First entry to SLAM (2) - use the least square method to find sub-pixel coordinates

introduction

In the process of stereo matching, we hope that the gap between matching points can be as small as possible. When we first entered SLAM-Harris corner detection , we accepted the use of Opencv to obtain Harris corners and deduced its mathematical formula in detail. The angular coordinates here are pixel coordinates corresponding to (integer, int). In order to obtain more accurate pixel coordinates, we need to obtain sub-pixel coordinates.

Reference

resource

Principle explanation

insert image description here
I believe you have seen this picture on various blogs, but most of them did not explain why.

answer

  • q, which is the sub-pixel to be requested, is very mysterious and unknown.
  • p i p_i pi, that is, the points around q, the coordinates are known, choose by yourself
  • ( p i − q ) (p_i-q) (piq ) , which is the first vector
  • p i p_ipiThe gray level at G i G_iGi, which is the second vector

Let's look at the picture above again,

  1. p 0 p_0 p0In this case, it is located in a white area, at this time, the gradient is 0
  2. p 1 p_1 p1In this case, it is located at the edge, where black and white intersect. At this time, the gradient is not 0, but, with p 1 − q p_1-qp1q is vertical!

All for a standard corner, will lead to:
G i ∗ ( pi − q ) = 0 G_i*(p_i-q)=0Gi(piq)=0

least square method

For the above equation, we actually took a lot of pi p_ipi, then we cannot find an exact point qqIf q satisfies all points, it is equivalent to an equation with no solution, sohow to solve an equation with no solution?
Let's convert the above formula:
G i ∗ q = G i ∗ pi G_i*q=G_i*p_iGiq=Gipi
我们令 G i : A q : [ x y ] G i ∗ p i : B G_i: A\\q:\left[ \begin{array}{c} x\\ y\\ \end{array} \right] \\ G_i*p_i:B GiAq[xy]Gipi: B
Then we want to solve the equation:
A [ xy ] = BA\left[ \begin{array}{c} x\\ y\\ \end{array} \right] =BA[xy]=B
For better visualization, we giveAAA andBBB Concrete assignment
We assume A = [ 1 1 0 1 2 1 ] A=\left[ \begin{array}{c} 1 \ 1\\ 0 \ 1\\ 2 \ 1\\ \end{array} \ right]A= 1 10 12 1
B = [ 2 2 3 ] B=\left[ \begin{array}{c} 2\\ 2\\ 3\\ \end{array} \right] B= 223

At this time, the equation we want to solve is
[ 1 1 0 1 2 1 ] ∗ [ xy ] = [ 2 2 3 ] \left[ \begin{array}{c} 1 \ 1\\ 0 \ 1\\ 2 \ 1 \\ \end{array} \right]*\left[ \begin{array}{c} x\\ y\\ \end{array} \right] =\left[ \begin{array}{c} 2\ \ 2\\ 3\\ \end{array} \right] 11 01 21  [xy]= 223
The default value is
[ 1 0 2 ] × x + [ 1 1 1 ] × y = [ 2 2 3 ] \left[\begin{array}{l} 1 \\ 0 \\ 2 \end{array}\ right] \times x+\left[\begin{array}{l} 1 \\ 1 \\ 1 \end{array}\right] \times y=\left[\begin{array}{l} 2 \\ \\3\end{array}\right] 102 ×x+ 111 ×y= 223
我们定义 a 1 = [ 1 0 2 ] a 2 = [ 1 1 1 ] b = [ 2 2 3 ] a_1=\left[\begin{array}{l} 1 \\ 0 \\ 2 \end{array}\right] a_2=\left[\begin{array}{l} 1 \\ 1 \\ 1 \end{array}\right] b=\left[\begin{array}{l} 2 \\ 2 \\ 3 \end{array}\right] a1= 102 a2= 111 b= 223
So in fact we can put a 1 a_1a1a2 a_2a2As a basis, our problem now is to find a set of x, yx,yx,y can be closest toBBB , drawn on the graph as shown in the figure below.
insert image description here
According to the normal solution, it is impossible for us to find a set of a 1 a_1a1a2 a_2a2The linear combination of , so that the combined vector is just equal to BBB , since anya 1 a_1a1a2 a_2a2The linear combination can only be in a 1 , a 2 a_1,a_2a1,a2on the plane where it is located.
Since no perfect solution can be found, we can only find the closest solution, and this solution is BBB ina 1 , a 2 a_1,a_2a1,a2Projected onto a plane, the foot is the error between the endpoint closest to the solution and the exact solution. As shown below.insert image description here

Now we just want to solve A [ x ^ y ^ ] = PA\left[ \begin{array}{c} \hat{x}\\ \hat{y}\\ \end{array} \right]=PA[x^y^]=P , and this must have a solution.
Also, we know thatPPP andbbThe error between b is: e = B − P = B − A [ x ^ y ^ ] e=BP=BA \left[ \begin{array}{c} \hat{x}\\ \hat{y} \\ \end{array} \right]e=BP=BA[x^y^]
to makebbb andPPThe gap between P is the smallest, then e must be perpendicular to a 1 , a 2 a_1,a_2a1,a2Composed of the plane S, that is, to be perpendicular to the intersection vector a 1 , a 2 a_1,a_2a1,a2, so we can draw the requirements e ∗ a 1 = 0 and e ∗ a 2 = 0 e*a_1=0 and e*a_2=0ea1=0 and ea2=0 , represented by matrix:
AT e = 0 A^{T} e=0AThat's it=
Enter 0 eee可得:
A T ( B − A [ x ^ y ^ ] ) = 0 A T A [ x ^ y ^ ] = A T B [ x ^ y ^ ] = ( A T A ) − 1 A T B A^{T}(B-A\left[ \begin{array}{c} \hat{x}\\ \hat{y}\\ \end{array} \right])=0 \\ A^{T} A \left[ \begin{array}{c} \hat{x}\\ \hat{y}\\ \end{array} \right]=A^{T} B\\ \left[ \begin{array}{c} \hat{x}\\ \hat{y}\\ \end{array} \right]=\left(A^{T} A\right)^{-1} A^{T} B AT(BA[x^y^])=0AT A[x^y^]=ATB[x^y^]=(AT A)1AT B
So far, we have found an approximate solutionx ^ \hat{x}x^

Pull back to the original formula

G i ∗ q = G i ∗ p i G_i*q=G_i*p_i Giq=Gipi
我们令 G i : A q : [ x y ] G i ∗ p i : B G_i: A\\q:\left[ \begin{array}{c} x\\ y\\ \end{array} \right] \\ G_i*p_i:B GiAq[xy]GipiB

From the above definitions and formulas, it can be deduced that:
q = ( G i TG i ) − 1 ( G i T ) G ipi = ( G i TG i ) − 1 ( G i TG i ) piq=(G_i^TG_i)^{ -1}(G_i^T)G_ip_i\\ =(G_i^TG_i)^{-1}(G_i^TG_i)p_iq=(GiTGi)1(GiT)Gipi=(GiTGi)1(GiTGi)pi
Then at this time, we have been able to obtain the coordinates of a point through multiple points.

weight introduction

But is this necessarily accurate? We use multiple points for calculation. The original intention is to be more accurate, but the distance from each point to the center is different, so the compensation can be treated equally. To introduce weights, Gaussian weights are generally used. Suppose pi p_ipiwhere the weight is wi w_iwi, the above formula is further revised as:
q = ( G i TG iwi ) − 1 ( G i TG iwi ) piq=(G_i^TG_iw_i)^{-1}(G_i^TG_iw_i)p_iq=(GiTGiwi)1(GiTGiwi)pi

Iteration and termination conditions

After solving once, you can get a sub-pixel point q ( qx , qy ) q(q_x,q_y)q(qx,qy) . ifqqq is the center point, again:
1. Select the window to get a new set ofpi p_ipi
2. To pi p_ipiFind the gradient
3. Solve it with the least square method
to get the new point qi q_iqi.
With so many iterations, a series of sub-pixel points q 2 , q 3 , q 4 , . . . . qn q_2,q_3,q_4,....q_n will be obtainedq2,q3,q4,....qn. So when does it end?
OpenCV's method is:
specify the number of iterations, for example, after 10 iterations, no calculation is performed, and the optimal solution is considered.
Specify the result precision, for example, set ϵ = 1.0 e − 6 \epsilon=1.0e^{-6}ϵ=1.0 e6 ,andqn − qn − 1 < = ϵ q_n-q_{n-1}<=\epsilonqnqn1<=ϵ , that is, considerqn q_nqnis the optimal solution.

code analysis

First entry to SLAM (3) - cornerSubPixel source code analysis

Guess you like

Origin blog.csdn.net/REstrat/article/details/127031411