[OpenCV.js] Getting SIFT and FLANN to work

Hi, all! I’m looking for help on how to use SIFT and FLANN with OpenCV.js.

I basically want to translate this code from Python:

needle = cv.imread('needle.png', 0)
haystack = cv.imread('haystack.png', 0)

sift = cv.SIFT_create()

FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
flann = cv.FlannBasedMatcher(index_params, search_params)

kp_needle, desc_needle = sift.detectAndCompute(needle, None)
kp_haystack, desc_haystack = sift.detectAndCompute(haystack, None)
matches = flann.knnMatch(desc_needle, desc_haystack, k=2)

This is what I understand so far would be the “translation” (and please correct me if I’m wrong!):

const needle = await loadImage('needle.png'),
    haystack = await loadImage('haystack.png'),
    needle_src = cv.imread(needle, 0),
    haystack_src = cv.imread(haystack, 0),

    sift = new cv.SIFT(),

    FLANN_INDEX_KDTREE = 1
    // ??? for flann variable of FlannBasedMatcher? (A)

let kp_needle = new cv.KeyPointVector(),
    kp_haystack = new cv.KeyPointVector(),
    desc_needle =  new cv.Mat(),
    desc_haystack =  new cv.Mat()

sift.detectAndCompute(needle_src, new cv.Mat(), kp_needle, desc_needle)
sift.detectAndCompute(haystack_src, new cv.Mat(), kp_haystack, desc_haystack)
// ??? for flann's knnMatch (B)

.

I tried setting up this for issue area A, mimicking the Python version as closely as I could, but the error I received indicates FlannBasedMatcher() takes 0 parameters:

const index_params = {algorithm: FLANN_INDEX_KDTREE, trees: 5},
    search_params = {checks: 50},
    flann = new cv.FlannBasedMatcher(index_params, search_params)

RuntimeError: abort(BindingError: Tried to invoke ctor of FlannBasedMatcher with invalid number of parameters (2) - expected (0) parameters instead!). Build with -s ASSERTIONS=1 for more info.

.

I’m not sure what I need to do to get that param data passed over if it wants no parameters. Any ideas?

I haven’t tried figuring out issue area B yet, since I imagine I need A sorted out first.

It was thanks to a code someone wrote using ORB that I was able to figure it out for SIFT. I can’t seem to find anything like that for FLANN.

I had to build OpenCV.js myself to add both SIFT and FLANN. It’s been rough, but I’ve been able to figure it out up until now. If I can get all these pieces figured out, I’ll share what I had to do so others can more easily use this, too.

I appreciate any help!