python - Python 中的 SIFT 对象匹配

标签 python opencv image-processing feature-detection sift

我正在尝试关注这个 tutorial用我自己的图像。但是,我得到的结果并不完全符合我的预期。我在这里遗漏了什么,或者在这种情况下 SIFT 不是一个足够好的解决方案吗? 非常感谢。

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

MIN_MATCH_COUNT = 10

img1 = cv2.imread('Q/IMG_1192.JPG', 0)          # queryImage
img2 = cv2.imread('DB/IMG_1208-1000.jpg', 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_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)

flann = cv2.FlannBasedMatcher(index_params, search_params)

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

# store all the good matches as per Lowe's ratio test.
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)

    img2 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.LINE_AA)

else:
    print ("Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT))
    matchesMask = None

draw_params = dict(matchColor = (0,255,0), # draw matches in green color
                   singlePointColor = None,
                   matchesMask = matchesMask, # draw only inliers
                   flags = 2)

img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params)

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

原始图像:

结果:

最佳答案

从提供的图像中,我发现 SIFT 无法找出非常大的图像的关键特征。考虑单独的汽车图像,它的宽度为 1728 像素,高度为 2304 像素。这太大了另一张图片的尺寸非常正常,汽车占据了较小的区域。

预期匹配的某些特征是车轮上的轮辋、 window 上的角、引擎盖周围的角等。但是在放大的图像中,例如没有明显角的图像,而是有更多存在的边缘。 SIFT 寻找本质上不同的特征点(特别是角点)。

将汽车图像调整为 (605 x 806) 尺寸并将另一幅图像调整为尺寸 (262 x 350) 后,在下图中找到了一个正确的匹配项(注意车轮附近的匹配项):

enter image description here

我为另一组包含一些字母和图画的图像尝试了相同的代码。这是结果:

enter image description here

关于python - Python 中的 SIFT 对象匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51693427/

相关文章:

python - 通过 Python 但通过 Linux 命令终端调用 MATLAB

python - Errno 13 权限被拒绝,在 WSL、virtualenv 中,即使作为 root

c++ - opencv 简单的 Blob 检测,得到一些未检测到的 Blob

image-processing - 如何通过地标点注册人脸

java - 如何使用java将PNG图像转换为灰度图像?

image-processing - 将 Vector<Point> 转换为 Mat

python - 文件夹+数据库的备份 - Python

python - 我是否应该删除整个列中具有相同值的变量来构建机器学习模型?

opencv - 使用 HSV 和 HoughCircles 通过 OpenCV 检测彩色球体

python - 将边界框的内容保存为新图像 - opencv python