Phương thức detect() của org.opencv.features2d.Feature2D (abstract) phát hiện key points. Nhận Mat nguồn và MatOfKeyPoint rỗng.
Phương thức drawMatches() của Features2d tìm và vẽ khớp giữa key points 2 ảnh. Tham số:
- src1, keypoints1 - Ảnh 1 và key points.
- src2, keypoints2 - Ảnh 2 và key points.
- matches1to2 - Các cặp khớp (keypoints1[i] ↔ keypoints2[matches[i]]).
- dst - Ảnh đích vẽ kết quả.
Quy trình khớp key points:
- Đọc 2 ảnh nguồn bằng imread().
- Lấy key points bằng detect() (dùng FastFeatureDetector).
- Tìm và vẽ khớp bằng drawMatches().
Ví dụ minh họa
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 {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String file1 = "D:\\Images\\feature1.jpg";
Mat src1 = Imgcodecs.imread(file1);
String file2 = "D:\\Images\\feature2.jpg";
Mat src2 = Imgcodecs.imread(file2);
Mat dst = new Mat();
FastFeatureDetector detector = FastFeatureDetector.create();
// Phát hiện key points
MatOfKeyPoint keyPoints1 = new MatOfKeyPoint();
detector.detect(src1, keyPoints1);
MatOfKeyPoint keyPoints2 = new MatOfKeyPoint();
detector.detect(src2, keyPoints2);
MatOfDMatch matches = new MatOfDMatch();
Features2d.drawMatches(src1, keyPoints1, src2, keyPoints2, matches, dst);
HighGui.imshow("Feature Matching", dst);
HighGui.waitKey();
}
}
Hình ảnh đầu vào và kết quả
Image 1: 
Image 2: 
Output: 