Ask Your Question

Revision history [back]

Setting the condition or threshold for detecting objects

Good day.

I am currently developing an object recognition app in Android. I'm using feature matching technique using FAST, ORB, and Bruteforce Hamming as the feature extractor, descriptor extractor, and descriptor matcher respectively. I was just wondering what condition or threshold is possible to completely decide if the object is detected or not. Any kind of help or suggestion is very much appreciated.

Here is my code:

    List<String> a = new ArrayList<String>();

    String result;

    String path = Environment.getExternalStorageDirectory().getAbsolutePath();

    Bitmap template = BitmapFactory.decodeFile(path + "/Scene/sample1.jpg");
    Bitmap fast = BitmapFactory.decodeFile(path + "/Scene/1.jpg");

    Mat object = new Mat();
    Mat scene = new Mat();

    Utils.bitmapToMat(template, object);
    Utils.bitmapToMat(fast, scene);

    FeatureDetector FastDetector = FeatureDetector.create(FeatureDetector.FAST);
    DescriptorExtractor OrbExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);

    Mat descriptors1 = new Mat();
    Mat descriptors2 = new Mat();

    MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
    MatOfKeyPoint keypoints2 = new MatOfKeyPoint();

    FastDetector.detect(object, keypoints1);
    FastDetector.detect(scene, keypoints2);

    OrbExtractor.compute(object, keypoints1, descriptors1);
    OrbExtractor.compute(scene, keypoints2, descriptors2);

    MatOfDMatch matches = new MatOfDMatch();

    matcher.match(descriptors1, descriptors2, matches);

    String sMatches = "All Matches: " + matches.size();
    a.add(sMatches);

    List<DMatch> matchesList = matches.toList();

    double maxDistance = 0;
    double minDistance = 100;

    int rowCount = matchesList.size();

    for(int i = 0; i < rowCount; i++){
        double dist = matchesList.get(i).distance;
        if(dist < minDistance){
            minDistance = dist;
        }
        if(dist > maxDistance){
            maxDistance = dist;
        }
    }

    String sMin = "Min Distance: " + minDistance;
    a.add(sMin);
    String sMax = "Max Distance: " + maxDistance;
    a.add(sMax);

    List<DMatch> goodMatchesList = new ArrayList<DMatch>();

    double upperBound = 3 * minDistance;

    for(int i = 0; i < rowCount; i++){
        if(matchesList.get(i).distance < upperBound){
            goodMatchesList.add(matchesList.get(i));
        }
    }

    MatOfDMatch goodMatches = new MatOfDMatch();
    goodMatches.fromList(goodMatchesList);

    String sGM = "Good Matches: " + goodMatches.size();
    a.add(sGM);

    Mat object2 = new Mat();
    Mat scene2 = new Mat();

    Imgproc.cvtColor(object, object2, Imgproc.COLOR_RGBA2RGB, 1);
    Imgproc.cvtColor(scene, scene2, Imgproc.COLOR_RGBA2RGB, 1);

    Mat img_matches = scene2.clone();

    Features2d.drawMatches(object2, keypoints1, scene2, keypoints2, goodMatches, img_matches, new Scalar(255,0,0), new Scalar(0,0,255), new MatOfByte(), 2);

    Bitmap output = Bitmap.createBitmap(img_matches.cols(), img_matches.rows(), Bitmap.Config.RGB_565);
    Utils.matToBitmap(img_matches, output);
    text.setText(Arrays.toString(a.toArray()));

    return output;