Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用SIFT描述符快速比较相似性/分类图像_Python_Opencv_Classification_Knn_Sift - Fatal编程技术网

Python-使用SIFT描述符快速比较相似性/分类图像

Python-使用SIFT描述符快速比较相似性/分类图像,python,opencv,classification,knn,sift,Python,Opencv,Classification,Knn,Sift,我知道这是一个关于堆栈溢出的流行问题,但是,我还没有找到最好的解决方案 背景 我正在尝试对图像进行分类。我目前有10000个独特的图像,一个给定的图像可以匹配。对于数据库中的每个图像,我只有一个用于培训的图像。所以我有一个10000分贝,可能的输出类也是10000。e、 假设有10000个独特的对象,每个对象我都有一个单独的图像 目标是将输入图像与数据库中的“最佳”匹配图像相匹配 我目前正在使用Python和OpenCV以及Sift库来识别关键点/描述符,然后应用标准匹配方法来查看数据库中输入图

我知道这是一个关于堆栈溢出的流行问题,但是,我还没有找到最好的解决方案

背景

我正在尝试对图像进行分类。我目前有10000个独特的图像,一个给定的图像可以匹配。对于数据库中的每个图像,我只有一个用于培训的图像。所以我有一个10000分贝,可能的输出类也是10000。e、 假设有10000个独特的对象,每个对象我都有一个单独的图像

目标是将输入图像与数据库中的“最佳”匹配图像相匹配

我目前正在使用Python和OpenCV以及Sift库来识别关键点/描述符,然后应用标准匹配方法来查看数据库中输入图像最匹配的图像

代码

我使用下面的代码迭代我的图像数据库,然后找到所有关键点/描述符,并将这些描述符保存到一个文件中。这是为了节省以后的时间

for i in tqdm(range(labels.shape[0])): #Use the length of the DB

    # Read img from DB
    img_path = 'data/'+labels['Image_Name'][i]
    img = cv2.imread(img_path) 

    # Resize to ensure all images are equal for ROI
    dim = (734,1024)
    img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

    #Grayscale
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    #Roi
    img = img[150:630, 20:700]

    # Sift
    sift = cv2.xfeatures2d.SIFT_create()
    keypoints_1, descriptors_1 = sift.detectAndCompute(img,None)

    # Save descriptors
    path = 'data/'+labels['Image_Name'][i].replace(".jpeg", "_descriptors.csv")
    savetxt(path, descriptors_1, delimiter=',')
然后,当我准备好对图像进行分类时,我可以读入所有描述符。事实证明,这比以前快了30%

# Array to store all of the descriptors from SIFT
descriptors = []

for i in tqdm(range(labels.shape[0])): #Use the length of the DB

    # Read in teh descriptor file
    path = 'data/'+labels['Image_Name'][i].replace(".jpeg", "_descriptors.csv")
    descriptor = loadtxt(path, delimiter=',')

    # Add to array
    descriptors.append(descriptor)
最后,我只需要读取图像,应用sift方法,然后找到最佳匹配

# Calculate simaularity 
img = cv2.imread(PATH)

# Resize
dim = (734,1024)
img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)

#Grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#Roi
img = img[150:630, 20:700]

# Sift
sift = cv2.xfeatures2d.SIFT_create()
keypoints_1, descriptors_1 = sift.detectAndCompute(img,None)

# Use Flann (Faster)
index_params = dict(algorithm=0, trees=5)
search_params = dict()
flann = cv2.FlannBasedMatcher(index_params, search_params)

# Store results
scoresdf = pd.DataFrame(columns=["index","score"])

#Find best matches in DB
for i in tqdm(range(labels.shape[0])):
    # load in data
    path = 'data/'+labels['Image_Name'][i].replace(".jpeg", "_descriptors.csv")

    # Get descriptors for both images to compare
    descriptors_2 = descriptors[i]
    descriptors_2 = np.float32(descriptors_2)

    # Find matches
    matches = flann.knnMatch(descriptors_1, descriptors_2, k=2)

    # select the lowest amount of keypoints
    number_keypoints = 0
    if len(descriptors_1) <= len(descriptors_2):
        number_keypoints = len(descriptors_1)
    else:
        number_keypoints = len(descriptors_2)

    # Find 'good' matches LOWE
    good_points = []
    ratio = 0.6
    for m, n in matches:
        if m.distance < ratio*n.distance:
            good_points.append(m)

    # Get simularity score
    score = len(good_points) / number_keypoints * 100
    scoresdf.loc[len(scoresdf)] = [i, score]
#计算相似性
img=cv2.imread(路径)
#调整大小
尺寸=(7341024)
img=cv2.调整大小(img、dim、插值=cv2.内部区域)
#灰度
img=cv2.cvt颜色(img,cv2.COLOR\u bgr2灰色)
#投资回报率
img=img[150:630,20:700]
#筛选
sift=cv2.xfeature2d.sift_create()
关键点_1,描述符_1=筛选、检测和计算(img,无)
#使用法兰(更快)
索引参数=dict(算法=0,树=5)
搜索参数=dict()
flann=cv2.FlannBasedMatcher(索引参数、搜索参数)
#存储结果
scoresdf=pd.DataFrame(列=[“索引”,“分数”])
#在数据库中查找最佳匹配项
对于tqdm中的i(范围(labels.shape[0]):
#载入数据
path='data/'+标签['Image\u Name'][i]。替换(“.jpeg”、“\u descriptors.csv”)
#获取两个图像的描述符以进行比较
描述符_2=描述符[i]
描述符_2=np.float32(描述符_2)
#查找匹配项
matches=flann.knnMatch(描述符_1,描述符_2,k=2)
#选择最少数量的关键点
数字\关键点=0

如果len(描述符_1),您是否找到了解决方案?我也有同样的问题。