OpenCV–Harris角点检测
原理参考博客,写的很好:https://blog.csdn.net/lwzkiller/article/details/54633670
函数:
dst = cv.cornerHarris( src, blockSize, ksize, k[, dst[, borderType]] )
src
:输入单通道8位或浮点图像;blockSize
:角点检测中指定区域的大小;ksize
:Sobel求导中使用的窗口大小;k
:取值参数为 [0,04,0.06]。
Note:在Harris角点检测中应用到了sobel算子,其主要作用在于求X轴以及Y轴上的梯度即导数。
代码示例:
import numpy as np
import cv2 as cv
#harris角点检
# dst = cv.cornerHarris( src, blockSize, ksize, k[, dst[, borderType]] )
#img类型要指定为float32
img = cv.imread("./imgs/test.jpg")
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
img_gray = np.float32(img_gray)
#角点检测
Harris = cv.cornerHarris(img_gray, 2, 3, 0.04)
print("img_shape:", img.shape)
print("harris_shape:", Harris.shape)
img_shape: (800, 1200, 3)
harris_shape: (800, 1200)
#筛选角点
img[Harris > (0.01 * Harris.max())] = [0,0,255]
cv.imshow("cornerHarris", img)
cv.waitKey(0)
cv.destroyAllWindows()