I'm trying to match 2 opposite images using OpenCV's DescriptorMatcher with no luck. The images are: http://i61.tinypic.com/28whu0g.jpg (left to right) and http://i61.tinypic.com/x35vte.jpg (right to left).
My code is pretty like alot of examples that I saw in StackOverflow and the web but still I always get no match.
String firstImageSourcePath = "RTL_IMAGE_PATH";
String secondImageSourcePath = "LTR_IMAGE_PATH";
Mat firstImageSrcImgMat = Highgui.imread(firstImageSourcePath);
Mat secondImageSrcImgMat = Highgui.imread(firstImageSourcePath);
if (firstImageSrcImgMat.empty() || secondImageSrcImgMat.empty()) {
System.out.println("Failed to load images");
return;
}
System.out.println("Loaded image at " + firstImageSourcePath + " and " + secondImageSourcePath);
FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.BRISK);
MatOfKeyPoint firstImgMatOfKeyPoints = new MatOfKeyPoint();
MatOfKeyPoint secondImgMatOfKeyPoints = new MatOfKeyPoint();
featureDetector.detect(firstImageSrcImgMat, firstImgMatOfKeyPoints);
featureDetector.detect(secondImageSrcImgMat, secondImgMatOfKeyPoints);
System.out.println("Detected " + firstImgMatOfKeyPoints.size() + " and " + secondImgMatOfKeyPoints + " blobs in the images");
List<KeyPoint> firstImgKeyPoints = firstImgMatOfKeyPoints.toList();
List<KeyPoint> secondImgKeyPoints = secondImgMatOfKeyPoints.toList();
System.out.println("First Image key points: " + firstImgKeyPoints);
System.out.println("Second Image key points: " + secondImgKeyPoints);
Mat firstImgDescriptors = new Mat();
Mat secondImgDescriptors = new Mat();
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.BRISK);
extractor.compute(firstImageSrcImgMat, firstImgMatOfKeyPoints, firstImgDescriptors);
extractor.compute(secondImageSrcImgMat, secondImgMatOfKeyPoints, secondImgDescriptors);
System.out.println("descriptorsA.size() : " + firstImgDescriptors.size());
System.out.println("descriptorsB.size() : " + secondImgDescriptors.size());
MatOfDMatch matches = new MatOfDMatch();
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT); // BRUTEFORCE_HAMMINGLUT
matcher.match(firstImgDescriptors, secondImgDescriptors, matches);
System.out.println("matches.size() : " + matches.size());
System.out.println("matches : " + matches);
MatOfDMatch matchesFiltered = new MatOfDMatch();
List<DMatch> matchesList = matches.toList();
List<DMatch> bestMatches = new ArrayList<DMatch>();
Double max_dist = 0.0;
Double min_dist = 100.0;
for (int i = 0; i < matchesList.size(); i++) {
Double dist = (double) matchesList.get(i).distance;
if (dist > 0)
System.out.println("dist : " + dist);
if (dist < min_dist && dist != 0) {
min_dist = dist;
}
if (dist > max_dist) {
max_dist = dist;
}
}
System.out.println("max_dist : " + max_dist);
System.out.println("min_dist : " + min_dist);
if (min_dist > 50) {
System.out.println("No match found, min_dist under minimum value");
return;
}
double threshold = 3 * min_dist;
double threshold2 = 2 * min_dist;
if (threshold > 75) {
threshold = 75;
} else if (threshold2 >= max_dist) {
threshold = min_dist * 1.1;
} else if (threshold >= max_dist) {
threshold = threshold2 * 1.4;
}
System.out.println("Threshold : " + threshold);
for (int i = 0; i < matchesList.size(); i++) {
Double dist = (double) matchesList.get(i).distance;
if (dist < threshold) {
bestMatches.add(matches.toList().get(i));
System.out.println(String.format(i + " best match added : %s", dist));
}
}
matchesFiltered.fromList(bestMatches);
System.out.println("matchesFiltered.size() : " + matchesFiltered.size());
if (matchesFiltered.rows() >= 1) {
System.out.println("match found");
} else {
System.out.println("match not found");
}
any hint what am I doing wrong?