Ask Your Question

BRISK Detector with RANSAC

asked 2016-02-03 15:12:22 -0500

MMOSX gravatar image

updated 2016-02-04 10:15:15 -0500

HI, I find the keypoint and the match between two images with BRISK ( I have to use it ) but now I need to use RANSAC to estimate the correspondence better than the only matcher function. Can you help me?

I think: I take the position of keypoint in the object and in the scene and calculate Homography with it, but i don't now how to continue.

Please help me.

This is my code:

Bitmap Matches(){
    FeatureDetector detector;
    MatOfKeyPoint keypoints1, keypoints2;
    DescriptorExtractor descriptorExtractor;
    Mat descriptors1, descriptors2;
    DescriptorMatcher descriptorMatcher;
    MatOfDMatch matches = new MatOfDMatch();
    keypoints1 = new MatOfKeyPoint();
    keypoints2 = new MatOfKeyPoint();
    descriptors1 = new Mat();
    descriptors2 = new Mat();
    detector = FeatureDetector.create(FeatureDetector.BRISK);
    descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.BRISK);
    descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
    detector.detect(immagine2, keypoints2);

    descriptorExtractor.compute(immagine1, keypoints1, descriptors1);
    descriptorExtractor.compute(immagine2, keypoints2, descriptors2);
    descriptorMatcher.match(descriptors2, descriptors1, matches);

    //calculate max and min distances between keypoints
    double max_dist=0;double min_dist=99;

    List<DMatch> matchesList = matches.toList();
    for(int i=0;i<descriptors2.rows();i++)
        double dist = matchesList.get(i).distance;
        if (dist<min_dist) min_dist = dist;
        if (dist>max_dist) max_dist = dist;

    //set up good matches, add matches if close enough
    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    MatOfDMatch gm = new MatOfDMatch();
    for (int i=0;i<descriptors1.rows();i++)

    //put keypoints mats into lists
    List<KeyPoint> keypoints1_List = keypoints1.toList();
    List<KeyPoint> keypoints2_List = keypoints2.toList();

    //put keypoints into point2f mats so calib3d can use them to find homography
    LinkedList<Point> objList = new LinkedList<Point>();
    LinkedList<Point> sceneList = new LinkedList<Point>();
    for(int i=0;i<good_matches.size();i++)
    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();

    //output image
    Mat outputImg = new Mat();
    MatOfByte drawnMatches = new MatOfByte();
    Features2d.drawMatches(immagine1, keypoints1, immagine2, keypoints2, gm, outputImg,
            Scalar.all(-1), Scalar.all(-1), drawnMatches,Features2d.NOT_DRAW_SINGLE_POINTS);

    //run homography on object and scene points
    Mat H = Calib3d.findHomography(obj, scene,Calib3d.RANSAC, 10);

    // trovo il contorno dell'oggetto
    Mat edges = new Mat();
    Mat hierarchy = new Mat();

    // ogni contorno è una lista di punti
    List<MatOfPoint> contourList = new ArrayList<MatOfPoint>(); // lista dei contorni

    Imgproc.Canny(immagine1_grey, edges, 10, 100);
    // Applico la trasformazione all'immagine
    Mat bordi = new Mat(edges.cols(),edges.rows(),CvType.CV_32FC2);
    //Trovo i contorni

    MatOfPoint2f approxCurve = new MatOfPoint2f();
    MatOfPoint points = null;
    //For each contour found
    for (int i=0; i<contourList.size(); i++)
        //Convert contours(i) from MatOfPoint to MatOfPoint2f
        MatOfPoint2f contour2f = new MatOfPoint2f( contourList.get(i).toArray() );
        //Processing on mMOP2f1 which is in type MatOfPoint2f
        double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
        Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

        //Convert back to MatOfPoint
        points = new MatOfPoint( approxCurve.toArray() );

        // Get bounding rect of contour
        Rect rect = Imgproc.boundingRect(points);

        // draw enclosing rectangle (all same color, but you could use variable i to make them ...
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2016-02-04 03:23:05 -0500

Eduardo gravatar image

updated 2016-02-04 03:26:02 -0500

These tutorials could help you:

You can get the list of inliers from findHomography with a RANSAC method.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-02-03 15:12:22 -0500

Seen: 256 times

Last updated: Feb 04 '16