Ask Your Question

Revision history [back]

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:

image description

thanks in advance :)