org.opencv.features2d.Feature2D (抽象)类的detect()方法检测给定图像的关键点。对于这个方法,你需要传递一个代表源图像的Mat对象和一个空的MatOfKeyPoint 对象来保存读取的关键点。
org.opencv.features2d.Feature2D类的drawMatches()方法找到两个给定图像的关键点之间的匹配并绘制它们。此方法接受以下参数 -
-
src1 -代表第一个源图像的Mat类的对象。
-
keypoints1 -所述的目的MatOfKeyPoint表示第一源图像的关键点类。
-
src2 - 代表第二个源图像的 Mat 类的对象。
-
keypoints2 -所述的目的MatOfKeyPoint表示第二源图像的关键点类。
-
match1to2 - 从第一个图像到第二个图像的匹配,这意味着 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 = "F:\11_1.png";
Mat src1 = Imgcodecs.imread(file1);
String file2 = "F:\11_2.png";
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 -
Image2 -



