import cv2
import numpy as np
import matplotlib.pyplot as plt
img =cv2.imread('E:/python/ha.png')
imgray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#harris角点检测图像需为float32
gray=np.float32(imgray)
dst=cv2.cornerHarris(gray,8,3,0.04)
dst=cv2.dilate(dst,None)
ret,dst=cv2.threshold(dst,0.01*dst.max(),255,0)
dst=np.uint8(dst)
#图像连通域
ret,labels,stats,centroids=cv2.connectedComponentsWithStats(dst)
#迭代停止规则
criteria=(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER,100,0.001)
corners=cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
res=np.hstack((centroids,corners))
res=np.int0(res)
#img[res[:,1],res[:,0]]=[0,120,255]
#img[res[:,3],res[:,2]]=[45,255,100]
for i in res:
x1,y1,x2,y2=i.ravel()
cv2.circle(img,(x1,y1),3,255,-1)
cv2.circle(img,(x2,y2),3,(0,255,0),-1)
img=img[:,:,::-1]
plt.imshow(img)
参考https://blog.csdn.net/zhu_hongji/article/details/81235643