当前位置:网站首页>[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
边栏推荐
- Heavy announcement! Icml2022 Awards: 15 outstanding papers, selected by Fudan University, Xiamen University and Shanghai Jiaotong University
- How to use different tools to analyze and optimize code performance when CPU utilization is high
- How does win11 automatically clean the recycle bin?
- Pay attention to the traffic safety warning of tourism passenger transport issued by the Ministry of public security
- Can TCP and UDP use the same port?
- Crazy God redis notes 02
- Execution process of select statement in MySQL
- the loss outweighs the gain! Doctors cheated 2.1 million yuan and masters cheated 30000 yuan of talent subsidies, all of which were sentenced!
- Implementing DDD based on ABP -- aggregation and aggregation root practice
- Recurrence of historical loopholes in ThinkPHP
猜你喜欢

Stop using xshell and try this more modern terminal connection tool

How does win11 automatically clean the recycle bin?

Application of machine vision in service robot
![[flight control development basic tutorial 3] crazy shell · open source formation UAV - serial port (basic transceiver)](/img/25/160b827d74f7902ec0d6be4683b1e5.png)
[flight control development basic tutorial 3] crazy shell · open source formation UAV - serial port (basic transceiver)

2019 popularization group summary

How does win11 reinstall the system?

Machine learning - what are machine learning, supervised learning, and unsupervised learning

Pytorch中的tensor操作

In May, 2022, video user insight: user use time increased, and the platform achieved initial results in cost reduction and efficiency increase

Heavy announcement! Icml2022 Awards: 15 outstanding papers, selected by Fudan University, Xiamen University and Shanghai Jiaotong University
随机推荐
How to ensure cache and database consistency
Execution process of select statement in MySQL
Stop using xshell and try this more modern terminal connection tool
In the first half of the year, sales increased by 10% against the trend. You can always trust Volvo, which is persistent and safe
快速学会配置yum的本地源和网络源,并学会yum的使用
Linear regression from zero sum using mxnet
重磅公布!ICML2022奖项:15篇杰出论文,复旦、厦大、上交大研究入选
[ctfshow-web]反序列化
E-week finance | postal Huiwanjia bank opened; Wechat public call to stop four types of financial marketing publicity
Comparison between agile development and Devops
【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用,虚拟磁盘文件丢失的数据恢复案例
Implement softmax classification from zero sum using mxnet
[basic course of flight control development 1] crazy shell · open source formation UAV GPIO (LED flight information light and signal light control)
What is a test case? How to design?
Reuse idea through registry
37. [categories of overloaded operators]
Machine learning - what are machine learning, supervised learning, and unsupervised learning
Review the past and know the new MySQL isolation level
The latest interface of Taobao / tmall keyword search
大家下午好,请教一个问题:如何从保存点启动一个之前以SQL提交的作业?问题描述:用SQL在cl