# CosDistance (cosine similarity)

**URL:** <https://forum.opencv.org/t/cosdistance-cosine-similarity/926>\
**Category:** Uncategorized\
**Created:** [January 19, 2021, 4:23am UTC](https://forum.opencv.org/t/cosdistance-cosine-similarity/926 "2021-01-19T04:23:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TSG](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tsg/32/533_2.png) [@TSG](https://forum.opencv.org/u/TSG)\
**Post date:** [January 19, 2021, 4:23am UTC](https://forum.opencv.org/t/cosdistance-cosine-similarity/926/1 "2021-01-19T04:23:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 19, 2021, 9:31am UTC](https://forum.opencv.org/t/cosdistance-cosine-similarity/926/2 "2021-01-19T09:31:56Z")

</div>

Source code is [here](https://github.com/opencv/opencv_contrib/blob/master/modules/tracking/src/tracking_by_matching.cpp#L41-L52)  
but don’t you forget a parenthesis?

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

```

---

<div class="post-metadata">

**Author:** ![TSG](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tsg/32/533_2.png) [@TSG](https://forum.opencv.org/u/TSG)\
**Post date:** [January 19, 2021, 10:54am UTC](https://forum.opencv.org/t/cosdistance-cosine-similarity/926/3 "2021-01-19T10:54:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 19, 2021, 11:07am UTC](https://forum.opencv.org/t/cosdistance-cosine-similarity/926/4 "2021-01-19T11:07:47Z")

</div>

I don’t think there is a problem: in source code there is [0.5](https://github.com/opencv/opencv_contrib/blob/master/modules/tracking/src/tracking_by_matching.cpp#L51)

```
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

---

<div class="post-metadata">

**Author:** ![TSG](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tsg/32/533_2.png) [@TSG](https://forum.opencv.org/u/TSG)\
**Post date:** [January 19, 2021, 11:25am UTC](https://forum.opencv.org/t/cosdistance-cosine-similarity/926/5 "2021-01-19T11:25:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 19, 2021, 11:38am UTC](https://forum.opencv.org/t/cosdistance-cosine-similarity/926/6 "2021-01-19T11:38:27Z")

</div>

you can post an [issue](https://github.com/opencv/opencv_contrib/issues) or ask to the [author](https://github.com/LeonidBeynenson)

---

<div class="post-metadata">

**Author:** ![TSG](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tsg/32/533_2.png) [@TSG](https://forum.opencv.org/u/TSG)\
**Post date:** [January 19, 2021, 11:52am UTC](https://forum.opencv.org/t/cosdistance-cosine-similarity/926/7 "2021-01-19T11:52:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 19, 2021, 11:55am UTC](https://forum.opencv.org/t/cosdistance-cosine-similarity/926/8 "2021-01-19T11:55:10Z")

</div>

There is no problem. You can post an issue on opencv\_contrib and make a pr to clarify doc : that’s opensource 🙂
