python - 筛选比较,计算相似度得分,python

标签 python comparison sift

我需要获得两个图像的相似度分数,我正在使用 SIFT 比较,我已经按照教程 Feature Matching但它缺少分数计算。
您会在我用于筛选比较的代码下方找到:

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

img1 = cv2.imread('C:/Users/Akhou/Desktop/ALTRAN Tech.jpg',0)          # queryImage
img2 = cv2.imread('rect.png',0) # trainImage

# Initiate SIFT detector
sift=cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)   # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params,search_params)

matches = flann.knnMatch(des1,des2,k=2)

# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in range(len(matches))]

# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
    if m.distance < 0.7*n.distance:
        matchesMask[i]=[1,0]

draw_params = dict(matchColor = (0,255,0),
                   singlePointColor = (255,0,0),
                   matchesMask = matchesMask,
                   flags = 0)

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)

plt.imshow(img3,),plt.show()

我还找到了计算分数的代码的一部分:
# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])
        a=len(good)
        print(a)
        percent=(a*100)/kp1
        print("{} % similarity".format(percent))
        if percent >= 75.00:
            print('Match Found')
            break;

但是当我将它添加到比较代码时,我收到此错误:
  percent=(a*100)/kp1
  TypeError: unsupported operand type(s) for /: 'int' and 'list

谢谢

最佳答案

我相信我为我的问题找到了解决方案,对于那些面临同样问题的人,您会在代码下方找到,我已经测试过,它似乎工作正常。

import numpy as np
import cv2
from matplotlib import pyplot as plt
from tkinter.filedialog import askopenfilename

filename1 = askopenfilename(filetypes=[("image","*.png")]) # queryImage
filename2 = askopenfilename(filetypes=[("image","*.png")]) # trainImage

img1=cv2.imread(filename1,4)
img2=cv2.imread(filename2,4)

# Initiate SURF detector
surf=cv2.xfeatures2d.SURF_create()

# find the keypoints and descriptors with SURF
kp1, des1 = surf.detectAndCompute(img1,None)
kp2, des2 = surf.detectAndCompute(img2,None)

# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)

# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])
        a=len(good)
        percent=(a*100)/len(kp2)
        print("{} % similarity".format(percent))
        if percent >= 75.00:
            print('Match Found')
        if percent < 75.00:
            print('Match not Found')

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
plt.imshow(img3),plt.show()

如果你想使用 sift,你可以改变 surf=cv2.xfeatures2d.SURF_create()sift=cv2.xfeatures2d.SIFT_create()kp, des = sift.detectAndCompute(img,None)
谢谢

关于python - 筛选比较,计算相似度得分,python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50217364/

相关文章:

python - 无法从给定格式中提取出生日期

java - 将字符串与数组列表中的字符串的一部分进行比较

python - Pip 安装但找不到模块

os.removexattr 的 Python 文档—— '*'(星号)参数是什么意思?

PYTHON - 如何迭代变量列表并根据设置条件更改值?

c# - C# 是适用于大型项目的可行语言吗?

java - java中比较数组字符串以获得 boolean 值

c++ - 使用 SIFT 描述符的 OpenCV (C++) 增加了检测到的特征的数量?

android - 使用 opencv for android 绘制火柴

opencv - 获取Matrix来训练sift特征的问题