Image matching using OpenCV ORB + Android

asked 2018-01-01 13:41:21 -0600

BhanuPrasad gravatar image

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.

edit retag flag offensive close merge delete

Comments

"and then I'm trying to identify which 'code' they photographed using feature detection" -- that seems a broken idea.

feature matching is to find the position of a known object. it won't give you any valid "distance measure", or "similarity" , and you cannot check "unknown" objects with this.

imho, you misunderstood something here.

berak gravatar imageberak ( 2018-01-02 02:48:49 -0600 )edit

which API should i use to get the difference percentage between the two images(one is from camera image another one is from database image).

BhanuPrasad gravatar imageBhanuPrasad ( 2018-01-02 04:46:15 -0600 )edit

no idea, without knowing more about, what you're trying to achieve. "codes" -- like in qr or bar codes or markers ? -- use a specialized api, like zbar

berak gravatar imageberak ( 2018-01-02 04:53:54 -0600 )edit

I have some leaf images in my database(those are static images), Now the end-user can capture the image by using his webcam, before saving the image into database, i want to check is there any similar image already exist in database(if it matches at lease 50% ) I want to tell the end-user there is some similar images already exist in the database. Please help me out...to choose the right approach . Thanks in advance

BhanuPrasad gravatar imageBhanuPrasad ( 2018-01-02 05:15:43 -0600 )edit