Image stitching Python

p0lly11 picture p0lly11 · Dec 29, 2013 · Viewed 13.2k times · Source

I have to stitch two or more images together using python and openCV. I found this code for finding keypoints and matches, but I don't know how to continue. Help me please!

import numpy as np
import cv2

MIN_MATCH_COUNT = 10

img1 = cv2.imread('a.jpg',0)          # queryImage
img2 = cv2.imread('b.jpg',0) # trainImage

# Initiate SIFT detector
sift = cv2.SIFT()

# 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)

Answer

dirkjot picture dirkjot · Jun 27, 2014

Your question is not very clear, but I assume what you mean is that you have a bunch of images and you want to have opencv find the corresponding landmarks and then warp/scale each picture so that they can form one big image.

A method without using the stitcher class, basically looping over pictures and determining the best fitting one each iteration, is documented in this github code