当前位置:网站首页>[300 opencv routines] 240. Shi Tomas corner detection in opencv
[300 opencv routines] 240. Shi Tomas corner detection in opencv
2022-07-26 17:27:00 【YouCans】
『youcans Of OpenCV routine 300 piece - General catalogue 』
【youcans Of OpenCV routine 300 piece 】240. OpenCV Medium Shi-Tomas Corner detection
An angle is a rapid change in the direction of a straight line . A corner is usually defined as the intersection of two edges , In other words, the neighborhood of the corner should have the boundaries of two different regions in different directions .
Angle is a highly effective feature . Corner detection (Corner Detection) It is widely used in motion detection 、 Image matching 、 Video tracking 、 3D reconstruction and target recognition .
Shi-Tomas The algorithm is right Harris Improvement of corner detection algorithm , General comparison Harris The algorithm gets better corners .
6.1 Harris - Stephens angle detector (Harris)
Harris The principle of corner detection algorithm is , Move on the image through the detection window , Calculate the gray changes of pixels in the window before and after moving . A corner is the intersection of two sides , Its feature is that moving the detection window in any direction will lead to significant changes in gray .
For point ( x , y ) (x,y) (x,y), Make w ( x , y ) w(x,y) w(x,y) Rectangular detection window or Gaussian detection window , I ( x , y ) I(x,y) I(x,y) Is the detection window at point ( x , y ) (x,y) (x,y) Gray value , I ( x + u , y + v ) I(x+u,y+v) I(x+u,y+v) Is the detection window sliding ( u , v ) (u,v) (u,v) Gray value after distance .
Calculation window w ( x , y ) w(x,y) w(x,y) slide ( u , v ) (u,v) (u,v) Gray change after distance :
E ( u , v ) = ∑ x , y w ( x , y ) [ I ( x + u , y + v ) − I ( x , y ) ] 2 E(u,v) = \sum _{x,y} w(x,y) [I(x+u, y+v) - I(x,y)]^2 E(u,v)=x,y∑w(x,y)[I(x+u,y+v)−I(x,y)]2
For very small displacements ( u , v ) (u,v) (u,v), Use first-order Taylor expansion for simplification :
E ( u , v ) = ∑ x , y w ( x , y ) [ I ( x , y ) + u I x + v I y + O ( u 2 , v 2 ) − I ( x , y ) ] 2 ≈ ∑ x , y w ( x , y ) [ u 2 I x 2 + 2 u v I x I y + v 2 I y 2 ] ≈ [ u , v ] ∑ x , y w ( x , y ) [ I x I x I x I y I x I y I y I y ] [ u v ] \begin{aligned}E(u,v) = &\sum_{x,y} w(x,y) [I(x,y) + uI_x + vI_y +O(u^2,v^2)- I(x,y)]^2 \\\approx & \sum_{x,y} w(x,y) [u^2 I_x^2 + 2uv I_x I_y + v^2 I_y^2] \\\approx & [u,v] \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}\begin{bmatrix}u\\v\end{bmatrix}\end{aligned} E(u,v)=≈≈x,y∑w(x,y)[I(x,y)+uIx+vIy+O(u2,v2)−I(x,y)]2x,y∑w(x,y)[u2Ix2+2uvIxIy+v2Iy2][u,v]x,y∑w(x,y)[IxIxIxIyIxIyIyIy][uv]
I x , I y I_x,I_y Ix,Iy Respectively in x, y Derivative of direction , Can be Sobel Gradient operator finds .
remember M Is the covariance matrix of the gradient :
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,y∑w(x,y)[IxIxIxIyIxIyIyIy]
By judging the size of two eigenvalues in the geometric model , To determine the attributes of pixels . matrix M yes I x , I y I_x,I_y Ix,Iy The quadratic function of , It can be expressed as an elliptical shape , The major and minor semiaxes of an ellipse are determined by the matrix M The eigenvalues of the λ 1 , λ 2 \lambda _1,\lambda _2 λ1,λ2 decision , The direction is determined by the eigenvector .
Define the corner response function R:
R = d e t ( M ) − k [ t r a c e ( M ) ] 2 d e t ( M ) = λ 1 ∗ λ 2 t r a c e ( M ) = λ 1 + λ 2 \begin{aligned}R =& det(M) - k [trace(M)]^2 \\&det(M) = \lambda _1 * \lambda _2 \\&trace(M) = \lambda _1 + \lambda _2\end{aligned} R=det(M)−k[trace(M)]2det(M)=λ1∗λ2trace(M)=λ1+λ2
k Is the adjustment parameter ( Usually take 0.04~0.06).
Corner response function R Only with matrix M The eigenvalues of the λ 1 , λ 2 \lambda _1,\lambda _2 λ1,λ2 of , It can be used to judge whether the area is a corner 、 The edge is still flat :
- λ 1 , λ 2 \lambda _1,\lambda _2 λ1,λ2 More hours , ∣ R ∣ |R| ∣R∣ smaller , That is, the gray level in all directions is basically unchanged , It indicates that the detector is in a flat area ;
- λ 1 > > λ 2 \lambda _1 >> \lambda _2 λ1>>λ2 or $ \lambda _2 >> \lambda _1$ when , R < 0 R <0 R<0 , That is, the gray changes in a certain direction , But it does not change in its orthogonal direction , It indicates that the detector is in the edge area ;
- λ 1 , λ 2 \lambda _1,\lambda _2 λ1,λ2 Large and λ 1 ∼ λ 2 \lambda _1 \sim \lambda _2 λ1∼λ2 when , ∣ R ∣ |R| ∣R∣ It's big , That is, the grayscale changes significantly in all directions , Indicates that the detector contains corners ( Or isolated point .
6.3 OpenCV Medium Shi-Tomas Angle detector
Shi-Tomas The algorithm is right Harris Improvement of corner detection algorithm , General comparison Harris The algorithm gets better corners .
Harris The corner response function in the algorithm is :
R = λ 1 λ 2 − k ( λ 1 + λ 2 ) 2 R =\lambda _1 \lambda _2 - k (\lambda _1 + \lambda _2)^2 R=λ1λ2−k(λ1+λ2)2
Shi-Tomas The algorithm modifies the corner response function to :
R = m i n ( λ 1 , λ 2 ) R = min (\lambda _1 , \lambda _2 ) R=min(λ1,λ2)
Only when M The eigenvalues of a matrix $ \lambda _1,\lambda _2$ Are greater than the minimum λ m i n \lambda {min} λmin when , Just think of it as a corner .
OpenCV Provided in Shi-Tomas Corner detection function cv.goodFeaturesToTrack() .
Function description :
cv.goodFeaturesToTrack(image, maxCorners, qualityLevel, minDistance[, corners=None, mask=Mat(), blockSize=3, useHarrisDetector=false, k=0.04]) → corners
function cv.goodFeaturesToTrack adopt Shi-Tomasi Methods to find the most prominent in the image N Corner points .
Parameter description :
- src: The input image , Single channel 8 Bit image or 32 Bit floating point number image
- corners: Output vector , Corner detected
- maxCorners: The upper threshold of the number of corners obtained
- qualityLevel: Minimum threshold of acceptable corner quality , Value range 0~1
- minDistance: The minimum Euclidean distance between acceptable corners
- mask: Mask area , Detect corners only in the mask area
- blockSize: neighborhood size , The default value is 3
- k:Harris Detector adjustment parameters , The default value is 0.04
- useHarrisDetector : The default value is False, Use cornerMinEigenVal Parameters of ,True Said the use of Harris detector
matters needing attention :
- Function USES cornerMinEigenVal or cornerHarris Calculate the corner response result of each pixel .
- With qualityLevel Multiply with the maximum corner value as the minimum eigenvalue (cornerMinEigenVal) Or response function value (cornerHarris), Corners smaller than this value are rejected . for example , The maximum response value is 1500, The coefficient is 0.1, Then the response value is less than 150 The corners of are abandoned .
- Less than maxDistance The corner of the distance is rejected , To avoid getting adjacent feature points .
routine 14.21: Feature detection Shi-tomas Angle detector
# 14.21 Feature detection Shi-tomas Angle detector
img = cv2.imread("../images/sign04.png", flags=1) # (300, 300, 3)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Harris Check corner
dst = cv2.cornerHarris(gray, 2, 3, k=0.04)
# Harris[dst > 0.01*dst.max()] = [0, 0, 255] # Filter corners , The red mark
corners = np.column_stack(np.where(dst>0.1*dst.max())) # Filter and return corner coordinates (y,x)
corners = corners.astype(np.int) # Point set of detected corners (y,x), (92, 2)
imgHarris = np.copy(img)
for point in corners: # Pay attention to the coordinate order
cv2.circle(imgHarris, (point[1], point, 4, (0,0,255), 2) # # At point (x,y) Draw a circle at
# Shi-tomas Check corner
corners = cv2.goodFeaturesToTrack(gray, 30, 0.3, 5) # (30, 1, 2)
corners = np.squeeze(corners).astype(np.int) # (30, 1, 2)->(30,2) Corner coordinates (x,y)
imgShiTomas = np.copy(img)
for point in corners:
cv2.circle(imgShiTomas, point, 4, (0,0,255), 2) # # At point (x,y) Draw a circle at
plt.figure(figsize=(9, 6))
plt.subplot(131), plt.axis('off'), plt.title("Origin")
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.subplot(132), plt.axis('off'), plt.title("Harris corners")
plt.imshow(cv2.cvtColor(imgHarris, cv2.COLOR_BGR2RGB))
plt.subplot(133), plt.axis('off'), plt.title("Shi-tomas corners")
plt.imshow(cv2.cvtColor(imgShiTomas, cv2.COLOR_BGR2RGB))
plt.tight_layout()
plt.show()

【 At the end of this section 】
Copyright notice :
[email protected] Original works , Reprint must be marked with the original link :(https://blog.csdn.net/youcans/article/details/125828053)
Copyright 2022 youcans, XUPT
Crated:2022-7-15238. OpenCV Medium Harris Corner detection
239. Harris Accurate positioning of corner detection (cornerSubPix)
240. OpenCV Medium Shi-Tomas Corner detection
边栏推荐
- Implementing dropout with mxnet from zero sum
- After Australia, New Zealand announced the ban on Huawei 5g! Huawei official response
- Application of machine vision in service robot
- Methods of path related comments (I)
- Recurrence of historical loopholes in ThinkPHP
- Xiaomi Wuhan headquarters building starts today! Lei Jun: planned according to the scale of 10000 people
- Mount NFS storage
- [ctfshow web] deserialization
- Comparison between agile development and Devops
- 如何使用 align-regexp 对齐 userscript 元信息
猜你喜欢

Anaconda download and Spyder error reporting solution

Crazy God redis notes 02

2019 popularization group summary

浅谈云原生边缘计算框架演进

Comparison between dimensional modeling and paradigm modeling

Avalanche subnets vs. polygon supernets of application chain

Execution process of select statement in MySQL

Pass-19,20

Tensor operation in pytoch

The principle of reliable transmission in TCP protocol
随机推荐
Common super easy to use regular expressions!
Is it safe for Guosen Securities to open an account? How can I find the account manager
SQL injection (mind map)
2022 software testing skills postman+newman+jenkins continuous integration practical tutorial
Eureka Registry - from entry to application
[visdrone data set] yolov7 training visdrone data set and results
Stop using xshell and try this more modern terminal connection tool
(25)Blender源码分析之顶层菜单Blender菜单
Leetcode:1206. design jump table [jump table board]
敏捷开发与DevOps的对比
Crazy God redis notes 02
常用超好用正则表达式!
Pyqt5 rapid development and practice 3.2 introduction to layout management and 3.3 practical application of QT Designer
MySQL foundation - basic database operation
Redis hotspot key and big value
On the evolution of cloud native edge computing framework
How does the data link layer transmit data
Analysis of the advantages of eolink and JMeter interface testing
Implement softmax classification from zero sum using mxnet
OpenWrt之feeds.conf.default详解