6 #include <Eigen/Eigenvalues>
43 std::vector<double>
mean(3, 0.0);
49 for (
int row = 0; row < height; row++)
51 for (
int col = 0; col < width; col++)
53 double red =
data.at(row * width * colors + col * colors);
54 double green =
data.at(row * width * colors + col * colors + 1);
55 double blue =
data.at(row * width * colors + col * colors + 2);
62 int numPixels = width * height;
73 std::vector<unsigned char>
data = array->getDataAsVector();
74 std::vector<unsigned char> new_data(
data.size());
76 for (
int i = 0; i <
data.size(); i++)
78 double a =
data.at(i);
84 array->getShape(), array->getType(), new_data, array->getPath());
88 std::vector<std::vector<double>>
90 std::vector<double>
mean = std::vector<double>())
97 std::vector<double> mean_vec(3, 0.0);
107 int numPixels = width * height;
109 std::vector<std::vector<double>> covariance(3, std::vector<double>(3, 0.0));
111 for (
int row = 0; row < height; row++)
113 for (
int col = 0; col < width; col++)
115 double red =
data.at(row * width * colors + col * colors);
116 double green =
data.at(row * width * colors + col * colors + 1);
117 double blue =
data.at(row * width * colors + col * colors + 2);
119 double redDeviation =
red -
mean[0];
121 double blueDeviation = blue -
mean[2];
123 covariance[0][0] += redDeviation * redDeviation;
124 covariance[0][1] += redDeviation * greenDeviation;
125 covariance[0][2] += redDeviation * blueDeviation;
126 covariance[1][1] += greenDeviation * greenDeviation;
127 covariance[1][2] += greenDeviation * blueDeviation;
128 covariance[2][2] += blueDeviation * blueDeviation;
132 for (
int i = 0; i < 3; i++)
134 for (
int j = 0; j < i; j++)
136 covariance[i][j] /= numPixels;
137 covariance[j][i] = covariance[i][j];
139 covariance[i][i] /= numPixels;
147 std::vector<double> mean_two,
148 std::vector<std::vector<double>> covariance_one,
149 std::vector<std::vector<double>> covariance_two)
152 Eigen::VectorXd meanOne(3);
153 meanOne << mean_one[0], mean_one[1], mean_one[2];
154 Eigen::VectorXd meanTwo(3);
155 meanTwo << mean_two[0], mean_two[1], mean_two[2];
165 Eigen::Matrix3d cov_one;
166 cov_one << covariance_one[0][0], covariance_one[0][1], covariance_one[0][2],
167 covariance_one[1][0], covariance_one[1][1], covariance_one[1][2], covariance_one[2][0],
168 covariance_one[2][1], covariance_one[2][2];
170 Eigen::Matrix3d cov_two;
171 cov_two << covariance_two[0][0], covariance_two[0][1], covariance_two[0][2],
172 covariance_two[1][0], covariance_two[1][1], covariance_two[1][2], covariance_two[2][0],
173 covariance_two[2][1], covariance_two[2][2];
178 Eigen::VectorXd meanDiff = meanOne - meanTwo;
181 Eigen::MatrixXd sigma = 0.5 * (cov_one + cov_two);
182 Eigen::MatrixXd sigma_inverse = sigma.inverse();
184 double det_cov_one = cov_one.determinant();
185 double det_cov_two = cov_two.determinant();
186 double det_sigma = sigma.determinant();
189 double bigger = meanDiff.transpose() * (sigma_inverse)*meanDiff;
191 double term_one = 0.125 * bigger;
192 double sqr =
std::sqrt(det_cov_one * det_cov_two);
194 double before_log = det_sigma / (sqr);
196 double term_two = 0.5 * std::log(before_log);
198 double distance = term_one + term_two;