improve featureMatching with SIFT and Flann

382 views Asked by At

I'm trying to match this logo with this image (they are 2 different images)

The code manage to find keypoints right but he doesn't match them right between the two images. The image above has shows keypoints as well

Code:

Before this part,I was loading the two images and extracting keypoints from each one of them using SIFT. So sceneDescripters is the keypoints descriptors for the first image, and templateDescripters is the descriptors for the second image.

In this part I'm matching the keypoints from each image using flann.

    MatOfDMatch matchs = new MatOfDMatch();
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    matcher.match(sceneDescripters, templateDescripters, matchs);

finding the min distance, and max distance. then renaming only the matches with small enough distance between them.

    DMatch[] matchesArr = matchs.toArray();

    double minDist = Double.MAX_VALUE; 
    double maxDist = 0;
    for(int i =0; i< matchesArr.length; i++)
    {
        double dist = matchesArr[i].distance;
        if(dist < minDist)
            minDist = dist;
        else if(dist > maxDist)
            maxDist = dist;
    }


    final double threasholdFactor = 2.0;
    List<DMatch> bestMatches = new Vector<DMatch>();
    for( int i = 0; i < matchesArr.length; i++ )
    {
        if( matchesArr[i].distance <= threasholdFactor*minDist )
        {
            bestMatches.add(matchesArr[i]); 
        }

    }

    MatOfDMatch bests = new MatOfDMatch();
    bests.fromList(bestMatches);

    Mat matchedImage = new Mat(scene.rows(), scene.cols()*2, scene.type());
    Features2d.drawMatches(scene, sceneKeyPoints, template, templateKeyPoints,bests , matchedImage);

    Imgcodecs.imwrite(resultImgPath, matchedImage);

And this is the result image

I gave it a small distance threshold so you could see that it gives only false matches. with an higher distance threshold it will return too many matches.

0

There are 0 answers