如何使用 OpenCV Java 庫匹配兩張影像的關鍵點?
org.opencv.features2d.Feature2D(抽象)類的detect()方法檢測給定影像的關鍵點。此方法需要傳遞一個表示源影像的Mat物件和一個空MatOfKeyPoint物件來儲存讀取的關鍵點。
org.opencv.features2d.Feature2D類的drawMatches()方法查詢兩個給定影像的關鍵點之間的匹配項並繪製它們。此方法接受以下引數:
src1 - 表示第一個源影像的Mat類物件。
keypoints1 - 表示第一個源影像的關鍵點的MatOfKeyPoint類物件。
src2 - 表示第二個源影像的Mat類物件。
keypoints2 - 表示第二個源影像的關鍵點的MatOfKeyPoint類物件。
matches1to2 - 從第一個影像到第二個影像的匹配項,這意味著 keypoints1[i] 在 keypoints2[matches[i]] 中有一個對應的點。
dst - 表示目標影像的Mat類物件。
因此,要匹配兩張影像的關鍵點,需要:
使用imread()方法讀取兩個源影像。
使用detect()方法獲取兩個影像的關鍵點。
使用drawMatches()方法查詢並繪製匹配項。
示例
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();
}
}輸入影像
影像1 -

影像2 -

輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP