Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

FLANN Index in Python - How to Add and Match?

Dear Internet,

I'm trying to add a number of images to a FLANN index (thousands in reality, once I have this working) and then find the closest match in the index to a query image. But it segfaults. :(

Bare essentials code:

import numpy as np
import cv2
import sys


FLANN_INDEX_LSH = 6


img1 = cv2.imread(sys.argv[1],0)


brisk = cv2.BRISK()


kp1, des1 = brisk.detectAndCompute(img1,None)


index_params= dict(algorithm = FLANN_INDEX_LSH,
                   table_number = 6, # 12
                   key_size = 12,     # 20
                   multi_probe_level = 1) #2
search_params = dict(checks=50)   # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params,search_params)

for filename in sys.argv[2:]:
    img2 = cv2.imread(filename, 0)
    print "Detecting and computing {0}".format(filename)
    kp2, des2 = brisk.detectAndCompute(img2,None)
    print "Adding..."
    flann.add(des2)


print len(flann.getTrainDescriptors()) #verify that it actually took the descriptors in

print "Training..."
flann.train()                    

print "Matching..."
matches = flann.knnMatch(des1,k=2)

(I've tried this both with 2.4.9 and a pull of 3.0 alpha as from the day I'm posting this, both had same result.)

Problem: Here's the output where it fails:

Training...

Segmentation fault (core dumped)

So it dies at training, or at match() if I skip training.

What has me stuck:

  1. I can't find this functionality in the docs as regards Python. Am I in the wrong places?
    http://docs.opencv.org/trunk/modules/flann/doc/flann_fast_approximate_nearest_neighbor_search.html
    http://docs.opencv.org/trunk/modules/features2d/doc/common_interfaces_of_descriptor_matchers.html

  2. For lack of docs, I'm trying to emulate in Python what I see in matching_to_many_images.cpp from the samples since that's the closest thing to what I'm trying to do. I think. So that's where I'm getting what I have so far.

Now what? Kind of lost. Sorry if this is actually clearly documented and I just couldn't find it.

Thanks!

FLANN Index in Python - How to Add and Match?Training Fails with Segfault

Dear Internet,

I'm trying to add a number of images to a FLANN index (thousands in reality, once I have this working) and then find the closest match in the index to a query image. But it segfaults. :(

Bare essentials code:

import numpy as np
import cv2
import sys


FLANN_INDEX_LSH = 6


img1 = cv2.imread(sys.argv[1],0)


brisk = cv2.BRISK()


kp1, des1 = brisk.detectAndCompute(img1,None)


index_params= dict(algorithm = FLANN_INDEX_LSH,
                   table_number = 6, # 12
                   key_size = 12,     # 20
                   multi_probe_level = 1) #2
search_params = dict(checks=50)   # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params,search_params)

for filename in sys.argv[2:]:
    img2 = cv2.imread(filename, 0)
    print "Detecting and computing {0}".format(filename)
    kp2, des2 = brisk.detectAndCompute(img2,None)
    print "Adding..."
    flann.add(des2)


print len(flann.getTrainDescriptors()) #verify that it actually took the descriptors in

print "Training..."
flann.train()                    

print "Matching..."
matches = flann.knnMatch(des1,k=2)

(I've tried this both with 2.4.9 and a pull of 3.0 alpha as from the day I'm posting this, both had same result.)

Problem: Here's the output where it fails:

Training...

Segmentation fault (core dumped)

So it dies at training, or at match() if I skip training.

What has me stuck:

  1. I can't find this functionality in the docs as regards Python. Am I in the wrong places?
    http://docs.opencv.org/trunk/modules/flann/doc/flann_fast_approximate_nearest_neighbor_search.html
    http://docs.opencv.org/trunk/modules/features2d/doc/common_interfaces_of_descriptor_matchers.html

  2. For lack of docs, I'm trying to emulate in Python what I see in matching_to_many_images.cpp from the samples since that's the closest thing to what I'm trying to do. I think. So that's where I'm getting what I have so far.

Now what? Kind of lost. Sorry if this is actually clearly documented and I just couldn't find it.

Thanks!