3 #include <SimoxUtility/algorithm/string.h>
12 int width = p1->getShape().at(0);
13 int height = p1->getShape().at(1);
14 int dimensions = p1->getShape().at(2);
17 ARMARX_INFO <<
"Trying to calculate cosine similarity for more than 3 channels, only first three channels will be looked at";
19 Eigen::MatrixXf m1r(width, height);
20 Eigen::MatrixXf m2r(width, height);
21 Eigen::MatrixXf m1g(width, height);
22 Eigen::MatrixXf m2g(width, height);
23 Eigen::MatrixXf m1b(width, height);
24 Eigen::MatrixXf m2b(width, height);
26 auto image1 = p1->getDataAsVector();
27 auto image2 = p2->getDataAsVector();
30 for(
int x = 0; x < width; x++){
31 for(
int y = 0; y < height; y++){
33 int index = x + width * (y + 0 * height);
34 double element1 = image1.at(
index);
35 double element2 = image2.at(
index);
39 index = x + width * (y + 1 * height);
40 element1 = image1.at(
index);
41 element2 = image2.at(
index);
45 index = x + width * (y + 2 * height);
46 element1 = image1.at(
index);
47 element2 = image2.at(
index);
54 double dotProductR = (m1r.array() * m2r.array()).sum();
55 double normProductR = m1r.norm() * m2r.norm();
56 double cosineR = 1.0 - (dotProductR / normProductR);
59 double dotProductG = (m1g.array() * m2g.array()).sum();
60 double normProductG = m1g.norm() * m2g.norm();
61 double cosineG = 1.0 - (dotProductG / normProductG);
64 double dotProductB = (m1b.array() * m2b.array()).sum();
65 double normProductB = m1b.norm() * m2b.norm();
66 double cosineB = 1.0 - (dotProductB / normProductB);
69 return (cosineR + cosineG + cosineB)/ 3;