Skip to content

FLANN::knnSearch does not assert the kNN value #10548

@ghost

Description

System information (version)
  • OpenCV => 3.4.0-dev
  • Operating System / Platform => ALL
  • Compiler => ALL
Detailed description

FLANN returns garbage indices and distances when kNN is larger than its training dataset size.
More importantly, when kNN is slightly larger than the dataset size (e.g. kNN=3, size()=2), the returned indices beyond the dataset size are zeros, which creates silent, misleading correspondences.

Steps to reproduce

This is not a Python bug, it is also reproducible using C++

import numpy as np
import cv2

kNN = 7 #Any number bigger that len(index_points) will do
index_points = np.array([[0, 1, 2], [3, 4, 5]], np.float32)
query_points = np.array([[1, 2, 3]], np.float32)

flann_index = cv2.flann.Index if cv2.__version__.startswith('2') else cv2.flann_Index
flann = flann_index(index_points, dict(algorithm=0, trees=5))
idx, dist = flann.knnSearch(query_points, kNN, params = {})

np.set_printoptions(formatter={'all':lambda x: '{0:.3g},'.format(x)})
print(idx)
print(dist)
Expected result
[[0, 1, nan, nan, nan, nan, nan,]]
[[3, 12, nan, nan, nan, nan, nan,]]

OR to throw an exception!

Actual result
[[0, 1, 1.63e+09, 1.7e+09, 0, 0, 15,]]
[[3, 12, 7.26e-39, 8.72e-39, 7.35e-39, 7.26e-39, 6.7e-39,]]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions