Java基于OpenCV的Surf特征检测与匹配

来源:互联网 发布:淘宝口令在线生成器 编辑:程序博客网 时间:2024/05/17 23:01
Mat srcImage = Highgui.imread("e:\\1.jpg");
Mat destImage = Highgui.imread("e:\\2.jpg");


if(srcImage.empty()||destImage.empty()){
System.out.println("图片读写失败");
return;
}

FeatureDetector dectorSurf =  FeatureDetector.create(FeatureDetector.SURF);

MatOfKeyPoint srcKeyPoints=new MatOfKeyPoint();
MatOfKeyPoint destKeyPoints=new MatOfKeyPoint();
dectorSurf.detect(srcImage, srcKeyPoints);
dectorSurf.detect(destImage, destKeyPoints);

DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);

Mat srcDescriptor=new Mat();
Mat descDescriptor=new Mat();
extractor.compute(srcImage, srcKeyPoints, srcDescriptor);
extractor.compute(destImage, destKeyPoints, descDescriptor);

DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

MatOfDMatch matches=new MatOfDMatch();
descriptorMatcher.match(srcDescriptor, descDescriptor, matches);

double maxDistance=0; double minDistance=100;
for(int i=0; i<srcDescriptor.rows(); i++){
if(maxDistance<matches.toList().get(i).distance){
maxDistance = matches.toList().get(i).distance;
}
if(minDistance>matches.toList().get(i).distance){
minDistance = matches.toList().get(i).distance;
}
}

List<DMatch> goodMatchs = new ArrayList<DMatch>();
for(int i=0; i<srcDescriptor.rows(); i++){
if(matches.toList().get(i).distance<3*minDistance){
goodMatchs.add(matches.toList().get(i));
}
}


List<MatOfByte> matchesMask= new ArrayList<MatOfByte>();
Mat outMat = new Mat();

MatOfDMatch goodMatchsALl = new  MatOfDMatch();
goodMatchsALl.fromList(goodMatchs);

List<MatOfDMatch> matchList = new ArrayList<MatOfDMatch>();
matchList.add(goodMatchsALl);

Features2d.drawMatches2(srcImage, srcKeyPoints, destImage, destKeyPoints, matchList, outMat, Scalar.all(-1), Scalar.all(-1), matchesMask, Features2d.NOT_DRAW_SINGLE_POINTS);

Highgui.imwrite("e:\\3.jpg", outMat);
0 0
原创粉丝点击