4
我的數據庫中有幾條魚圖像,我的目標是找到用戶輸入的魚圖像和數據庫中的圖像之間的相似性分數。爲此,我正在使用此鏈接中的opencv Feature matching + Homograpy。Opencv Python - 來自特徵匹配+同形相似度得分
我當前的代碼是其次。
query_image = '/home/zealous/Pictures/train_images/AbudefdufWhitleyiJER.jpg'
trained_image_folder = '/home/zealous/Pictures/train_images'
我現在的代碼如下。
def feature_matcher(query_image, image_folder):
min_match_count = 10
img1 = cv2.imread(query_image, 0)
surf = cv2.xfeatures2d.SURF_create(800)
kp1, des1 = surf.detectAndCompute(img1, None)
bf = cv2.BFMatcher(cv2.NORM_L2)
all_files = next(os.walk(image_folder))[2]
for file_name_temp in all_files:
try:
train_image = image_folder + '/' + file_name_temp
img2 = cv2.imread(train_image, 0)
surf = cv2.xfeatures2d.SURF_create(800)
kp2, des2 = surf.detectAndCompute(img2, None)
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.7*n.distance:
good.append(m)
if len(good) > min_match_count:
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1,1,2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
h, w = img1.shape
pts = np.float32([[0, 0], [0, h-1], [w-1, h-1], [w-1, 0]]).reshape(-1,1,2)
dst = cv2.perspectiveTransform(pts, M)
if not M==None:
print "\n"
print "-"*2, file_name_temp
print "number of good matches", len(good)
print "*"*10, matchesMask
我得到它,我通過觀看精彩的比賽和matchesMask變量(其中包含一些0和1)的數量假定相當不錯的輸出。如果數據庫包含與輸入圖像相同的圖像,那麼會有很多好的匹配,並且所有匹配Mask元素將爲1.
我的問題是如何基於此計算相似性分數?我應該假設matchMask中有更多的1(Inliers),這兩個圖像都是相似的,或者我應該把1(內點)和0(外點)數量之間的比率並基於此計算相似度。
我知道這已經在許多問題進行了討論,但所有的建議和答案是C++語言,所以我不能找出解決方案..