角点检测顾名思义,就是对类似顶点的检测,与边缘有所区别
边缘可能在某一方向上变化不是特别明显,但角点在任何方向上变换都很明显
cv2.cornerHarris(img,blockSize,ksize,k)
cv2.cornerHarris(gray,2,3,0.04)
参数一:img,类型为float32的图像
参数二:blockSize 角点检测中指定区域的大小
参数三:ksize Sobel算子求导中使用的窗口大小,一般情况设置为3
参数四:k 取值参数为[0.04,0.06],opencv推荐使用0.04
import cv2
import numpy as np
from matplotlib import pyplot as pltdef show_photo(name,picture):#图像显示函数cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()img = cv2.imread('E:\Jupyter_workspace\study\data/cfx.png')
print('img.shape:',img.shape)#结果为:img.shape: (151, 269, 3)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#将彩色图转换为灰度图
#gray = np.float32(gray)dst = cv2.cornerHarris(gray,2,3,0.04)
print("dst.shape:",dst.shape)#结果为:dst.shape: (151, 269)img[dst>0.01*dst.max()] = [0,0,255]#(B,G,R)以红色点描绘显示
show_photo('dst',img)
原图:
角点检测结果: