0

I am recently working on a project where in I have extracted some features regarding an image, and want to find if there are any similarities between two images using those features. Here are the list of features that I have extracted:

  1. Aspect ratio (width/height)
  2. Normalised area (Cropped roi's area to input image area)
  3. Centre of the cropped image
    and other features from a single image

Now, I want to feed these values into a vector, and use that vector to find cosine similarity. In Short, use such vector from two images, and find the similarity between them. I know how cross product of two vectors works, but
I want help in storing these images into vector and usage of the vector. Any suggestions would be deeply appreciated.

Shruthi Kodi
  • 107
  • 1
  • 3
  • 10

1 Answers1

2

oh, not too difficult.

step 1: fill your feature(Mat) with numbers, one after the other:

Mat feature; // you could use a std::vector, too, but cv::Mat has the 
             // handy dot-product used below already built in.
feature.push_back(aspect_ratio);
feature.push_back(area);
feature.push_back(center.x);
feature.push_back(center.y);
feature.push_back(more_stuff);
...

step 2: to compare those features, use the cosine norm :

Mat feature_a, feature_b; // composed like above
double ab = feature_a.dot(feature_b);
double aa = feature_a.dot(feature_a);
double bb = feature_b.dot(feature_b);
return -ab / sqrt(aa*bb);
berak
  • 39,159
  • 9
  • 91
  • 89
  • 1
    Thanks berak.. I understand how you are passing values. Can we store any values in matrices? I mean can the values be of float and double datatypes? – Shruthi Kodi May 09 '15 at 04:01
  • the 1st one you push_back() will determine the type of the Mat. anything after that will get converted to the type of the 1st. – berak May 09 '15 at 07:01
  • Hi @berak, I am trying to use the code you helped me with, but I see theta values to be negative. What can I infer from that? – Shruthi Kodi May 13 '15 at 20:49
  • how would i know, what theta is ? (all of the context missing) – berak May 13 '15 at 20:50
  • Sorry, My mistake! I assumed the -ab/sqrt(aa*bb) to be theta value.I now understand that its cos(theta) value. Thanks! – Shruthi Kodi May 13 '15 at 20:54
  • I have a doubt regarding the cosine similarity. Can the cosine similarity of two vectors be negative? Also, is there any function across opencv or C++ that can calculate the theta out of cos theta value(if the value is negative)? – Shruthi Kodi May 13 '15 at 21:07
  • Thanks @berak!! Looks like I have forgotten my basic trigonometry here. Obtuse angles give negative values for cos theta. And cosine similarity says, More nearer to zero, more are they similar. So, i got my answer. – Shruthi Kodi May 13 '15 at 21:23
  • phew, i'm glad you put in better words i ever would have ;) – berak May 13 '15 at 21:25