Image comparing using "ORB" opencv for android

asked 2018-11-04 11:18:10 -0600

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

berak gravatar image

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

}

edit retag flag offensive close merge delete

Comments

sorry dear, but you're "on the wrong bus" here.

feature matching (as it is in the tutorials, please look again !) is meant to find a homography between known parts of images (e.g. if you want to find matching parts of 2 images for stitching).

it was never intended to be used for image similarity

also "image similarity" is a far too broad term here, what are you trying to achieve, exactly ?

(right now, this is clearly an XY-problem)

berak gravatar imageberak ( 2018-11-04 11:24:16 -0600 )edit

Sorry for that, i am new to OpenCV but i have background in android development, my goal is to make an android application that searches by image in online database (MySQL), so the user can select an image from gallery of the phone and then the app will return the similar images from database, in other words ( content-based image retrieval using deep learning) , so i was starting from the above example by trying comparing two images, so do you have any idea to guide me in order make my app? thank you in advance.

ahmad14 gravatar imageahmad14 ( 2018-11-04 11:37:15 -0600 )edit

i have background in android development

yea, at least you seem to be quick at porting c++ code to java ;)

but again, define "similarity" (that's far too broad here !)

are you looking for "object detection" (we have a banana here) ? "close duplicates" (a gray scale img of the eiffel tower vs. a color one) ?

try to look up "content based image retrieval" (CBIR) is that it ?

different goals need different measures, there's no "one-size-fits-all", so be as concise as possible about it, please.

(and put anything about databases on hold, until you know what you need. maybe it's a machine-learning model, not images ?)

berak gravatar imageberak ( 2018-11-04 11:53:45 -0600 )edit

problem again is -- you have a computer-vision problem, but no such skills.

berak gravatar imageberak ( 2018-11-04 12:03:57 -0600 )edit

ok, what i am looking for, for example retrieving social image like birthday or graduations etc... that means if the user have an image that is related to a specific event, and there are other images of this event that are similar in visual content of the image , so he can search by the image he have in order to retrieve other images

ahmad14 gravatar imageahmad14 ( 2018-11-04 12:04:34 -0600 )edit

sorry for bad english

ahmad14 gravatar imageahmad14 ( 2018-11-04 12:05:41 -0600 )edit

@ahmad14, lol, that's even more difficult, because it's more a "concept".

sorry to say so, but you won't be able to do this AT ALL, now. (given your entire lack of background knowledge)

take a few courses in machine learning, invest 2 more years, come back, really.

right now, it's all "hot air" only (sorry for the harsh verdict, again).

berak gravatar imageberak ( 2018-11-04 12:08:06 -0600 )edit

it's not your english, which is bad, it's your current skillset, (you don't know, what you're doing).

berak gravatar imageberak ( 2018-11-04 12:10:20 -0600 )edit

haha, no problem, thank you for your attention, i will try to have some tutorials

ahmad14 gravatar imageahmad14 ( 2018-11-04 12:19:31 -0600 )edit