5 #include <Eigen/Eigenvalues>
37 std::vector<double>
mean(3, 0.0);
43 for (
int row = 0; row < height; row++){
44 for (
int col = 0; col < width; col++){
45 double red =
data.at(row * width * colors + col * colors);
46 double green =
data.at(row * width * colors + col * colors + 1);
47 double blue =
data.at(row * width * colors + col * colors + 2);
54 int numPixels = width * height;
64 std::vector<unsigned char>
data = array->getDataAsVector();
65 std::vector<unsigned char> new_data(
data.size());
67 for(
int i = 0; i <
data.size(); i++){
68 double a =
data.at(i);
84 std::vector<double> mean_vec(3, 0.0);
91 int numPixels = width * height;
93 std::vector<std::vector<double>> covariance(3, std::vector<double>(3, 0.0));
95 for(
int row = 0; row < height; row++){
96 for(
int col = 0; col < width; col++){
97 double red =
data.at(row * width * colors + col * colors);
98 double green =
data.at(row * width * colors + col * colors + 1);
99 double blue =
data.at(row * width * colors + col * colors + 2);
101 double redDeviation =
red -
mean[0];
103 double blueDeviation = blue -
mean[2];
105 covariance[0][0] += redDeviation * redDeviation;
106 covariance[0][1] += redDeviation * greenDeviation;
107 covariance[0][2] += redDeviation * blueDeviation;
108 covariance[1][1] += greenDeviation * greenDeviation;
109 covariance[1][2] += greenDeviation * blueDeviation;
110 covariance[2][2] += blueDeviation * blueDeviation;
114 for(
int i = 0; i < 3; i++){
115 for (
int j = 0; j < i; j++){
116 covariance[i][j] /= numPixels;
117 covariance[j][i] = covariance[i][j];
119 covariance[i][i] /= numPixels;
125 double calculate_bhattacharyya_distance(std::vector<double> mean_one, std::vector<double> mean_two, std::vector<std::vector<double> > covariance_one, std::vector<std::vector<double> > covariance_two)
128 Eigen::VectorXd meanOne(3);
129 meanOne << mean_one[0], mean_one[1], mean_one[2];
130 Eigen::VectorXd meanTwo(3);
131 meanTwo << mean_two[0], mean_two[1], mean_two[2];
141 Eigen::Matrix3d cov_one;
142 cov_one << covariance_one[0][0], covariance_one[0][1], covariance_one[0][2],
143 covariance_one[1][0], covariance_one[1][1], covariance_one[1][2],
144 covariance_one[2][0], covariance_one[2][1], covariance_one[2][2];
146 Eigen::Matrix3d cov_two;
147 cov_two << covariance_two[0][0], covariance_two[0][1], covariance_two[0][2],
148 covariance_two[1][0], covariance_two[1][1], covariance_two[1][2],
149 covariance_two[2][0], covariance_two[2][1], covariance_two[2][2];
154 Eigen::VectorXd meanDiff = meanOne - meanTwo;
157 Eigen::MatrixXd sigma = 0.5 * (cov_one + cov_two);
158 Eigen::MatrixXd sigma_inverse = sigma.inverse();
160 double det_cov_one = cov_one.determinant();
161 double det_cov_two = cov_two.determinant();
162 double det_sigma = sigma.determinant();
165 double bigger = meanDiff.transpose() * (sigma_inverse) * meanDiff;
167 double term_one = 0.125 * bigger;
168 double sqr =
std::sqrt(det_cov_one * det_cov_two);
170 double before_log = det_sigma / (sqr);
172 double term_two = 0.5 * std::log(before_log);
174 double distance = term_one + term_two;