How to Detect the key points of an image using OpenCV Java library?


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

You can draw the draw key points on the image using the drawKeypoints() method of the org.opencv.features2d.Features2d class.

Note

  • Since Feature2D is an abstract class you need to instantiate one of its subclasses to invoke the detect() method. Here we have used the FastFeatureDetector class.

  • Features2D and Features2d are two different classes of the package features2d don’t get confused...

Example

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Scalar;
import org.opencv.features2d.FastFeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.highgui.HighGui;z
import org.opencv.imgcodecs.Imgcodecs;
public class DetectingKeyPoints{
   public static void main(String args[]) throws Exception {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      //Reading the contents of the image
      String file ="D:\Images\javafx_graphical.jpg";
      Mat src = Imgcodecs.imread(file);
      //Reading the key points of the image
      Mat dst = new Mat();
      MatOfKeyPoint matOfKeyPoints = new MatOfKeyPoint();
      FastFeatureDetector featureDetector = FastFeatureDetector.create();
      featureDetector.detect(src, matOfKeyPoints);
      //Drawing the detected key points
      Features2d.drawKeypoints(src, matOfKeyPoints, dst, new Scalar(0, 0, 255));
      HighGui.imshow("Feature Detection", dst);
      HighGui.waitKey();
   }
}

Input Image

Output

Updated on: 10-Apr-2020

682 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements