5 std::vector< std::pair<uint32_t, float> >
sparsify(
const cv::Mat &dense) {
6 std::vector< std::pair<uint32_t, float> > sparse;
7 for(
int i=0; i<dense.size().area(); i++) {
8 if( dense.at<
float>(i) != 0.f) sparse.push_back( std::pair<uint32_t, float>(i, dense.at<
float>(i)) );
13 float cos_sim(
const std::vector<std::pair<uint32_t, float> > &weights0,
14 const std::vector<std::pair<uint32_t, float> > &weights1,
15 const std::vector<float> &idfw) {
16 float ab = 0.f, a2 = 0.f, b2 = 0.f;
17 for(
size_t i=0, j=0; i < weights0.size() || j < weights1.size();) {
18 if(i < weights0.size() && j < weights1.size()) {
19 if (weights0[i].first == weights1[j].first) {
20 float a = weights0[i].second * idfw[weights0[i].first];
21 float b = weights1[j].second * idfw[weights1[j].first];
26 }
else if (weights0[i].first < weights1[j].first) {
27 float a = weights0[i].second * idfw[weights0[i].first];
31 float b = weights1[j].second * idfw[weights1[j].first];
35 }
else if(i < weights0.size()) {
36 float a = weights0[i].second * idfw[weights0[i].first];
40 float b = weights1[j].second * idfw[weights1[j].first];
45 return ab / (sqrtf(a2)*sqrtf(b2));
48 float min_hist(
const std::vector<std::pair<uint32_t, float> > &weights0,
49 const std::vector<std::pair<uint32_t, float> > &weights1,
50 const std::vector<float> &idfw) {
51 float a = 0.f, b = 0.f, ab = 0.f;
52 for(
int k=0; k<weights0.size(); k++) {
53 a += weights0[k].second*idfw[weights0[k].first];
55 for(
int k=0; k<weights1.size(); k++) {
56 b += weights1[k].second*idfw[weights1[k].first];
58 for(
size_t i=0, j=0; i < weights0.size() && j < weights1.size();) {
59 if(i < weights0.size() && j < weights1.size()) {
60 if (weights0[i].first == weights1[j].first) {
61 float min_val = weights0[i].second / a;
62 if(min_val > (weights1[j].second / b)) {
63 min_val = weights1[j].second / b;
65 ab += min_val * idfw[weights0[i].first];
67 }
else if (weights0[i].first < weights1[j].first) {