First entry to SLAM (1) - Harris corner detection

Reference

https://blog.csdn.net/lbaihao/article/details/78919700
https://docs.opencv.org/4.x/dc/d0d/tutorial_py_features_harris.html

1 Mathematical derivation

Harris corner detection principle:

The Harris algorithm uses the autocorrelation of the image gray in the window to determine the corner point, sets a window, and moves in the image, and calculates the autocorrelation coefficient of the image of the area where the window is located before and after the movement.

insert image description here
The autocorrelation function is calculated as follows, (x, y) is the center position of the window, w(u, v) is the weight (generally a Gaussian function), L represents the window, and (u, v) represents the image position in the window.
c ( x , y , Δ x , Δ y ) = ∑ ( u , v ) ∈ L w ( u , v ) ⋅ [ I ( u , v ) − I ( u + Δ x , v + Δ y ) ] 2 \mathrm{c}(\mathrm{x}, \mathrm{y}, \Delta \mathrm{x}, \Delta \mathrm{y})=\sum_{(\mathrm{u}, \mathrm{v} ) \in \mathrm{L}} \mathrm{w}(\mathrm{u}, \mathrm{v}) \cdot[\mathrm{I}(\mathrm{u}, \mathrm{v})-\ mathrm{I}(\mathrm{u}+\Delta \mathrm{x}, \mathrm{v}+\Delta \mathrm{y})]^{2}c(x,y,Δx , _y ) _=(u,v)Lw(u,v)[I(u,v)I(u+Δx , _v+y ) ] _2.
Let the infinitesimal function I of
I ( u + Δ x , v + Δ y ) = I ( u , v ) + I x ( u , v ) ⋅ Δ x + I y ( u , v ) . ⋅ Δ y + ⋯ + R n(u, v) ≈ I(u, v) + I x(u, v) ⋅ Δ x + I y(u, v) ⋅ Δ y {\color{Brown} \begin {array}{c} \mathrm{I}(\mathrm{u}+\Delta \mathrm{x}, \mathrm{v}+\Delta\mathrm{y})=\mathrm{I}(\mathrm{ u}, \mathrm{v})+\mathrm{I}_{\mathrm{x}}(\mathrm{u}, \mathrm{v}) \cdot \Delta \mathrm{x}+\mathrm{I }_{\mathrm{y}}(\mathrm{u}, \mathrm{v}) \cdot \Delta \mathrm{y}+\cdots+\mathrm{R}_{\mathrm{n}}(\mathrm {u}, \mathrm{v}) \\\approx \mathrm{I}(\mathrm{u}, \mathrm{v})+\mathrm{I}_{\mathrm{x}}(\mathrm{ u}, \mathrm{v}) \cdot \Delta \mathrm{x}+\mathrm{I}_{\mathrm{y}}(\mathrm{u}, \mathrm{v}) \cdot \Delta\ mathrm{y}\end{array}}I(u+Δx , _v+y ) _=I(u,v)+Ix(u,v)Δx _+Iy(u,v)y _++Rn(u,v)I(u,v)+Ix(u,v)Δx _+Iy(u,v)y _
Substituting into the autocorrelation function, we get:c ( x , y , Δ x , Δ y ) = ∑ ( u , v ) ϵ L w ( u , v ) ⋅ [ I ( u , v ) − I ( u + Δ x , v + Δ y ) ≈ ∑ ( u , v ) ∈ L w ( u , v ) ⋅ { I ( u , v ) − [ I ( u , v ) + I x ( u , v ) ⋅ Δ x + I y ( u , v ) ⋅ Δ y ] } 2 = ∑ ( u , v ) ∈ L w ( u , v ) [ I x ( u , v ) ⋅ Δ x + I y ( u , v ) ⋅ Δ y ] 2 \begin{aligned} \mathrm{c}(\mathrm{x}, \mathrm{y}, \Delta \mathrm{x}, \Delta\mathrm{y}) &=\sum_{(\mathrm{u}, \mathrm{v }) \epsilon \mathrm{L}} \mathrm{w}(\mathrm{u}, \mathrm{v}) \cdot[\mathrm{I}(\mathrm{u}, \mathrm{v})- \mathrm{I}(\mathrm{u}+\Delta \mathrm{x}, \mathrm{v}+\Delta\mathrm{y})]^{2} \\ & \approx \sum_{(\mathrm {u}, \mathrm{v}) \in \mathrm{L}} \mathrm{w}(\mathrm{u}, \mathrm{v}) \cdot\left\{\mathrm{I}(\mathrm {u}, \mathrm{v})-\left[\mathrm{I}(\mathrm{u}, \mathrm{v})+\mathrm{I}_{\mathrm{x}}(\mathrm{ u},\mathrm{v}) \cdot \Delta \mathrm{x}+\mathrm{I}_{\mathrm{y}}(\mathrm{u}, \mathrm{v}) \cdot \Delta \mathrm{y }\right]\right\}^{2} \\ &=\sum_{(\maths{u}, \maths{v}) \in \maths{L}} \maths{w}(\maths{u }, \mathrm{v})\left[\mathrm{I}_{\mathrm{x}}(\mathrm{u}, \mathrm{v}) \cdot \Delta \mathrm{x}+\mathrm{ I}_{\mathrm{y}}(\mathrm{u}, \mathrm{v}) \cdot \Delta \mathrm{y}\right]^{2} \end{aligned}c(x,y,Δx , _y ) _=( u , v ) ϵL _w(u,v)[I(u,v)I(u+Δx , _v+y ) ] _2(u,v)Lw(u,v){ I(u,v)[I(u,v)+Ix(u,v)Δx _+Iy(u,v)y ] } _2=(u,v)Lw(u,v)[Ix(u,v)Δx _+Iy(u,v)y ] _2
Expanding the square term and writing it in matrix form gives:
[ I x ( u , v ) ⋅ Δ x + I y ( u , v ) ⋅ Δ y ] 2 = I 2 x ( u , v ) Δ x 2 + I 2 y ( u , v ) Δ y 2 + 2 I x ( u , v ) I y ( u , v ) Δ x Δ y = I 2 x ( u , v ) Δ x 2 + I 2 y ( u , v ) Δ y 2 + 2 I x ( u , v ) I y ( u , v ) Δ x Δ y = [ I x 2 ( u , v ) Δ x + I x ( u , v ) I y ( u , v ) Δ y I y 2 ( u , v ) Δ y + I x ( u , v ) I y ( u , v ) Δ x ] ⋅ [ Δ x Δ y ] = [ Δ x Δ y ] ⋅ [ I x 2 ( u , v ) I x ( u , v ) I y ( u , v ) I x ( u , v ) I y ( u , v ) I y 2 ( u , v ) ] ⋅ [ Δ x Δ y ] \begin{array}{l} {\left[I_{x}(u, v) \cdot \Delta x+I_{y}(u, v) \cdot \Delta y\right]^{2}} \\ =I^{2}{ }_{x}(u, v) \Delta x^{2}+I^{2}{ }_{y}(u, v) \Delta y^{2}+2 I_{x}(u, v) I_{y}(u, v) \Delta x \Delta y \\ =I^{2}{ }_{x}(u, v) \Delta x^{2}+I^{2}{ }_{y}(u, v) \Delta y^{2}+2 I_{x}(u, v) I_{y}(u, v) \Delta x \Delta y \\ =\left[\begin{array}{lc} I_{x}^{2}(u, v) \Delta x+I_{x}(u, v) I_{y}(u, v) \Delta y & I_{y}^{2}(u, v) \Delta y+I_{x}(u, v) I_{y}(u, v) \Delta x \end{array}\right] \cdot\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right] \\ =\left[\begin{array}{ll} \Delta x & \Delta y \end{array}\right] \cdot\left[\begin{array}{cc} I_{x}^{2}(u, v) & I_{x}(u, v) I_{y}(u, v) \\ I_{x}(u, v) I_{y}(u, v) & I_{y}^{2}(u, v) \end{array}\right] \cdot\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right] \end{array} [Ix(u,v)Δx _+Iy(u,v)y ] _2=I2x(u,v)Δx2+I2y(u,v)Δy2+2 Ix(u,v ) Iy(u,v)ΔxΔy=I2x(u,v)Δx2+I2y(u,v)Δy2+2 Ix(u,v ) Iy(u,v)ΔxΔy=[Ix2(u,v)Δx+Ix(u,v ) Iy(u,v)ΔyIy2(u,v)Δy+Ix(u,v ) Iy(u,v)Δx][Δx _y _]=[Δ xy _][Ix2(u,v)Ix(u,v ) Iy(u,v)Ix(u,v ) Iy(u,v)Iy2(u,v)][Δx _y _]

Substituting into the autocorrelation function, we get:
c ( x , y , Δ x , Δ y ) ≈ ∑ ( u , v ) ∈ L w ( x , y ) [ I x ( u , v ) ⋅ Δ x + I y ( u , v ) ⋅ Δ y ] 2 = ∑ ( u , v ) ∈ L w ( x , y ) [ I 2 x ( u , v ) Δ x 2 + I 2 y ( u , v ) Δ y 2 + 2 I x ( u , v ) I y ( u , v ) Δ x Δ y ] = ∑ ( u , v ) ∈ L w ( x , y ) [ Δ x Δ y ] [ I x 2 ( u , v ) I x ( u , v ) I y ( u , v ) I x ( u , v ) I y ( u , v ) I y 2 ( u , v ) ] [ Δ x Δ y ] = [ Δ x Δ y ] { ∑ ( u , v ) ∈ L w ( x , y ) [ I x 2 ( u , v ) I x ( u , v ) I y ( u , v ) I x ( u , v ) I y ( u , v ) I y 2 ( u , v ) ] } [ Δ x Δ y ] = [ Δ x Δ y ] [ ∑ ( u , v ) ϵ LI x 2 ( u , v ) ∑ ( u , v ) ϵ LI x ( u , v ) I y ( u , v ) ∑ ( u , v ) ∈ LI x ( u , v ) I y ( u , v ) ∑ ( u , v ) ϵ LI y 2 ( u , v ) ] [ Δ x Δ y ] = [ Δ x Δ y ] ⋅ [ ACCB ] ⋅ [ Δ x Δ y ] \begin{array}{l} c(x, y, \Delta x, \Delta y) \approx \sum_{(u, v) \in L} w(x, y)\left[I_{x}(u,v) \cdot \Delta x+I_{y}(u, v) \cdot \Delta y\right]^{2}\\ \\ =\sum_{(u, v) \in L} w(x, y)\left[I^{2}{ }_{x}(u, v) \Delta x^{2}+I^{2}{ }_{y}(u, v) \Delta y^{2}\right. \left.+2 I_{x}(u, v) I_{y}(u, v) \Delta x \Delta y\right]\\ \\ =\sum_{(u, v) \in L} w(x, y)\left[\begin{array}{ll} \Delta x & \Delta y \end{array}\right]\left[\begin{array}{cc} I_{x}^{2}(u, v) & I_{x}(u, v) I_{y}(u, v) \\ I_{x}(u, v) I_{y}(u, v) & I_{y}^{2}(u, v) \end{array}\right]\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right]\\ \\ =\left[\begin{array}{ll} \Delta x & \Delta y \end{array}\right]\left\{\sum_{(u, v) \in L} w(x, y)\left[\begin{array}{cc} I_{x}^{2}(u, v) & I_{x}(u, v) I_{y}(u, v) \\ I_{x}(u, v) I_{y}(u, v) & I_{y}^{2}(u,v) \end{array}\right]\right\}\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right]\\ \\ =\left[\begin{array}{ll} \Delta x & \Delta y \end{array}\right]\left[\begin{array}{cc} \sum_{(u, v) \epsilon L} I_{x}^{2}(u, v) & \sum_{(u, v) \epsilon L} I_{x}(u, v) I_{y}(u, v) \\ \sum_{(u, v) \in L} I_{x}(u, v) I_{y}(u, v) & \sum_{(u, v) \epsilon L} I_{y}^{2}(u, v) \end{array}\right]\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right]\\ \\ =\left[\begin{array}{ll} \Delta x & \Delta y \end{array}\right] \cdot\left[\begin{array}{ll} A & C \\ C & B \end{array}\right] \cdot\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right] \end{array}v) \\ \sum_{(u, v) \in L} I_{x}(u, v) I_{y}(u, v) & \sum_{(u, v) \epsilon L} I_{y}^{2}(u, v) \end{array}\right]\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right]\\ \\ =\left[\begin{array}{ll} \Delta x & \Delta y \end{array}\right] \cdot\left[\begin{array}{ll} A & C \\ C & B \end{array}\right] \cdot\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right] \end{array}v) \\ \sum_{(u, v) \in L} I_{x}(u, v) I_{y}(u, v) & \sum_{(u, v) \epsilon L} I_{y}^{2}(u, v) \end{array}\right]\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right]\\ \\ =\left[\begin{array}{ll} \Delta x & \Delta y \end{array}\right] \cdot\left[\begin{array}{ll} A & C \\ C & B \end{array}\right] \cdot\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right] \end{array}c(x,y,Δx , _y ) _(u,v)Lw(x,y)[Ix(u,v)Δx _+Iy(u,v)y ] _2=(u,v)Lw(x,y)[I2x(u,v)Δx2+I2y(u,v)Δy2+ 2 Ix(u,v ) Iy(u,v)ΔxΔy]=(u,v)Lw(x,y)[Δ xy _][Ix2(u,v)Ix(u,v ) Iy(u,v)Ix(u,v ) Iy(u,v)Iy2(u,v)][Δx _y _]=[Δ xy _]{ (u,v)Lw(x,y)[Ix2(u,v)Ix(u,v ) Iy(u,v)Ix(u,v ) Iy(u,v)Iy2(u,v)]}[Δx _y _]=[Δ xy _][( u , v ) ϵL _Ix2(u,v)(u,v)LIx(u,v ) Iy(u,v)( u , v ) ϵL _Ix(u,v ) Iy(u,v)( u , v ) ϵL _Iy2(u,v)][Δx _y _]=[Δ xy _][ACCB][Δx _y _]

Determine the dependent variable:
c ( x , y , Δ x , Δ y ) = [ Δ x Δ y ] ⋅ M ⋅ [ Δ x Δ y ] {\color{Red} \begin{array}{l} c (x, y, \Delta x, \Delta y) =\left[\begin{array}{ll} \Delta x & \Delta y \end{array}\right] \cdot M \cdot\left[\begin {array}{l} \Delta x\\\Delta y \end{array}\right] \end{array}}c(x,y,Δx , _y ) _=[Δ xy _]M[Δx _y _]
M = ∑ x , y w ( x , y ) [ I x I x I x I y I x I y I y I y ] M = \sum_{x,y} w(x,y) \begin{bmatrix}I_x I_x & I_x I_y \\ I_x I_y & I_y I_y \end{bmatrix} M=x,yw(x,y)[IxIxIxIyIxIyIyIy]
ThisriIx I_xIxSum I y I_yIyare the image derivatives of the image in the x and y directions (they can be easily obtained using cv2.Sobel())

The equation of an ellipse

insert image description here
x 2 a 2 + y 2 b 2 = 1 \frac{x^{2}}{a^{2}}+\frac{y^{2}}{b^{2}}=1 a2x2+b2y2=1
矩阵表达:
[ x y ] [ 1 a 2 0 0 1 b 2 ] [ x y ] = 1 \left[\begin{array}{ll} x & y \end{array}\right]\left[\begin{array}{ll} \frac{1}{a^{2}} & 0 \\ 0 & \frac{1}{b^{2}} \end{array}\right]\left[\begin{array}{l} x \\ y \end{array}\right]=1 [xy][a2100b21][xy]=1
An nxn matrix can solve its eigenvalues. We solve the eigenvalues ​​of the above-mentioned intermediate matrix, and we can get
{ λ 1 = 1 a 2 λ 2 = 1 b 2 { a = 1 λ 1 b = 1 λ 2 \left\ {\begin{array} { l } { \lambda _ { 1 } = \frac { 1 } { a ^ { 2 } } } \\ { \lambda _ { 2 } = \frac { 1 } { b ^ { 2 } } } \end{array} \quad \left\{\begin{array}{l} a=\frac{1}{\sqrt{\lambda_{1}}} \\ b=\frac{1}{ \sqrt{\lambda_{2}}} \end{array}\right.\right.{ l1=a21l2=b21{ a=l1 1b=l2 1
The larger the eigenvalue, the shorter the axis radius.

M matrix

c ( x , y , Δ x , Δ y ) = [ Δ x Δ y ] ⋅ M ⋅ [ Δ x Δ y ] {\color{Red} \begin{array}{l} c(x, y, \Delta x, \Delta y) =\left[\begin{array}{ll} \Delta x & \Delta y \end{array}\right] \cdot M \cdot\left[\begin{array}{l} \Delta x \\ \Delta y \end{array}\right] \end{array}} c(x,y,Δx , _y ) _=[Δ xy _]M[Δx _y _]
M = ∑ x , y w ( x , y ) [ I x I x I x I y I x I y I y I y ] M = \sum_{x,y} w(x,y) \begin{bmatrix}I_x I_x & I_x I_y \\ I_x I_y & I_y I_y \end{bmatrix} M=x,yw(x,y)[IxIxIxIyIxIyIyIy]
We found that the autocorrelation function is actually a matrix representation of an ellipse.
Then, we can solveMMThe eigenvalues ​​of the M matrix getλ 1 \lambda_1l1and λ 2 \lambda_2l2

  1. λ 1 \lambda_1l1and λ 2 \lambda_2l2are very small, and the axis lengths are large, which means that the point is in a flat area.
  2. λ 1 \lambda_1l1and λ 2 \lambda_2l2They are all very large and relatively close to each other, and their axis lengths are very small, which can be considered as corner points.
  3. λ 1 \lambda_1l1and λ 2 \lambda_2l2One of them is very small, one is very large, one axis is long, and the other axis is short, which can be considered as the edge area at this time.
    insert image description here

If we follow the above method, it is obvious that we need to calculate a lot of things for each point, and the speed is too slow, so an empirical formula is proposed.
R = det ( M ) − k ( trace ( M ) ) 2 R=det(M)-k(trace(M))^2R=d e t ( M )k(trace(M))2
of which,

  • der ( M ) = λ 1 ⋅ λ 2 = AB − C 2 der(M)=\lambda_1\cdot\lambda_2=AB-C^2he ( M ) _=l1l2=ABC2
  • t r a c e ( M ) = λ 1 + λ 2 = A + B trace(M)=\lambda_1+\lambda_2=A+B trace(M)=l1+l2=A+B
  • λ 1 \lambda_1l1and λ 2 \lambda_2l2is MMEigenvalues ​​of M
  • k k k is an empirical value, generally 0.02-0.04

2 Opencv implementation

2.1 Function cv2.cornerHarris() [just a rough understanding]

2.1.1 Parameters

  • img: input image, should be float32 grayscale image
  • blockSize: the size of the area considered by corner detection
  • ksize: Aperture parameter using Sobel derivative
  • k: Harris detection score RRkkin Rparameter value of k
# -*-coding:utf-8-*-
import numpy as np
import cv2

filename = '1.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# opencv需要输入是float32类型的
gray = np.float32(gray)
# 返回值是一个跟图片同样大小的矩阵分数图
dst = cv2.cornerHarris(gray, 5, 3, 0.04)

# 通过图像自身的分数图的大小关系,得到哪些像素点应该是角点,
# 这些像素点的值都变为[0,0,255],也就是红色。
img[dst>0.01*dst.max()]=[0,0,255]
cv2.imshow('dst', img)
cv2.waitKey(0)

insert image description hereinsert image description here

2.2 cv2.goodFeaturesToTrack()

The problem with the above API is that what is returned is not the position coordinates of the corner points, but a score map, and we directly process the source image with the score map.
cv2.goodFeaturesToTrack(), not only supports Harris corner detection, but also supports corner detection of Shi Tomasi algorithm.

2.2.1 Parameters

  • gray_src: 8-bit or 32-bit single-channel grayscale image
  • corners: position point vector, which saves the coordinates of the detected corner points
  • max_corners: defined as the maximum number of corner points that can be detected;
  • qualityLevel: The quality level of the detected corner points, and the points whose eigenvalues ​​are less than the maximum eigenvalue of qualityLevel will be discarded;
  • minDistance: the minimum distance between two corner points, in pixels;
  • mask: Specify the detection area, if the entire image is detected, the mask is empty
  • blockSize: Calculate the window size of the autocorrelation function covariance matrix;
  • useHarrisDetector: Whether to use Harris corner detection, if false, use Shi-Tomasi operator
  • k: The hyperparameter of Harris corner detection, generally 0.02 to 0.04, userHarrisDetector is false, it will not work.
# -*-coding:utf-8-*-
import numpy as np
import cv2

filename = '1.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)


corners = cv2.goodFeaturesToTrack(gray, maxCorners=1000, qualityLevel=0.01, minDistance=10)
corners = np.int0(corners)
for i in corners:
    x,y = i.ravel()
    cv2.circle(img, (x, y), 2, (0, 0, 255), -1)

cv2.imshow('out.jpg', img)

cv2.waitKey(0)


insert image description hereinsert image description here

sub-pixel

cv2.cornerSubPix()

parameter:

  • image: input image
  • corners: Enter the initial coordinates of the corner points
  • winSize: half of the side length of the search window, if winSize=(5,5), a search window with a size of (5 2+1) (5 2+1)=11 11 will be used
  • zeroZone: half of the side length of the dead region in the middle of the search area. If the value is set to (-1, -1), it means that there is no such area
  • cirteria: The termination condition of the iterative process of corner point precision, generally use criteria = ( cv 2. Term C riteria EPS + cv 2. Term C riteria MAXITER , 100 , 0.001 ) criteria = (cv2.TermCriteria_EPS + cv2.TermCriteria_MAX_ITER, 100, 0.001)criteria=(cv2.TermCriteriaEPS+cv2.TermCriteriaMAXITER,100,0.001)

Guess you like

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