Ask Your Question

image comparison with a database

asked 2013-03-06 20:27:48 -0500

simba1382 gravatar image

updated 2015-12-31 08:51:52 -0500

Im not yet an Open CV user, Ive been using Matlab but Ive kept an eye in OpenCV for a while already. That being said, I would like to know if its even possible to implement one idea I had for a pet project of mine, before delving deep into openCV and really learning the language.

My goal is to compare images (They`re going to have noise) with a database of images, and tell me if it finds a match. For instance:img1 img2

How would I even tell they're both similar? Are there algorithms I can implement to tell me that?

I suppose I should use some sort of noise reduction/edge detection first (I already tried some and had success with edge detection, actually). So, assuming I have a decent edge detection, how could I compare them?

Thanks in advance

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2013-03-07 04:07:08 -0500

Guanta gravatar image

I assume your are looking to get similar images. Hereby, I would not go into edge-detection for image similarities. Imho the typical approach would be a kind of "Bag (visual) of Words (BoW)" approach. It works as follows:

  1. You compute features at keypoints of your image-database.
  2. Cluster them ( i.e. build your vocabulary )
  3. Compute for each image a histogram of features ( i.e. compute nearest match to the vocabulary for each descriptor and count them ). This is your descriptor for an image
  4. The original approach would be learning based: Assign labels to your images and train a classifier with your labels and your descriptors. However, in your case this step could be simplified by just taking the nearest neighbors. So you'd match the descriptors of step 3 and take let's say the 10 nearest image-descriptors.

OpenCV provides a BoW class which basically does step 1-3 (you only need to chose which keypoint-detector and descriptor and matcher you need). If you are more interested why and how BoW works in detail, see for example . If you google around you will probably also find examples for the complete realization with OpenCV.

Another approach which covers step 1-3 is to compute immediatly an image-descriptor without computing local features and cluster them. Maybe the GIST-Descriptor is worth looking into it: . Good luck!

edit flag offensive delete link more


Thanks a LOT, you actually gave me some hope with this message! I had almost given up already, a learning based approach is EXACTLY what I want. Both links you sent me are done in MatLab (Which I'm familiar with, so that's actually good!), that being said do you recommend simply continuing with Matlab or actually swtiching to OpenCV? for performance purposes, that is.

simba1382 gravatar imagesimba1382 ( 2013-03-07 07:41:39 -0500 )edit

That is a difficult question and depends on many factors: 1. The keypoint/descriptor/matchers/classifier you are chosing. Some may be definitly faster using OpenCV since many algorithms are parallelized. This brings me to point 2: The system you use or on which the software is intended to be used. Maybe youn could use algorithms from the GPU or OCL module which would give you a huge gain speed. 3. However all these things don't really matter if you can do your job in much faster time in matlab than coding it in a different language. Note: maybe the shift from Matlab to Python is less difficult than from Matlab to C++ and still is fast enough for your purpose, but think over it thoroughly.

Guanta gravatar imageGuanta ( 2013-03-07 08:24:17 -0500 )edit

@simba1382 and @Guanta Did you implemented this by using bagofwords_classification.cpp from opencv version 2.4.x In case you did then plz help me in the changes that we are required to make in this .cpp files for proper working of code.. I am new to opencv and still trying sample code.

How and where to add the Feature detector,descriptor extractor, descriptor matcher ?? in that .cpp code

zits gravatar imagezits ( 2013-04-27 11:38:43 -0500 )edit

@zits: No I have used the official OpenCV classes documented here: and I can only recommend to use them.

The example in bagofwords_classification.cpp seems a little bit overloaded and configured for a specific dataset, however maybe you can adapt it to your needs. If you are experienced (or get more experienced), you can also try a more advanced module which was integrated recently: OpenFABMap, see here: (note there exist also an example: opencv-dir/samples/cpp/fabmap_sample.cpp)

Guanta gravatar imageGuanta ( 2013-04-27 18:00:26 -0500 )edit

Ok thanks a lot @Guanta

zits gravatar imagezits ( 2013-04-28 04:27:30 -0500 )edit

answered 2014-09-05 17:47:36 -0500

@Guanta Can you please help me how to go about the step 4 if my aim is just to find similar images and not to train? How to go about it once I have completed steps 1-3 and have the descriptors. Please help

edit flag offensive delete link more


If you don't want to train, then search for the nearest neighbors of each image descriptor, e.g. by BFMatcher.

Guanta gravatar imageGuanta ( 2014-09-22 04:41:50 -0500 )edit
Login/Signup to Answer

Question Tools



Asked: 2013-03-06 20:27:48 -0500

Seen: 18,442 times

Last updated: Sep 05 '14