Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Image matching using OpenCV ORB + Android

I'm writing an app in which the user takes a photograph of a 'code', and then I'm trying to identify which 'code' they photographed using feature detection in OpenCV. The user can take a photograph, then I iterate through all the stored 'codes' on the device, matching each and selecting the code with the highest number of good matches

 public Integer findMathcingImages(Mat img1, Mat img2) {
    Size sz = new Size(200, 200);
    FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
    DescriptorExtractor descriptor = DescriptorExtractor
            .create(DescriptorExtractor.ORB);

    DescriptorMatcher matcher = DescriptorMatcher
            .create(DescriptorMatcher.BRUTEFORCE_HAMMING);

    Mat descriptors1 = new Mat();
    MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
    detector.detect(img1, keypoints1);
    descriptor.compute(img1, keypoints1, descriptors1);

    Mat descriptors2 = new Mat();
    MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
    detector.detect(img2, keypoints2);
    descriptor.compute(img2, keypoints2, descriptors2);
    Imgproc.cvtColor(img1, img1, Imgproc.COLOR_RGBA2GRAY);
    Imgproc.cvtColor(img2, img2, Imgproc.COLOR_RGBA2GRAY);
    MatOfDMatch matches = new MatOfDMatch();
    matcher.match(descriptors1, descriptors2, matches);
    int DIST_LIMIT = 50;
    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));
        }// end if

    MatOfDMatch matches_final_mat = new MatOfDMatch();
    matches_final_mat.fromList(matches_final);

    Integer good_mathces= matches_final.size();

    Scalar RED = new Scalar(255, 0, 0);
    Scalar GREEN = new Scalar(0, 255, 0);

    Mat outputImg = new Mat();
    MatOfByte drawnMatches = new MatOfByte();

    Features2d.drawMatches(img1, keypoints1, img2, keypoints2,
            matches_final_mat, outputImg, GREEN, RED, drawnMatches,
            Features2d.NOT_DRAW_SINGLE_POINTS);

    Bitmap imageMatched = Bitmap.createBitmap(outputImg.cols(),
            outputImg.rows(), Bitmap.Config.RGB_565);
    Utils.matToBitmap(outputImg, imageMatched);
    Log.d("DISTFILTER", "GoodMathces:" + good_mathces+ "");
    return good_mathces;
}

The problem I'm having is that the wrong image is detected along with the correct images,. AM I doing anything wrong? please help me out.