images matching result [closed]

asked 2014-07-01 10:22:06 -0600

Hello I am new to opencv library and I have a code to process two images if they are same. But at end of the code i can't get good result, such as if images are same or not. Thanks for your help! and this is the code:

public class TemplateMatching { static int fw; static int fh; static String yo; public int run(String inFile, String templateFile) {

    // System.out.println("\nRunning Template Matching");

    Mat img = Highgui.imread(inFile);
    Mat templ = Highgui.imread(templateFile);

FeatureDetector  fd = FeatureDetector.create(FeatureDetector.SURF); 
final MatOfKeyPoint keyPointsLarge = new MatOfKeyPoint();
final MatOfKeyPoint keyPointsSmall = new MatOfKeyPoint();

fd.detect(img, keyPointsLarge);
fd.detect(templ, keyPointsSmall);

// System.out.println("keyPoints.size() : "+keyPointsLarge.size()); //System.out.println("keyPoints2.size() : "+keyPointsSmall.size());

Mat descriptorsLarge = new Mat();
Mat descriptorsSmall = new Mat();

DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.BRIEF);
extractor.compute(img, keyPointsLarge, descriptorsLarge);
extractor.compute(templ, keyPointsSmall, descriptorsSmall);

// System.out.println("descriptorsA.size() : "+descriptorsLarge.size()); // System.out.println("descriptorsB.size() : "+descriptorsSmall.size());

MatOfDMatch matches = new MatOfDMatch();

DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);
matcher.match(descriptorsLarge, descriptorsSmall, matches);

//System.out.println("matches.size() : "+matches.size());

MatOfDMatch matchesFiltered = new MatOfDMatch();

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

Double max_dist = 0.0;
Double min_dist = 100.0;

for (int i = 0; i < matchesList.size(); i++)
{
    Double dist = (double) matchesList.get(i).distance;

    if (dist < min_dist && dist != 0)
    {
        min_dist = dist;
    }

    if (dist > max_dist)
    {
        max_dist = dist;
    }

}

 // System.out.println("max_dist : "+max_dist);

// System.out.println("min_dist : "+min_dist);

if(min_dist > 50 )
{
   // System.out.println("No match found");
  //  System.out.println("Just return ");
    return -1;
}

double threshold = 3 * min_dist;
double threshold2 = 2 * min_dist;

if (threshold > 75)
{
    threshold  = 75;
}
else if (threshold2 >= max_dist)
{
    threshold = min_dist * 1.1;
}
else if (threshold >= max_dist)
{
    threshold = threshold2 * 1.4;
}

//System.out.println("Threshold : "+threshold);

for (int i = 0; i < matchesList.size(); i++)
{
    Double dist = (double) matchesList.get(i).distance;

    if (dist < threshold)
    {
        bestMatches.add(matches.toList().get(i));
        //System.out.println(String.format(i + " best match added : %s", dist));
    }
}
matchesFiltered.fromList(bestMatches);

//System.out.println("matchesFiltered.size() : " + matchesFiltered.size());

if(matchesFiltered.rows() >=21)
{
    //System.out.println("match found");
    return 1;
}
else
{
    return -1;
}

} }

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-06 07:35:48.319783

Comments

How do you define "same"? Pixel-wise? Human perception-wise? Containing same objects?

Witek gravatar imageWitek ( 2014-07-01 16:07:32 -0600 )edit

Thank you Witek.Yes it is exactly what i look for: Containing same objects,because the app will be able to tell if the pictures were took at same place.Thank you.

hounakey gravatar imagehounakey ( 2014-07-01 19:42:56 -0600 )edit

If you want to verify if pictures were taken in the same place, your code is more or less ok. Perhaps you need to play with thresholds and the value '21' in this line:

if(matchesFiltered.rows() >=21)

Why is it set arbitrarily to 21? Check how many good matches you are getting for some scenes that are similar. It will depend strongly on the appearance of objects in the images. Thus, I would rather use a percentage of all matches found here.

Witek gravatar imageWitek ( 2014-07-02 04:54:00 -0600 )edit

but i have no idea of how to match images using threshold.Please can you help some litle or link me to a tutorial that can help me?I have all most seek on the web but i got nothing.

hounakey gravatar imagehounakey ( 2014-07-02 09:16:07 -0600 )edit
Witek gravatar imageWitek ( 2014-07-02 09:42:05 -0600 )edit

Here's a tutorial for using "good features to track"- http://abhishek4273.wordpress.com/2014/07/05/track-the-region-of-interest/

Abhishek Kumar Annamraju gravatar imageAbhishek Kumar Annamraju ( 2014-07-05 00:48:56 -0600 )edit