Skip to content

FLANN error when setting eps parameter in python #21642

@VasylVaskivskyi

Description

@VasylVaskivskyi
System information (version)
  • OpenCV => 4.5.5 opencv-contrib-python
  • Operating System / Platform => Windows 10 64 bit
  • Compiler => ¯\_(ツ)_/¯
Detailed description

I tried to set eps FLANN search parameter in python

FLANN_INDEX_KDTREE = 1
index_param = dict(algorithm=FLANN_INDEX_KDTREE, trees=4)
search_param = dict(checks=32, eps=0.0, sorted=True, explore_all_trees=False)
matcher = cv.FlannBasedMatcher(index_param, search_param)
matches = matcher.knnMatch(des2, des1, k=2)

But this returned cv2.error: Unknown C++ exception from OpenCV code

I tried to use c_float from ctypes lib (eps=c_float(0.0)), but it gave me a different error SystemError: <class 'cv2.FlannBasedMatcher'> returned NULL without setting an error

Steps to reproduce
import cv2 as cv
from ctypes import c_float


def find_features(img, nfeatures_limit=5000):
    # default values except for threshold - discard points that have 0 response
    detector = cv.FastFeatureDetector_create(threshold=1, nonmaxSuppression=True,
                                             type=cv.FAST_FEATURE_DETECTOR_TYPE_9_16)
    # default values
    descriptor = cv.xfeatures2d.DAISY_create(radius=21, q_radius=3, q_theta=8, q_hist=8,
                                             norm=cv.xfeatures2d.DAISY_NRM_NONE,
                                             interpolation=True, use_orientation=False)
    kp = detector.detect(img)
    kp = sorted(kp, key=lambda x: x.response, reverse=True)[:nfeatures_limit]
    kp, des = descriptor.compute(img, kp)

    if kp is None or len(kp) < 3:
        return [], []
    if des is None or len(des) < 3:
        return [], []
    return kp, des


def match_features(img1_kp_des, img2_kp_des):
    kp1, des1 = img1_kp_des
    kp2, des2 = img2_kp_des

    FLANN_INDEX_KDTREE = 1
    index_param = dict(algorithm=FLANN_INDEX_KDTREE, trees=4)
    search_param = dict(checks=32, eps=0.0, sorted=True, explore_all_trees=False)
    matcher = cv.FlannBasedMatcher(index_param, search_param)
    matches = matcher.knnMatch(des2, des1, k=2)

    # Filter out unreliable points
    good = []
    for m, n in matches:
        if m.distance < 0.5 * n.distance:
            good.append(m)

    print('good matches', len(good), '/', len(matches))
    if len(good) < 3:
        return None

    src_pts = np.array([kp1[m.trainIdx].pt for m in good], dtype=np.float32).reshape((-1, 1, 2))
    dst_pts = np.array([kp2[m.queryIdx].pt for m in good], dtype=np.float32).reshape((-1, 1, 2))

    # find out how images shifted (compute affine transformation)
    affine_transform_matrix, mask = cv.estimateAffinePartial2D(dst_pts, src_pts, method=cv.RANSAC, confidence=0.99)
    return affine_transform_matrix


ref_img_kpdes = find_features(ref_img)
mov_img_kpdes = find_features(mov_img)
est_t_mat_pyr = match_features(ref_img_kpdes, mov_img_kpdes)
Issue submission checklist
  • [+] I report the issue, it's not a question
  • [+] I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found any solution
  • [+] I updated to the latest OpenCV version and the issue is still there
  • [+] There is reproducer code and related data files: videos, images, onnx, etc

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions