网络异常,图片无法展示
|
代码找了好多,但是没有java版本的
网络异常,图片无法展示
|
网络异常,图片无法展示
|
public static void TestSift(Mat image1,Mat image2) { Mat clone1=image1.clone(); Mat src1=image1.clone(); Mat clone2=image2.clone(); Mat src2=image2.clone(); Imgproc.GaussianBlur(clone1, clone1, new Size(3, 3), 0, 0); Imgproc.cvtColor(clone1, clone1,Imgproc.COLOR_BGR2GRAY); Imgproc.GaussianBlur(clone2, clone2, new Size(3, 3), 0, 0); Imgproc.cvtColor(clone2, clone2,Imgproc.COLOR_BGR2GRAY); MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); Mat des1=new Mat(); Mat des2=new Mat(); // 匹配的点数 SIFT sift=SIFT.create(200); //提取对象关键点 // sift.detect(clone1,keypoints1); // sift.detect(clone2,keypoints2); //提取描述子 // sift.compute(clone1,keypoints1, des1); // sift.compute(clone2,keypoints2, des2); sift.detectAndCompute(clone1, new Mat(), keypoints1, des1); sift.detectAndCompute(clone2, new Mat(), keypoints2, des2); MatOfDMatch md=new MatOfDMatch(); FlannBasedMatcher matcher=FlannBasedMatcher.create(); matcher.match(des1, des2, md); double maxDist = 0; double minDist = 50; DMatch[] mats = md.toArray(); List<DMatch> bestMatches= new ArrayList<DMatch>(); for (int i = 0; i < mats.length; i++) { double dist = mats[i].distance; if (dist < minDist) { minDist = dist; } if (dist > maxDist) { maxDist = dist; } } System.out.println("max_dist : "+maxDist); System.out.println("min_dist : "+minDist); double threshold = 3 * minDist; double threshold2 = 2 * minDist; if (threshold2 >= maxDist){ threshold = minDist * 1.1; } else if (threshold >= maxDist){ threshold = threshold2 * 1.4; } if(0d==threshold) { threshold=0.3*maxDist; } System.out.println("Threshold : "+threshold); for (int i = 0; i < mats.length; i++) { //distance越小,代表DMatch的匹配率越高, Double dist = (double) mats[i].distance; System.out.println(String.format(i + " match distance best : %s", dist)); if (dist <= threshold) { bestMatches.add(mats[i]); System.out.println(String.format(i + " best match added : %s", dist)); } } md.fromList(bestMatches); Mat result=new Mat(); Features2d.drawMatches(src1, keypoints1, src2, keypoints2, md, result); HighGui.imshow("SIFT", result); } 复制代码
knn写法
public static void TestSiftKnn(Mat image1,Mat image2) { //FastFeatureDetector detector = FastFeatureDetector.create(FastFeatureDetector.FAST_N); Mat clone1=image1.clone(); Mat src1=image1.clone(); Mat clone2=image2.clone(); Mat src2=image2.clone(); Imgproc.GaussianBlur(clone1, clone1, new Size(3, 3), 0, 0); Imgproc.cvtColor(clone1, clone1,Imgproc.COLOR_BGR2GRAY); Imgproc.GaussianBlur(clone2, clone2, new Size(3, 3), 0, 0); Imgproc.cvtColor(clone2, clone2,Imgproc.COLOR_BGR2GRAY); MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); Mat des1=new Mat(); Mat des2=new Mat(); SIFT sift=SIFT.create(200); //提取对象关键点 // sift.detect(clone1,keypoints1); // sift.detect(clone2,keypoints2); //提取描述子 // sift.compute(clone1,keypoints1, des1); // sift.compute(clone2,keypoints2, des2); sift.detectAndCompute(clone1, new Mat(), keypoints1, des1); sift.detectAndCompute(clone2, new Mat(), keypoints2, des2); MatOfDMatch md=new MatOfDMatch(); FlannBasedMatcher matcher=FlannBasedMatcher.create(); //matcher.match(des1, des2, md); List<Mat> matList=new ArrayList<Mat>(); List<MatOfDMatch> mdList=new ArrayList<MatOfDMatch>(); matList.add(des1); matcher.add(matList); matcher.train(); matcher.knnMatch(des2, mdList, 2); List<DMatch> dMatchList = new ArrayList<DMatch>(); Iterator<MatOfDMatch> ite=mdList.iterator(); while(ite.hasNext()) { MatOfDMatch next=ite.next(); if (next.toArray()[0].distance < 0.2 * next.toArray()[1].distance) { System.out.println(next.toArray()[0].distance+"->"+next.toArray()[1].distance); dMatchList.add(next.toArray()[0]); }else { ite.remove(); } } md.fromList(dMatchList); Mat result=new Mat(); // Features2d.drawMatches(src1, keypoints1, src2, keypoints2, md, result); Features2d.drawMatchesKnn(src1, keypoints1, src2, keypoints2, mdList, result); HighGui.imshow("SIFT", result); }