OpenCV - 如何使用C ++计算黎曼距离?

时间:2017-02-14 13:15:55

标签: matlab opencv image-processing distance

Matlab中两个协方差矩阵A和B的黎曼距离计算:

C = sqrt(sum(log(eig(A,B)).^2))

OpenCV与C ++的等价物是什么?

1 个答案:

答案 0 :(得分:2)

现在我得到的结果与Matlab相同。遵循我的C ++解决方案:

double calculateRiemannianDistance(cv::Mat A, cv::Mat B) {
Eigen::MatrixXf e_A, e_B;
cv::cv2eigen(A, e_A);
cv::cv2eigen(B, e_B);

/* compute generalized eigenvalues */
Eigen::GeneralizedEigenSolver<Eigen::MatrixXf> ges;
ges.compute(e_A, e_B);
cv::Mat gev;
cv::eigen2cv(Eigen::MatrixXf(ges.eigenvalues().real()), gev);

/* riemannian distance */
cv::Mat partial_res;
cv::log(gev, partial_res);
cv::pow(partial_res, 2, partial_res);
double distance = sqrt(cv::sum(partial_res)[0]);
return distance;
}