-
Notifications
You must be signed in to change notification settings - Fork 4
/
cvxEntropy.cpp
66 lines (55 loc) · 1.71 KB
/
cvxEntropy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// cvxEntropy.cpp
// RGB_RF
//
// Created by jimmy on 2016-05-26.
// Copyright © 2016 jimmy. All rights reserved.
//
#include "cvxEntropy.hpp"
using cv::Mat;
double CvxEntropy::computeGaussianEntropy(const cv::Mat & cov)
{
assert(cov.rows == cov.cols);
double det = cv::determinant(cov);
det = fabs(det) + 0.00000000000001;
assert(det > 0.0);
double temp = pow(2.0 * M_PI * M_E, cov.rows);
double entropy = 0.5 * log( temp * det);
return entropy;
}
// paper: On Entropy Approximation for Gaussian Mixture Random Vectors 2008
// appendex A
double CvxEntropy::conputeGMMEntropyFirstOrderAppro(const cv::ml::EM & em_model)
{
double entropy = 0.0;
Mat estimated_means = em_model.getMeans();
Mat estimated_weights = em_model.getWeights();
assert(estimated_means.type() == CV_64F);
assert(estimated_weights.type() == CV_64F);
for(int i = 0; i<estimated_weights.cols; i++)
{
double log_likelihood = em_model.predict2(estimated_means.row(i), cv::noArray())[0];
entropy += -estimated_weights.at<double>(0, i) * log_likelihood;
}
return entropy;
}
int CvxEntropy::argminEntropy(const vector<cv::Mat> & covs)
{
assert(covs.size() > 1);
Mat entropy(1, (int)covs.size(), CV_64FC1, 0.0);
for (int i = 0; i<covs.size(); i++) {
entropy.at<double>(0, i) = CvxEntropy::computeGaussianEntropy(covs[i]);
}
double minv = 0.0, maxv = 0.0;
int min_idx = 0, max_idx = 0;
cv::minMaxIdx(entropy, &minv, &maxv, &min_idx, &max_idx);
return min_idx;
}
void CvxEntropy::duplicateMatrix(cv::Mat & inoutmat)
{
assert(inoutmat.rows != 0);
cv::Mat m;
inoutmat.copyTo(m);
m.push_back(inoutmat);
inoutmat = m;
}