OpenCV学习笔记:FLANN特征匹配
本次给出FLANN特征匹配的Java实现。
[简介]
特征匹配记录下目标图像与待匹配图像的特征点(KeyPoint),并根据特征点集合构造特征量(descriptor),对这个特征量进行比较、筛选,最终得到一个匹配点的映射集合。我们也可以根据这个集合的大小来衡量两幅图片的匹配程度。
特征匹配与模板匹配不同,由于是计算特征点集合的相关度,转置操作对匹配影响不大,但它容易受到失真、缩放的影响。
[特征匹配]
FeatureMatching.java:
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.highgui.Highgui;
import com.thrblock.opencv.fm.view.ConsoleView;
import com.thrblock.opencv.fm.view.MatchingView;
public class FeatureMatching {
private Mat src;
private MatOfKeyPoint srcKeyPoints;
private Mat srcDes;
private FeatureDetector detector;
private DescriptorExtractor extractor;
private DescriptorMatcher matcher;
private MatchingView view;
public FeatureMatching(MatchingView view) {
this.view = view;
srcKeyPoints = new MatOfKeyPoint();
srcDes = new Mat();
detector = FeatureDetector.create(FeatureDetector.SURF);
extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
}
public int doMaping(String dstPath) {
view.setDstPic(dstPath);
// 读入待测图像
Mat dst = Highgui.imread(dstPath);
System.out.println("DST W:"+dst.cols()+" H:" + dst.rows());
// 待测图像的关键点
MatOfKeyPoint dstKeyPoints = new MatOfKeyPoint();
detector.detect(dst, dstKeyPoints);
// 待测图像的特征矩阵
Mat dstDes = new Mat();
extractor.compute(dst, dstKeyPoints, ds