0. 导入图像
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('picture.jpg')[:,:,::-1]
plt.imshow(img)
plt.show()
1. Harris
- cv2.cornerHarris( src, blockSize, ksize, k[, dst[, borderType]] )
- scr 为输入的图像,需为灰度图且应为 float32 类型
- blockSize 为角点检测考虑的邻域大小
- ksize 为 Sobel 导数的光圈参数
gray_img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
gray = np.float32(gray_img)
dst = cv2.cornerHarris(gray,2,3,0.04)
# 结果用于标记角点,并不重要
dst = cv2.dilate(dst,None)
# 最佳值的阈值,它可能因图像而异。
img[dst>0.01*dst.max()] = [0,0,255]
plt.imshow(img)
plt.show()
输出结果:
2. Shi-Tomasi
- cv2.goodFeaturesToTrack( image, maxCorners, qualityLevel, minDistance[, corners[, mask[, blockSize[, useHarrisDetector[, k]]]]] )
- image 为输入的图像,要求同 Harris 角点检测
- maxCorners 为想要检测的角点数目
- qualityLeve 为角点的最低质量,0 到 1之间。低于这个数的所有角点都会被忽略
- minDistance 为检测出的两个角点之间的最短欧式距离
img1 = cv2.imread('picture.jpg')
gray = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
corners = cv2.goodFeaturesToTrack(gray,200,0.01,10)
corners = np.int0(corners)
for i in corners:
x,y = i.ravel()
cv2.circle(img1,(x,y),3,255,-1)
plt.figure(figsize=(10,10))
plt.imshow(img1)
plt.show()
输出结果: