Ask Your Question

Setting the condition or threshold for detecting objects

asked 2017-09-21 10:22:47 -0600

cartmanbrah gravatar image

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();

    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;
    String sMax = "Max Distance: " + maxDistance;

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

    double upperBound = 3 * minDistance;

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

    MatOfDMatch goodMatches = new MatOfDMatch();

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

    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);

    return output;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-09-22 01:09:16 -0600

berak gravatar image

updated 2017-09-22 01:13:22 -0600

you must have misunderstood something,

feature matching != object detection. it was never made for that purpose, and you won't get anywhere like this.

to do a real object detection, you'd have to train a ml model, like a cascade, a hogdescriptor, or a neural network,

all you get from the feature matching is a set of matching features between 2 views of a scene, which is used to get a homography (for alignment, stiching, registration, pose, etc.)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-09-21 10:22:47 -0600

Seen: 420 times

Last updated: Sep 21 '17