当前位置:网站首页>[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
边栏推荐
- Speaker recruitment | AI time recruit icml/ijcai 2022 as a Chinese speaker!!!
- 重磅公布!ICML2022奖项:15篇杰出论文,复旦、厦大、上交大研究入选
- What is a test case? How to design?
- 【Express接收Get、Post、路由请求参数】
- 如何快速使用 ELisp 进行插件编写
- The latest interface of Taobao / tmall keyword search
- About the adjustment of the game background, reading this article is enough
- Linear regression from zero sum using mxnet
- 现在网上开户安全么?股票开户要找谁?
- After Oracle creates a table partition, the partition is not given during the query, but the value specified for the partition field will be automatically queried according to the partition?
猜你喜欢

Tensor operation in pytoch

Small application of C language using structure to simulate election

Anaconda download and Spyder error reporting solution

leetcode:1206. 设计跳表【跳表板子】

How can win11 system be reinstalled with one click?

Pass-19,20

得不偿失!博士骗领210万元、硕士骗领3万元人才补贴,全被判刑了!
![[Development Tutorial 9] crazy shell arm function mobile phone-i2c tutorial](/img/9d/2a1deca934e6d56d729922b1d9e515.png)
[Development Tutorial 9] crazy shell arm function mobile phone-i2c tutorial

Recurrence of historical loopholes in ThinkPHP

【Express接收Get、Post、路由请求参数】
随机推荐
Stand aside with four and five rear cameras, LG or push the 16 rear camera mobile phone!
机器视觉在服务机器人中的应用
Relationship between standardization, normalization and regularization
Reuse idea through registry
【机器学习】Mean Shift原理及代码
Detailed explanation of openwrt's feeds.conf.default
(25)Blender源码分析之顶层菜单Blender菜单
Machine learning - what are machine learning, supervised learning, and unsupervised learning
Thoroughly uncover how epoll realizes IO multiplexing
VIM visualization mode and its usage
Chuan Hejing technology's mainland factory was invaded by a virus, and the whole line was shut down!
The first self-developed embedded 40nm industrial scale memory chip in China was released, breaking the status quo that the localization rate is zero
[basic course of flight control development 2] crazy shell · open source formation UAV - timer (LED flight information light and indicator light flash)
Crazy God redis notes 02
How to use different tools to analyze and optimize code performance when CPU utilization is high
Avalanche subnets vs. polygon supernets of application chain
Pytest(思维导图)
2022 software testing skills postman+newman+jenkins continuous integration practical tutorial
Pack tricks
Batch normalization batch_ normalization