CosDistance (cosine similarity)

Hi,

Is the opencv cosDistance.compute cosine similarity as per the formula , below is the c++ code snippet which seem to yield two different result for cosine similarity ?

   Mat d1, d2;
d1,d2 are mat with exact row and column
float cosdist = (d1.dot(d2)) / sqrt(d1.dot(d1)) * sqrt(d2.dot(d2));
cout << "Estimated Cosine Similarity " << 1 - cosdist << endl;
cv::tbm::CosDistance cosd = cv::tbm::CosDistance::CosDistance(cv::Size(d1.cols, d1.rows));
float dcos = (cosd.compute(d1, d2));
cout << "Estimated Cv Cosine Similarity " << dcos << endl;

Source code is here
but don’t you forget a parenthesis?

float cosdist = (d1.dot(d2)) / (sqrt(d1.dot(d1)) * sqrt(d2.dot(d2)));

I afraid the computed cosine is still almost 1.9999 times higher the lower cosdist computed by opencv ie computed =1.9999xopencv 's CosDistance , any reason why the opencv computation is much lower than the cosine formulation ?
source code
double xy = descr1.dot(descr2);
double xx = descr1.dot(descr1);
double yy = descr2.dot(descr2);
double norm = sqrt(xx * yy) + 1e-6;
return 0.5f * static_cast(1.0 - xy / norm);

I don’t think there is a problem: in source code there is 0.5

Mat d1 = (Mat_<float>(1, 6) << 0, 1.1, 2, 10, 11, 12);
Mat d2 = (Mat_<float>(1, 6) << 0, 1.1, 0.1, 10, 11, 12);
float cosdist = (d1.dot(d2)) / (sqrt(d1.dot(d1)) * sqrt(d2.dot(d2)));
cout << "Estimated Cosine Similarity " << 0.5 * (1 - cosdist) << endl;
cv::detail::tracking::tbm::CosDistance cosd = cv::detail::tracking::tbm::CosDistance(d1.size());
float dcos = (cosd.compute(d1, d2));
cout << "Estimated Cv Cosine Similarity " << dcos << endl;

gives
Estimated Cosine Similarity 0.0024437
Estimated Cv Cosine Similarity 0.00244371

I just wonder what is the reason behind application of 0.5 factor reduction , the document never mentioned this is a modified version of the original cosine similarity which measures the cosine of the angle between two vectors projected in a multi-dimensional space.

A.B=||A|| ||B||cos theta

you can post an issue or ask to the author

1 Like

thanks for the reply , the reason why I raise this issue is because different value of angular distance is computed using arccosine(theta)/PI

There is no problem. You can post an issue on opencv_contrib and make a pr to clarify doc : that’s opensource :slight_smile: