opencv python 角点提取

https://blog.csdn.net/zhu_hongji/article/details/81235643
图像特征描述
角点检测原理

ret,labels,stats,centroids=cv2.connectedComponentsWithStats(dst)

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)

shi_Tomasi角点

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.goodFeaturesToTrack(gray,12,0.01,10)

res=np.int0(dst)
for i in res:
    x,y=i.ravel()
    cv2.circle(img,(x,y),3,255,-1)
img=img[:,:,::-1]
plt.imshow(img)

角点具有旋转不变性但是不具有缩放性

fast角点

import cv2
import numpy as np
import matplotlib.pyplot as plt

A = cv2.imread('E:/python/cd.png')
imgray=cv2.cvtColor(A,cv2.COLOR_BGR2GRAY)

fast = cv2.FastFeatureDetector_create(threshold=20,nonmaxSuppression=True,
                                      type=cv2.FAST_FEATURE_DETECTOR_TYPE_9_16)
kp = fast.detect(imgray,None)
img2 = cv2.drawKeypoints(A, kp,np.array([]), color=(255,0,0))
print ("Threshold: ", fast.getThreshold())#输出阈值
print ("nonmaxSuppression: ", fast.getNonmaxSuppression())#是否使用非极大值抑制 
print ("Total Keypoints with nonmaxSuppression: ", len(kp))#特征点个数

img=img2[:,:,::-1]
plt.imshow(img)

你可能感兴趣的:(python)