参考资料:https://blog.csdn.net/wsp_1138886114/article/details/90415190
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img = cv.imread("../SampleImages/chinease_tower.jpg", cv.IMREAD_COLOR)
plt.imshow(img[:,:,::-1])
#转换为灰度图
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
plt.imshow(img_gray, cmap=plt.cm.gray)
#转换为灰度图
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
plt.imshow(img_gray, cmap=plt.cm.gray)
#焦点检测
#1. cornerHarris函数要求的输入是float32,首先通过numpy.float32进行转换
img_gray = np.float32(img_gray)
#2. 使用cornerHarris进行焦点检测
# cv.(src=gray, blockSize, ksize, k, dst=None, borderType=None)
# src - float32类型的灰度图像数据
# blockSize - 焦点检测的窗口大小
# ksize: sobel算子卷积核大小
# k: Harris算法中的的k值,[0.04,0.06]
# 参考资料:https://blog.csdn.net/wsp_1138886114/article/details/90415190
results = cv.cornerHarris(img_gray, 2, 3, 0.04)
#设置阈值,大于阈值的角点用绿色标注
img[results > 0.001 * results.max()] = [0, 255, 0]
#图像显示
plt.imshow(img[:,:,::-1])