角点检测

import cv2 as cv
import numpy as np

filename = 'JG.png'
img = cv.imread(filename)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
_, img2 = cv.threshold(gray, 0, 0xff, cv.THRESH_OTSU)
# cv.imshow('ordinary', img2)
# FAST角点检测算法:
fast = cv.FastFeatureDetector_create(threshold=40, nonmaxSuppression=True, type=cv.FAST_FEATURE_DETECTOR_TYPE_9_16)
kp = fast.detect(img, None)
cv.drawKeypoints(img, kp, img, color=(255, 255, 255))

# SURF角点检测
minHessian = 1000
detector = cv.xfeatures2d.SURF_create(minHessian)
descriptor = cv.xfeatures2d.SURF_create()
matcher1 = cv.DescriptorMatcher_create('BruteForce')
# 检测特征点
keyPoint1 = detector.detect(img)
keyPoint2 = detector.detect(img2)
# 计算特征点对应描述子
_, descriptor1 = descriptor.compute(img, keyPoint1)
_, descriptor2 = descriptor.compute(img2, keyPoint2)
# 描述子匹配
matches = matcher1.match(descriptor1, descriptor2)
img_matches = np.empty(img2.shape)
img_matches1 = cv.drawMatches(img, keyPoint1, img2, keyPoint2, matches, img_matches)
cv.imshow('img_matches', img_matches1)
cv.waitKey()
print('keyPoint1.size = ', len(keyPoint1))
print('keyPoint2.size = ', len(keyPoint2))

cv.waitKey()
cv.destroyAllWindows()