有2个图像,我为每个图像找到ORB描述符。接下来,我通过距离和RANSAC算法对描述符进行过滤,但事实证明并不是耳朵指示徽标等。问题是什么?
下面是代码:
public static void compareFeature(String filename1, String filename2) {
ORB orb = ORB.create();
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
Mat img1 = Imgcodecs.imread(filename1, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Mat descriptors1 = new Mat();
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
orb.detectAndCompute(img1, new Mat(), keypoints1, descriptors1);
// second image
Mat img2 = Imgcodecs.imread(filename2, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Mat descriptors2 = new Mat();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
orb.detectAndCompute(img2, new Mat(), keypoints2, descriptors2);
// MATCHING
// match these two keypoints sets
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors1, descriptors2, matches);
List<DMatch> matchesList = matches.toList();
// ratio test
LinkedList<DMatch> good_matches = new LinkedList<>();
Double max_dist = 0.0;
Double min_dist = 100.0;
for (DMatch aMatchesList1 : matchesList) {
Double dist = (double) aMatchesList1.distance;
if (dist < min_dist)
min_dist = dist;
if (dist > max_dist)
max_dist = dist;
}
for (DMatch aMatchesList : matchesList) {
if (aMatchesList.distance <= (1.5 * min_dist))
good_matches.addLast(aMatchesList);
}
// get keypoint coordinates of good matches to find homography and remove outliers using ransac
List<Point> pts1 = new ArrayList<>();
List<Point> pts2 = new ArrayList<>();
for (DMatch good_matche : good_matches) {
pts1.add(keypoints1.toList().get(good_matche.queryIdx).pt);
pts2.add(keypoints2.toList().get(good_matche.trainIdx).pt);
}
// convertion of data types - there is maybe a more beautiful way
Mat outputMask = new Mat();
MatOfPoint2f pts1Mat = new MatOfPoint2f();
pts1Mat.fromList(pts1);
MatOfPoint2f pts2Mat = new MatOfPoint2f();
pts2Mat.fromList(pts2);
// Find homography - here just used to perform match filtering with RANSAC, but could be used to e.g. stitch images
// the smaller the allowed reprojection error (here 15), the more matches are filtered
System.out.println("pts1mat " + pts1Mat.size());
System.out.println("pts2mat " + pts2Mat.size());
Mat Homog = Calib3d.findHomography(pts1Mat, pts2Mat, Calib3d.RANSAC, 15, outputMask, 2000, 0.995);
System.out.println("pts1mat " + pts1Mat.size());
System.out.println("pts2mat " + pts2Mat.size());
// outputMask contains zeros and ones indicating which matches are filtered
LinkedList<DMatch> better_matches = new LinkedList<DMatch>();
for (int i = 0; i < good_matches.size(); i++) {
if (outputMask.get(i, 0)[0] != 0.0) {
better_matches.add(good_matches.get(i));
}
}
// DRAWING OUTPUT
Mat outputImg = new Mat();
// this will draw all matches, works fine
MatOfDMatch better_matches_mat = new MatOfDMatch();
better_matches_mat.fromList(better_matches);
Features2d.drawMatches(img1, keypoints1, img2, keypoints2, better_matches_mat, outputImg);
// save image
Imgcodecs.imwrite("result.jpg", outputImg);
System.out.println("better_matches_mat " + better_matches_mat.size());
它不能正常工作。告诉我要解决的问题,要解决的问题,以便一切正常。以及如何比较类似图像的描述符?