Found a lot of code, but no Java version

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. Create (200); // sift. Detect (clone1,keypoints1); // sift.detect(clone2,keypoints2); Sift.com pute(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; {} the if (d = = 0 threshold) threshold = 0.3 * maxDist; } System.out.println("Threshold : "+threshold); for (int i = 0; i < mats.length; Double dist = (Double) MATS [I]. Distance; Double dist = (Double) MATS [I]. 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); }Copy the code

KNN method

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.com pute(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); }Copy the code