1

I am new to OpenCV and I am trying to write an android code using OpenCV to compare two images for similarities, for my example i loaded two images from Drawable folder as you see in the code, but i am not able to complete the code in order to get a percentage of matching between images and to set a threshold or something? so please can any one help me solving my issue, thank you in advance. below is my Code:

public class MainActivity extends AppCompatActivity {

 //TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    System.loadLibrary("opencv_java3");

   // textView =(TextView)findViewById(R.id.textView);

    Mat m1 = Imgcodecs.imread(ResourcesCompat.getDrawable(getResources(), R.drawable.image1, null).toString());

    Mat m2 = Imgcodecs.imread(ResourcesCompat.getDrawable(getResources(), R.drawable.image2, null).toString());

    //Imgproc.cvtColor(m1, m1, Imgproc.COLOR_RGB2BGRA);
    //Imgproc.cvtColor(m2, m2, Imgproc.COLOR_RGB2BGRA);

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
    MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
    detector.detect(m1, keypoints1);

    FeatureDetector detector2 = FeatureDetector.create(FeatureDetector.ORB);
    MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
    detector2.detect(m2, keypoints2);

    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
    Mat descriptors1 = new Mat();
    extractor.compute(m1, keypoints1, descriptors1);

    DescriptorExtractor extractor2 = DescriptorExtractor.create(DescriptorExtractor.ORB);
    Mat descriptors2 = new Mat();
    extractor2.compute(m2, keypoints2, descriptors2);

    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
    MatOfDMatch matches = new MatOfDMatch();

    matcher.match(descriptors1, descriptors2, matches);


    List<DMatch> matchesList = matches.toList();
    double maxDistance = 0;
    double minDistance = 1000;

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

    List<DMatch> goodMatchesList = new ArrayList<DMatch>();
    double upperBound = 1.6 * 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);

}

}

wisam
  • 57
  • 1
  • 12
  • I'm not sure what do you mean by `percentage of matching between images`. What would you expect it to be for an image of banana and apple or for an image of fresh banana and old banana? – Dmitrii Z. Nov 04 '18 at 18:38
  • hello, what i mean that if the two images for example are for the same object but its taken from another position, like two images for same event like birthday for example, so i need to compare two images to find how similar they are and assign a specific threshold for that. what i am doing is a simple testing code for an search by image application( image search engine ) that takes image from gallery and return similar images from online database. – wisam Nov 04 '18 at 19:46
  • What you are looking for is [DecsriptorMatcher](https://docs.opencv.org/3.4.3/javadoc/org/opencv/features2d/DescriptorMatcher.html). You can take a look at this [findMatches](https://github.com/g-rauhoeft/enchantment/blob/538216243d7d0bdc035ce9e6e375c5f3826fc1e2/src/net/lunareffect/enchantment/ImageMatcher.java#L76) function. For your case 0.75 on line 87 would be a threshold. – Dmitrii Z. Nov 04 '18 at 21:26
  • i tried the function that you mentioned above ( findMatches ) off corse i remove the method and i kept the same functionality of code with replacing variables with my variables, but i it gives me ratio 0.0 even if i used same images exactly to compare – wisam Nov 05 '18 at 21:58
  • I'm 100% sure that it's because you have an error in your code at line #228 & your second image of a banana has more shadows than the first one. – Dmitrii Z. Nov 05 '18 at 22:32

0 Answers0