Reputation: 186
I am trying to use OpenCV 3, in particular SIFT features, findHomography and warpPerspective, in order to find the image1 in a larger image2 and then perspective transform image2 so that it becomes (almost) equal to image1.
This is the code:
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()
The resulting image is only slightly warped but not enough to match the img1.
Here are the matching result and the warped (?) result.
Upvotes: 3
Views: 4728
Reputation: 186
I solved the problem. I was doing the homography in the wrong direction. It should be:
M, mask = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC, 5.0)
instead of:
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
Upvotes: 3