代码实现
# 导入库
import cv2 as cv
import matplotlib.pyplot as plt # 或from matplotlib import pyplot as plt
import numpy as np
# %% 参数设置
# detector parameters
blockSize=3 # 用于角点检测的领域大小即窗口尺寸
Ksize=3 # 用于计算梯度图的sobel算子的尺寸
k=0.06
image=cv.imread('Scenery.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,cv.COLOR_BGR2GRAY) # 彩色图像灰度值化
# %%
# 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,blockSize,Ksize,k)
# result is dilated for marking the corners, not necessary
kernel = cv.getStructuringElement(cv.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():
cv.circle(image,(c,r),5,(0,0,255),0)
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
plt.imshow(image)
plt.show()