OpenCV—Harris Corner Detection

角点是指在各个方向上灰度值变化都非常大的区域,灰度变化也就是灰度梯度。寻找角点也就是寻找灰度梯度最大的像素点。

我们使用一个窗口在图像上滑动来计算灰度的梯度 E ( u , v ) E(u,v) E(u,v)
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,yw(x,y)[I(x+u,y+v)I(x,y)]2
其中 w ( x , y ) w(x,y) w(x,y)为窗口函数, I ( x , y ) I(x,y) I(x,y)为某个像素点的灰度值, I ( x + u , y + v ) I(x+u,y+v) I(x+u,y+v)为窗口移动后的灰度值。

进行泰勒展开:
E ( u , v ) = ∑ x , y w ( x , y ) [ u , v ] [ I x I x I x I y I x I x I x I y ] [ u v ] E(u,v)=\sum_{x,y}w(x,y) [u,v]\begin{bmatrix} I_xI_x & I_xI_y\\I_xI_x & I_xI_y\end{bmatrix}\begin{bmatrix} u\\v\end{bmatrix} E(u,v)=x,yw(x,y)[u,v][IxIxIxIxIxIyIxIy][uv]
其中, I x , I y I_x,I_y Ix,Iy为灰度值在两个方向的梯度。


M = ∑ x , y w ( x , y ) [ I x I x I x I y I x I x I x I y ] M=\sum_{x,y}w(x,y) \begin{bmatrix} I_xI_x & I_xI_y\\I_xI_x & I_xI_y\end{bmatrix} M=x,yw(x,y)[IxIxIxIxIxIyIxIy]

E ( u , v ) = [ u , v ] M [ u v ] E(u,v)=[u,v]M\begin{bmatrix} u\\v\end{bmatrix} E(u,v)=[u,v]M[uv]
这里使用一个便于计算的数值来判断窗口在滑动过程中是否出现角点,即
R = d e t ( M ) − k ( t r a c e ( M ) ) 2 R=det(M)-k (trace(M))^2 R=det(M)k(trace(M))2

  • ∣ R ∣ |R| R很小时,表示平坦区域;
  • R < 0 R<0 R<0时,表示边缘区域;
  • R R R很大时,表示角点区域;

可以使用OpenCV的 cv.cornerHarris() 可直接获得图像中每个位置像素点的计算结果R,同时指定一个合适的阈值就可以筛选出所需要的角点信息。

Parameters:

  • img - Input image, it should be grayscale and float32 type.
  • blockSize - It is the size of neighbourhood considered for corner detection
  • ksize - Aperture parameter of Sobel derivative used.
  • k - Harris detector free parameter in the equation.

实例代码:

img = cv.imread('lena.jpg')
img_gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
# Input image should be grayscale and float32 type.
img_gray = np.float32(img_gray)
# the result of Harris Corner Detection is a grayscale image with these scores.
dst = cv.cornerHarris(img_gray,2,3,0.04)

#result is dilated for marking the corners, not important
dst = cv.dilate(dst,None)

# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]
img_plt = cv.cvtColor(img,cv.COLOR_BGR2RGB)
plt.figure(figsize=(10,10))
plt.imshow(img_plt)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值