My code is working good but when it extracts keypoints, it matches poorly the two images.
Here you can find my code, but I don\'t know how to draw good matched in JAVA
Your code should be like this:
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);;
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
//first image
Mat img1 = Highgui.imread("<image1 path>");
Mat descriptors1 = new Mat();
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
detector.detect(img1, keypoints1);
descriptor.compute(img1, keypoints1, descriptors1);
//second image
Mat img2 = Highgui.imread("<image2 path>");
Mat descriptors2 = new Mat();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
detector.detect(img2, keypoints2);
descriptor.compute(img2, keypoints2, descriptors2);
//matcher should include 2 different image's descriptors
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors1,descriptors2,matches);
//feature and connection colors
Scalar RED = new Scalar(255,0,0);
Scalar GREEN = new Scalar(0,255,0);
//output image
Mat outputImg = new Mat();
MatOfByte drawnMatches = new MatOfByte();
//this will draw all matches, works fine
Features2d.drawMatches(img1, keypoints1, img2, keypoints2, matches,
outputImg, GREEN, RED, drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);
Also if you want to display just features, you can add this code:
Mat featuredImg = new Mat();
Scalar kpColor = new Scalar(255,159,10);//this will be color of keypoints
//featuredImg will be the output of first image
Features2d.drawKeypoints(img1, keypoints1, featuredImg , kpColor, 0);
//featuredImg will be the output of first image
Features2d.drawKeypoints(img1, keypoints1, featuredImg , kpColor, 0);
Then you can show matched points like this:
Bitmap imageMatched = Bitmap.createBitmap(outputImg.cols(), outputImg.rows(), Bitmap.Config.RGB_565);//need to save bitmap
Utils.matToBitmap(outputImg, imageMatched);
ImageView.setImageBitmap(imageMatched);
Eventually you can implement good matches. I hope this thread will be helpful.
Good Matches method is basing on removing from your MatOfDMatch matches = new MatOfDMatch();
list matched points that has different descriptors or spacial location. What I suggest to do is to loop over matches list and put to the new list matches that are satisfy conditions like:
int DIST_LIMIT = 80;
List<DMatch> matchesList = matches.toList();
List<DMatch> matches_final= new ArrayList<DMatch>();
for(int i=0; i<matchesList.size(); i++)
if(matchesList .get(i).distance <= DIST_LIMIT){
matches_final.add(matches.toList().get(i));
}
}
MatOfDMatch matches_final_mat = new MatOfDMatch();
matches_final_mat.fromList(matches_final);
Same thing you can do with matched points coordinates. Here is the useful link.