opencvflannpython

How to implement Cosine Metric to opencv flann (knnsearch)


I am using opencv flann index to search image in database. as i see in the documentation, flann provides metrics as below:

   FLANN_DIST_EUCLIDEAN = 1,
    FLANN_DIST_L2 = 1,
    FLANN_DIST_MANHATTAN = 2,
    FLANN_DIST_L1 = 2,
    FLANN_DIST_MINKOWSKI = 3,
    FLANN_DIST_MAX   = 4,
    FLANN_DIST_HIST_INTERSECT   = 5,
    FLANN_DIST_HELLINGER = 6,
    FLANN_DIST_CHI_SQUARE = 7,
    FLANN_DIST_CS         = 7,
    FLANN_DIST_KULLBACK_LEIBLER  = 8,
    FLANN_DIST_KL                = 8,
    FLANN_DIST_HAMMING          = 9,

without cosine distance So, how to implement it into opencv lib? Thanks


Solution

  • This came up a few years ago in the flann github repo. (See this issue.) The gist (as I understood it) is that Cosine Similarity isn't a distance metric. There are deeper explanations in the issue.