Home > Java > javaTutorial > body text

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

王林
Release: 2023-09-08 09:21:01
forward
701 people have browsed it

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();
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!