Ask Your Question

Revision history [back]

Image comparing using "ORB" opencv for android

hi everyone, 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);

}

click to hide/show revision 2
retagged

updated 2018-11-04 11:58:19 -0600

berak gravatar image

Image comparing using "ORB" opencv for android

hi everyone, 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);

}