5 #include <SimoxUtility/algorithm/string.h>
15 int width = p1->getShape().at(0);
16 int height = p1->getShape().at(1);
17 int dimensions = p1->getShape().at(2);
21 ARMARX_INFO <<
"Trying to calculate cosine similarity for more than 3 channels, only "
22 "first three channels will be looked at";
24 Eigen::MatrixXf m1r(width, height);
25 Eigen::MatrixXf m2r(width, height);
26 Eigen::MatrixXf m1g(width, height);
27 Eigen::MatrixXf m2g(width, height);
28 Eigen::MatrixXf m1b(width, height);
29 Eigen::MatrixXf m2b(width, height);
31 auto image1 = p1->getDataAsVector();
32 auto image2 = p2->getDataAsVector();
35 for (
int x = 0; x < width; x++)
37 for (
int y = 0; y < height; y++)
40 int index = x + width * (y + 0 * height);
41 double element1 = image1.at(
index);
42 double element2 = image2.at(
index);
46 index = x + width * (y + 1 * height);
47 element1 = image1.at(
index);
48 element2 = image2.at(
index);
52 index = x + width * (y + 2 * height);
53 element1 = image1.at(
index);
54 element2 = image2.at(
index);
61 double dotProductR = (m1r.array() * m2r.array()).sum();
62 double normProductR = m1r.norm() * m2r.norm();
63 double cosineR = 1.0 - (dotProductR / normProductR);
66 double dotProductG = (m1g.array() * m2g.array()).sum();
67 double normProductG = m1g.norm() * m2g.norm();
68 double cosineG = 1.0 - (dotProductG / normProductG);
71 double dotProductB = (m1b.array() * m2b.array()).sum();
72 double normProductB = m1b.norm() * m2b.norm();
73 double cosineB = 1.0 - (dotProductB / normProductB);
76 return (cosineR + cosineG + cosineB) / 3;