cv2.KeyPoint是opencv中關鍵點檢測函數detectAndCompute()返回的關鍵點的類,他包含關鍵點的位置,方向等屬性具體如下:
#point2f pt;//位置坐標
# float size; // 特征點鄰域直徑
#float angle; // 特征點的方向,值為[零, 三百六十),負值表示不使用
# float response;
# int octave; // 特征點所在的圖像金字塔的組
# int class_id; // 用於聚類的id
代碼如下:
#關鍵點屬性
import cv2
img1 = cv2.imread('./data/33.jpg',0)
# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
print("數據類型:",type(kp1[0]))
print("關鍵點坐標:",kp1[0].pt)#第一個關鍵點位置坐標
print("鄰域直徑:",kp1[0].size)#關鍵點鄰域直徑
運行結果:
數據類型: <class 'cv2.KeyPoint'>
關鍵點坐標: (6.030540943145752, 39.67108917236328)
鄰域直徑: 2.439194679260254
cv2.DMatch是opencv中匹配函數(例如:knnMatch)返回的用於匹配關鍵點描述符的類,這個DMatch 對象具有下列屬性:
• DMatch.distance - 描述符之間的距離。越小越好。
• DMatch.trainIdx - 目標圖像中描述符的索引。
• DMatch.queryIdx - 查詢圖像中描述符的索引。
• DMatch.imgIdx - 目標圖像的索引。
代碼如下:
import cv2
img1 = cv2.imread('./data/33.jpg',0) # queryImage
img2 = cv2.imread('./data/3.jpg',0) # trainImage
# Initiate SIFT detector
orb = cv2.ORB_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Match descriptors.
matches = bf.match(des1,des2)
print("數據類型:",type(matches[0]))#查看類型
print("描述符之間的距離:",matches[0].distance)# 描述符之間的距離。越小越好。
print("圖像中描述符的索引:",matches[0].queryIdx)#查詢圖像中描述符的索引。
運行結果:
數據類型: <class 'cv2.DMatch'>
描述符之間的距離: 28.0
圖像中描述符的索引: 0