Commit 167eb072b40cc6b7da92f5a7a735c8326cbc3b79

Authored by Scott Klum
1 parent bd8be323

Added support for multiple histograms

Showing 1 changed file with 19 additions and 11 deletions
openbr/plugins/distance/emd.cpp
... ... @@ -8,9 +8,8 @@ namespace br
8 8  
9 9 /*!
10 10 * \ingroup distances
11   - * \brief Fuses similarity scores across multiple matrices of compared templates
  11 + * \brief Computes Earth Mover Distance
12 12 * \author Scott Klum \cite sklum
13   - * \note Operation: Mean, sum, min, max are supported.
14 13 */
15 14 class EMDDistance : public UntrainableDistance
16 15 {
... ... @@ -18,17 +17,26 @@ class EMDDistance : public UntrainableDistance
18 17  
19 18 float compare(const Template &a, const Template &b) const
20 19 {
21   - Mat sig_a(a.m().cols, 2, CV_32FC1);
22   - Mat sig_b(b.m().cols, 2, CV_32FC1);
23   -
24   - for (int i=0; i<a.m().cols; i++) {
25   - sig_a.at<float>(i,0) = a.m().at<float>(0,i);
26   - sig_a.at<float>(i,1) = i;
  20 + const int dims_a = a.m().rows > 1 ? 3 : 2;
  21 + const int dims_b = b.m().rows > 1 ? 3 : 2;
  22 +
  23 + Mat sig_a(a.m().cols, dims_a, CV_32FC1);
  24 + Mat sig_b(b.m().cols, dims_b, CV_32FC1);
  25 +
  26 + for (int i=0; i<a.m().rows; i++) {
  27 + for (int j=0; j<a.m().cols; j++) {
  28 + sig_a.at<float>(i*a.m().cols+j,0) = a.m().at<float>(i,j);
  29 + sig_a.at<float>(i*a.m().cols+j,1) = j;
  30 + if (dims_a == 3) sig_a.at<float>(i*a.m().cols+j,2) = i;
  31 + }
27 32 }
28 33  
29   - for (int i=0; i<b.m().cols; i++) {
30   - sig_b.at<float>(i,0) = b.m().at<float>(0,i);
31   - sig_b.at<float>(i,1) = i;
  34 + for (int i=0; i<b.m().rows; i++) {
  35 + for (int j=0; j<b.m().cols; j++) {
  36 + sig_b.at<float>(i*b.m().cols+j,0) = b.m().at<float>(i,j);
  37 + sig_b.at<float>(i*b.m().cols+j,1) = j;
  38 + if (dims_b == 3) sig_a.at<float>(i*b.m().cols+j,2) = i;
  39 + }
32 40 }
33 41  
34 42 return EMD(sig_a,sig_b,CV_DIST_L2);
... ...