sveatlo
sveatlo

Reputation: 593

OpenCV's Flann matcher index in python

I'm implementing a program which is supposed to match an image (img1) to a very similar image (usually just different resolution an lighting; sometimes some translation) from a set of around 15-30 images.

I'm using ORB feature detector and and Flann matcher. To use the matcher I compute keypoints and descriptors for the first image (img1) and then for each picture from the set, run the flann matcher comparing each of the images with img1 and get the best result.

However, if I understood it correctly, there is something called "Flann matcher Index", which can be trained for the set and than chooses the best match for you. Is this correct? If so, how can I use it with python? I saw some examples for C++ but not python is there some documentation which I overlooked?

EDIT: Basically I want to know if something like this is possible in python

Upvotes: 5

Views: 7256

Answers (1)

via tom
via tom

Reputation: 41

is this what you want?

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

img1 = cv2.imread( file1,0)          # queryImage
img2 = cv2.imread( file2,0) # trainImage

# Initiate SIFT detector
orb = cv2.ORB_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)

# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)   # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params,search_params)

des1 = np.float32(des1)
des2 = np.float32(des2)

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

# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in range(len(matches))]

# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
    if m.distance < 0.7*n.distance:
        matchesMask[i]=[1,0]

draw_params = dict(matchColor = (0,255,0),
                   singlePointColor = (255,0,0),
                   matchesMask = matchesMask,
                   flags = 0)

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)

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

Upvotes: 2

Related Questions