Search code examples
javaopencvimage-processingfeature-detectionfeature-descriptor

How to get pixel coordinates from a list of best matches using openCV (java)?


I am using openCV (java) and I have a list of best matches. Here's my code:

  final MatOfKeyPoint keyPoints1 = new MatOfKeyPoint();
  final MatOfKeyPoint keyPoints2 = new MatOfKeyPoint();
  ...
  MatOfDMatch matches = new MatOfDMatch();
  ...
  List<DMatch> matchesList = matches.toList();
  List<DMatch> bestMatches = new ArrayList<DMatch>();

    for (int i = 0; i < matchesList.size(); i++)
        {
            Double dist = (double) matchesList.get(i).distance;

            if (dist < threshold)
            {
                bestMatches.add(matches.toList().get(i));
            }
        }

I know I have to use the indexes of train and query descriptors but I don't know how. I have tried like these but doesn't work since MatOfKeyPoint can't be accessed like an array in Java.

for(int i = 0; i < bestMatches.size(); i++)
{
    keyPoints1[bestMatches.get(i).queryIdx].pt;
    keyPoints2[bestMatches.get(i).trainIdx].pt;
}

How can I find the pixel coordinates?


Solution

  • LinkedList<Point> queryList = new LinkedList<Point>();
    LinkedList<Point> trainList = new LinkedList<Point>();
    
    List<KeyPoint> keypoints_queryList = keyPoints1.toList();
    List<KeyPoint> keypoints_trainList = keyPoints2.toList();
    
    for (int i = 0; i < bestMatches.size(); i++) {
       queryList.addLast(keypoints_queryList.get(bestMatches.get(i).queryIdx).pt);
       trainList.addLast(keypoints_trainList.get(bestMatches.get(i).trainIdx).pt);
    }
    
    
    MatOfPoint2f queryCoords = new MatOfPoint2f();
    queryCoords.fromList(queryList);
    
    MatOfPoint2f trainCoords = new MatOfPoint2f();
    trainCoords.fromList(trainList);
    

    Hope this helps !!