如何使用 OpenCV Java 庫檢測影像的關鍵點?


org.opencv.features2d.Feature2D(抽象)類的detect()方法檢測給定影像的關鍵點。對該方法,你需要傳入一個表示源影像的Mat物件和一個空的MatOfKeyPoint物件以儲存讀取到的關鍵點。

你可以使用org.opencv.features2d.Features2d類的drawKeypoints()方法在影像上繪製關鍵點。

注意

  • 由於 Feature2D 是一個抽象類,你需要例項化其子類之一來呼叫 detect() 方法。此處我們使用了 FastFeatureDetector 類。

  • Features2DFeatures2d是 features2d 包的兩個不同的類,不要弄混了...

示例

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

輸入影像

輸出

更新於: 10-Apr-2020

825 次瀏覽

開啟您的職業生涯

完成學習以獲得認證

開始學習
廣告