sift
官方文档地址:https://docs.opencv.org/4.10.0/da/df5/tutorial_py_sift_intro.html
- 创建SIFT实例
cv2.SIFT.create()
- 特征检测
sift.detect
- 描述子
sift.compute/sift.detectAndCompute
- 画特征
cv2.drawKeypoints
原图
特征点
代码
import cv2
first = './12.png'
'''
sift 特征检测
官方文档地址:https://docs.opencv.org/4.10.0/da/df5/tutorial_py_sift_intro.html
'''
if __name__ == "__main__":
# 读取图片,转成灰度图
img = cv2.imread(first)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 创建SIFT实例
sift = cv2.SIFT.create()
# 特征检测
# sift.detect() function finds the keypoint in the images. You can pass a mask if you want to search only a part of image.
# Each keypoint is a special structure which has many attributes like its (x,y) coordinates,
# size of the meaningful neighbourhood, angle which specifies its orientation,
# response that specifies strength of keypoints etc.
kp = sift.detect(gray, None)
print(f"关键点:{kp}")
print(f"f关键点:{type(kp)}")
print(f"f关键点:{len(kp)}")
print(f"f关键点:{kp[0]}")
# 画出关键点
# DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS可以展示特征点的大小和方向
img = cv2.drawKeypoints(gray, kp, img, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow("sift", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# kp, des = sift.compute(gray, kp) 用计算出来的特征点计算描述子
# 或者直接计算特征点和描述子
# Here kp will be a list of keypoints and des is a numpy array of shape: len(kp) X 128
kp, des = sift.detectAndCompute(gray, None)
# print(kp)
# print(type(kp))
# print(kp[0])
print(des.shape)
print(des[0])
关键点:(< cv2.KeyPoint 0x11f179110>, < cv2.KeyPoint 0x11f179140>, < cv2.KeyPoint 0x11f179470>, < cv2.KeyPoint 0x11f179560>, < cv2.KeyPoint 0x11f179530>, < cv2.KeyPoint 0x11f179500>, < cv2.KeyPoint 0x11f1794d0>, < cv2.KeyPoint 0x11f1794a0>, < cv2.KeyPoint 0x11f17a9d0>, < cv2.KeyPoint 0x11f228c60>, < cv2.KeyPoint 0x11f228c30>, < cv2.KeyPoint 0x11f2288a0>, < cv2.KeyPoint 0x11f228930>, < cv2.KeyPoint 0x11f2289c0>, < cv2.KeyPoint 0x11f2289f0>, < cv2.KeyPoint 0x11f228ab0>, < cv2.KeyPoint 0x11f22bed0>, < cv2.KeyPoint 0x11f244570>, < cv2.KeyPoint 0x11f2443c0>, < cv2.KeyPoint 0x11f2443f0>, < cv2.KeyPoint 0x11f244510>, < cv2.KeyPoint 0x11f2445a0>, < cv2.KeyPoint 0x11f244630>, < cv2.KeyPoint 0x11f244660>, < cv2.KeyPoint 0x11f244690>, < cv2.KeyPoint 0x11f2446c0>, < cv2.KeyPoint 0x11f2446f0>, < cv2.KeyPoint 0x11f244720>)
f关键点:<class 'tuple'>
f关键点:28
f关键点:< cv2.KeyPoint 0x11f179110>
(28, 128)
[ 0. 0. 0. 0. 0. 0. 0. 2. 1. 0. 0. 0. 0. 3.
25. 18. 0. 0. 0. 0. 0. 18. 23. 2. 0. 0. 0. 0.
0. 2. 1. 1. 23. 0. 0. 0. 0. 0. 0. 10. 139. 2.
0. 0. 6. 29. 81. 136. 13. 0. 0. 0. 106. 162. 82. 26.
23. 0. 0. 0. 22. 16. 7. 36. 41. 4. 0. 0. 0. 0.
0. 1. 162. 34. 0. 0. 26. 11. 5. 22. 71. 6. 0. 0.
162. 83. 5. 7. 122. 7. 0. 0. 70. 11. 1. 17. 22. 3.
0. 0. 0. 0. 0. 0. 162. 37. 0. 0. 15. 2. 0. 0.
83. 10. 0. 0. 162. 36. 0. 0. 94. 11. 0. 0. 78. 9.
0. 0.]