0

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?

DeborahAnn
  • 191
  • 1
  • 11

1 Answers1

1
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 !!

Shubham Jaiswal
  • 359
  • 1
  • 9
  • Thank you, it did help a lot! Why did you imported the LinkedList in the MatOfPoint2f though? Isn't better if I iterate directly on the LinkedList? Also, can you help me out a little bit more? I basically need to choose one Point between all of these and perform a touch event at those coordinates. If image 1 contains a similar subset version of the image 2, for instance the instagram app icon, I want to perform a touch event at the coordinates where the instagram app is located. – DeborahAnn Dec 09 '19 at 16:49
  • The idea behind using MatofPoint2f was to use it for establishing a homography, if need be. For your second question, can u share sample images. When you establish a homography between your images, you get an output of a mapping of coordinates between the two images. You can use it to extract your region of interest and thereafter mark that area with a bounding rectangle or any shape of your choice to help perform the touch event. – Shubham Jaiswal Dec 09 '19 at 17:19
  • Thank you! I was able to get my region of interest, marked the area with a bounding rectangle and perform the touch event. I used template matching instead of the BRISK keypoint detector and descriptor extractor. But now my problem is that I don't know how to adjust the threshold, because even though my code mark properly the matching area, it happens that sometimes mark a random rectangle even though the two images have no real match. I opened a new question: https://stackoverflow.com/questions/59273899/how-to-adjust-the-threshold-for-template-matching-in-opencv-java – DeborahAnn Dec 10 '19 at 19:13