'Summary:
This method does a fast local approximate nearest neighbors (FLANN) calculation between two sets
of feature vectors. The result are two numpy arrays the first one is a list of indexes of the
matches and the second one is the match distance value. For the match indices or idx, the index
values correspond to the values of td, and the value in the array is the index in td. I.
I.e. j = idx[i] is where td[i] matches sd[j].
The second numpy array, at the index i is the match distance between td[i] and sd[j].
Lower distances mean better matches.
Parameters:
sd - A numpy array of feature vectors of any size.
td - A numpy array of feature vectors of any size, this vector is used for indexing
and the result arrays will have a length matching this vector.
Returns:
Two numpy arrays, the first one, idx, is the idx of the matches of the vector td with sd.
The second one, dist, is the distance value for the closest match.
Example:
>>> kpt,td = img1._getRawKeypoints() # t is template
>>> kps,sd = img2._getRawKeypoints() # s is source
>>> idx,dist = img1._getFLANNMatches(sd,td)
>>> j = idx[42]
>>> print kps[j] # matches kp 42
>>> print dist[i] # the match quality.
Notes:
If you would prefer to work with the raw keypoints and descriptors each image keeps
a local cache of the raw values. These are named:
self._mKeyPoints # A tuple of keypoint objects
See: http://opencv.itseez.com/modules/features2d/doc/common_interfaces_of_feature_detectors.html#keypoint-keypoint
self._mKPDescriptors # The descriptor as a floating point numpy array
self._mKPFlavor = "NONE" # The flavor of the keypoints as a string.
See:
ImageClass._getRawKeypoints(self,thresh=500.00,forceReset=False,flavor="SURF",highQuality=1)
ImageClass._getFLANNMatches(self,sd,td)
ImageClass.drawKeypointMatches(self,template,thresh=500.00,minDist=0.15,width=1)
ImageClass.findKeypoints(self,min_quality=300.00,flavor="SURF",highQuality=False )
ImageClass.findKeypointMatch(self,template,quality=500.00,minDist=0.2,minMatch=0.4)'

def _getFLANNMatches(self, sd, td):

    try:
        import cv2
    except:
        logger.warning("Can't    run    FLANN    Matches    without    OpenCV    >=    2.3.0")
        return
    FLANN_INDEX_KDTREE = 1
    flann_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=4)
    flann = cv2.flann_Index(sd, flann_params)
    (idx, dist) = flann.knnSearch(td, 1, params={})
    del flann
    return (idx, dist)
