Home >Java >javaTutorial >How to match keypoints of two images using OpenCV Java library?

How to match keypoints of two images using OpenCV Java library?

王林
王林forward
2023-09-08 09:21:01729browse

org.opencv.features2d.Feature2D (Abstract) class’s detect() method detects key points of a given image. For this method, you need to pass a Mat object representing the source image and an empty MatOfKeyPoint object to hold the read keypoints.

org.opencv.features2d.Feature2D The strong>drawMatches() method of class finds matches between key points of two given images and draws them. This method accepts the following parameters -

  • src1 - an object source image representing the first Mat class.

  • keypoints1 - Object of class MatOfKeyPoint, representing the key point of the first source image.

  • src2 - An object of class Mat representing the second source image.

  • keypoints2 - Object of class MatOfKeyPoint, representing the key points of the second source image.

  • matches1to2 strong> - a match from the first image to the second image, meaning keypoints1[i] is in keypoints2[matches[i] ] There are corresponding points in .

  • dst - An object of Mat class representing the target image.

So, matching the key points of two images -

  • Read the two sources using the imread() method image.

  • Get the key points of the image and use the detect() method to draw two images.

  • Use the drawMatches() method to find and draw matches.

Example

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.features2d.FastFeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
public class MatchingKeypoints {
   public static void main(String args[]) throws Exception {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      //Reading the source images
      String file1 ="D:\Images\feature1.jpg";
      Mat src1 = Imgcodecs.imread(file1);
      String file2 ="D:\Images\feature2.jpg";
      Mat src2 = Imgcodecs.imread(file2);
      //Creating an empty matrix to store the destination image
      Mat dst = new Mat();
      FastFeatureDetector detector = FastFeatureDetector.create();
      //Detecting the key points in both images
      MatOfKeyPoint keyPoints1 = new MatOfKeyPoint();
      detector.detect(src1, keyPoints1);
      MatOfKeyPoint keyPoints2 = new MatOfKeyPoint();
      detector.detect(src2, keyPoints2);
      MatOfDMatch matof1to2 = new MatOfDMatch();
      Features2d.drawMatches(src1, keyPoints1, src2, keyPoints2, matof1to2, dst);
      HighGui.imshow("Feature Matching", dst);
      HighGui.waitKey();
   }
}

Input image

Image1 -

如何使用OpenCV Java库匹配两个图像的关键点?

Image2 -

如何使用OpenCV Java库匹配两个图像的关键点?

Output

如何使用OpenCV Java库匹配两个图像的关键点?

The above is the detailed content of How to match keypoints of two images using OpenCV Java library?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete