OpenCV warpPerspective不支持单应性

时间:2019-09-25 11:16:43

标签: python opencv homography

我正在尝试使用OpenCV 3,特别是SIFT功能,findHomography和warpPerspective,以便在较大的image2中找​​到image1,然后透视变换image2,以使其(几乎)等于image1。

这是代码:

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

MIN_MATCH_COUNT = 10

img1 = cv2.imread('box.png',0)          # queryImage
img2 = cv2.imread('box_in_scene.png',0) # trainImage

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

# find the keypoints and descriptors with SIFT
kp1, des1 = detector.detectAndCompute(img1, None)
print("Image 1: # kps: {}, descriptors: {}".format(len(kp1), des1.shape))
kp2, des2 = detector.detectAndCompute(img2, None)
print("Image 2: # kps: {}, descriptors: {}".format(len(kp2), des2.shape))

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)
else:
    print("Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT))

im_out = cv2.warpPerspective(img2, M, (img1.shape[1] * 2, img1.shape[0] * 2))
plt.imshow(im_out)
#plt.imshow(img2)
plt.show()

生成的图像仅略微变形,但不足以匹配img1。

这是匹配结果和扭曲(?)结果。

Matching result

Warped (?) result

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。我在错误的方向上做单应性。应该是:

    M, mask = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC, 5.0)

代替:

    M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

Right warped result