How to match object in image with SURF on Android
Hello guys,
I am trying to build an application that has a capability to matching an image using SURF, I've succeed to obtain the keypoints and descriptors for each image. but when I try to match it using DescriptorMatcher.FLANNBASED, it did'nt gave the expected output because when I tried to match different image and draw the matches it keep drawing(picture below).
I think I missed something here and it will be great if there is an answer for this question or link/tutorial.
Here is my code :
public void sift() {
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
Mat rgba = new Mat();
Mat rgba1 = new Mat();
Mat output = new Mat();
Utils.bitmapToMat(inputImage, rgba);
Utils.bitmapToMat(queryImage, rgba1);
MatOfKeyPoint keyPoints = new MatOfKeyPoint();
MatOfKeyPoint keyPoints1 = new MatOfKeyPoint();
descriptor = new Mat();
descriptor1 = new Mat();
matches = new MatOfDMatch();
Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGBA2GRAY);
Imgproc.cvtColor(rgba1, rgba1, Imgproc.COLOR_RGBA2GRAY);
//Imgproc.Canny(rgba, rgba, 80, 100);
//Imgproc.equalizeHist(rgba, rgba);
detector.detect(rgba, keyPoints);
detector.detect(rgba1, keyPoints1);
descriptorExtractor.compute(rgba, keyPoints, descriptor);
descriptorExtractor.compute(rgba, keyPoints, descriptor1);
descriptorMatcher.match(descriptor, descriptor1, matches);
//Log.v("Keypoints", "Number of Matches: "+matches.size());
Log.v("Keypoints", "Number of Matches: "+keyPoints.size());
Log.v("Keypoints", "Number of Matches: "+keyPoints1.size());
Features2d.drawMatches(rgba, keyPoints1, rgba1, keyPoints1, matches, output);
Bitmap imageMatched = Bitmap.createBitmap(output.cols(), output.rows(), Bitmap.Config.RGB_565);//need to save bitmap
Utils.matToBitmap(output, imageMatched);
imageView.setImageBitmap(imageMatched);
}
output image:
thanks in advance :)
do you understand, that this is for locating a known (part of a ) scene, not at all for arbitrary object detection ?
"when I tried to match different image and draw the matches it keep drawing" -- yes, indeed. you told it to find 500 keyoints per image, and match the features, and it will just do that, come rain or come shine. it never knows about your intended "objects" in the 1st place.
@berak oh I see, thanks for your comment, I will try to find some sample then, I thought I got it all part. sorry sir. can you point me into some link or tutorial for this?
no, you still misunderstand it, -- this method is not at all feasable for object detection !
(it's not about finding another sample !)
@berak thanks for your insight sir, Actually I plan to use it to matching an image in android app. I've came across some paper/journal use this method for matching an image hence I also tried it after read some research about the theory
let me clarify: you can use such features for object detection, you'll just need an entirely different matching algorithm, like a flann-index (no, that is not the same as the flannbased matcher!) or BagofWords with machine-learning.
unfortunately, none of it is available from the java api, and you won't fnd any ready made snippets or tutorials for this.
@berak ok I got it, thank you sir