OpenCV: knnMatch function does not work correctly

I am writing an android program to find an object. I am using ORB descriptors. But I need to use the knnMatch function. It doesn't seem to work correctly in my opinion, and I don't know why.

Here's an example of my code:

 .
 .
 Mat frameDescriptors = new Mat();
    FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);  
    List<List<DMatch>> knnMatches=new ArrayList<List<DMatch>>();
.
.
.
detector.detect(mRgba, frameKeypoints);
extractor.compute(mRgba, frameKeypoints, frameDescriptors);  
.
.
.
matcher.knnMatch(imageDescriptors,frameDescriptors, knnMatches, 5);
for(int q=0;q<knnMatches.size();q++){
                Log.d("PPP","Writing for q=" +q);
                for(int w=0;w<knnMatches.get(q).size();w++){
                    Log.d("PPP","Distance for: "+ q + "-" + w + " qIdx: " + knnMatches.get(q).get(w).queryIdx +" tIdx: " +knnMatches.get(q).get(w).trainIdx + " " + knnMatches.get(q).get(w).distance);
                }
}

      

And here is a sample log result:

Writing for q = 0
Distance for: 0-0 qIdx: 206 tIdx: 30 7.0
Distance for: 0-1 qIdx: 206 tIdx: 45 8.0
Distance for: 0-2 qIdx: 206 tIdx: 55 9.0
Distance for: 0-3 qIdx: 206 tIdx: 5 10.0
Distance for: 0-4 qIdx: 206 tIdx: 15 10.0
Writing for q = 1
Distance for: 1-0 qIdx: 206 tIdx: 30 7.0
Distance for: 1-1 qIdx: 206 tIdx: 45 8.0
Distance for: 1-2 qIdx: 206 tIdx: 55 9.0
Distance for: 1-3 qIdx: 206 tIdx: 5 10.0
Distance for: 1-4 qIdx: 206 tIdx: 15 10.0
Writing for q = 2
Distance for: 2-0 qIdx: 206 tIdx: 30 7.0
Distance for: 2-1 qIdx: 206 tIdx: 45 8.0
Distance for: 2-2 qIdx: 206 tIdx: 55 9.0
Distance for: 2-3 qIdx: 206 tIdx: 5 10.0
Distance for: 2-4 qIdx: 206 tIdx: 15 10.0

You can see that everyone is exactly the same trainIdx and every time I get the same distance. I don't know where I am going wrong. Can you help me?

+3


source to share





All Articles