Harris⻆点检测算法思想
算法的核心是利用局部窗口在图像上进行移动,如果窗口内的灰度值有较大变化,那么这个窗口所在区域就存在角点。
Harris⻆点检测算法三步
1.当窗口同时向水平和垂直两个方向移动时,计算窗口内部的像素值变化量E(x,y);
2.计算每个窗口对应的角点响应函数R;
3.对该函数R进行阈值处理,如果其大于阈值,则表示该窗口对应一个角点特征。
对梯度图矩阵M对角化的理解
有人觉得R与R^(-1)写反了,其实这样是可以的,只是R不再是由特征向量构成的矩阵,R的逆才是。
cornerHarris代码实现
原来import cv2 as cv 后,cv2与cv都可以使用,但是其他的模块只能使用as之后的简称。
import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np
# detector parameters
block_size = 3
sobel_size = 3
k = 0.06
image = cv.imread('./img3.jpg')
print(image.shape)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print("width: %s height: %s channels: %s"%(width, height, channels))
gray_img = cv.cvtColor(image, cv2.COLOR_BGR2GRAY)
print(gray_img.shape)
# modify the data type setting to 32-bit floating point
gray_img = np.float32(gray_img)
# detect the corners with appropriate values as input parameters
corners_img = cv.cornerHarris(gray_img, block_size, sobel_size, k)
# result is dilated for marking the corners, not necessary
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))
dst = cv.dilate(corners_img, kernel)
# Threshold for an optimal value, marking the corners in Green
#image[corners_img>0.01*corners_img.max()] = [0,0,255]
for r in range(height):
for c in range(width):
pix=dst[r,c]
if pix>0.05*dst.max():
cv2.circle(image,(c,r),5,(0,0,255),0)
image = cv.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.show()