Commit d86637aec2a1289af9f609d7cd5968b646321dff
1 parent
0d0f5bd4
implemented BayesianQuantizationDistance
Showing
1 changed file
with
61 additions
and
0 deletions
openbr/plugins/quantize.cpp
| @@ -56,6 +56,67 @@ class QuantizeTransform : public Transform | @@ -56,6 +56,67 @@ class QuantizeTransform : public Transform | ||
| 56 | BR_REGISTER(Transform, QuantizeTransform) | 56 | BR_REGISTER(Transform, QuantizeTransform) |
| 57 | 57 | ||
| 58 | /*! | 58 | /*! |
| 59 | + * \ingroup distances | ||
| 60 | + * \brief Bayesian quantization distance | ||
| 61 | + * \author Josh Klontz \cite jklontz | ||
| 62 | + */ | ||
| 63 | +class BayesianQuantizationDistance : public Distance | ||
| 64 | +{ | ||
| 65 | + Q_OBJECT | ||
| 66 | + QVector<float> loglikelihood; | ||
| 67 | + | ||
| 68 | + void train(const TemplateList &src) | ||
| 69 | + { | ||
| 70 | + if (src.first().size() > 1) | ||
| 71 | + qFatal("Expected sigle matrix templates."); | ||
| 72 | + | ||
| 73 | + Mat data = OpenCVUtils::toMat(src.data()); | ||
| 74 | + QList<int> labels = src.labels<int>(); | ||
| 75 | + | ||
| 76 | + QVector<qint64> genuines(256*256,0), impostors(256*256,0); | ||
| 77 | + qint64 totalGenuines(0), totalImpostors(0); | ||
| 78 | + for (int i=0; i<labels.size(); i++) { | ||
| 79 | + const uchar *a = data.ptr(i); | ||
| 80 | + for (int j=0; j<labels.size(); j++) { | ||
| 81 | + const uchar *b = data.ptr(j); | ||
| 82 | + const bool genuine = (labels[i] == labels[j]); | ||
| 83 | + for (int k=0; k<data.cols; k++) { | ||
| 84 | + if (genuine) { genuines[256*a[k]+b[k]]++; genuines[256*b[k]+a[k]]++; totalGenuines++; } | ||
| 85 | + else { impostors[256*a[k]+b[k]]++; impostors[256*b[k]+a[k]]++; totalImpostors++; } | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + loglikelihood = QVector<float>(256*256); | ||
| 91 | + for (int i=0; i<256*256; i++) | ||
| 92 | + loglikelihood[i] = log((double(genuines[i]+1)/totalGenuines)/(double(impostors[i]+1)/totalImpostors)); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + float compare(const Template &a, const Template &b) const | ||
| 96 | + { | ||
| 97 | + const uchar *aData = a.m().data; | ||
| 98 | + const uchar *bData = b.m().data; | ||
| 99 | + const int size = a.m().rows * a.m().cols; | ||
| 100 | + float likelihood = 0; | ||
| 101 | + for (int i=0; i<size; i++) | ||
| 102 | + likelihood += loglikelihood[256*aData[i]+bData[i]]; | ||
| 103 | + return likelihood; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + void load(QDataStream &stream) | ||
| 107 | + { | ||
| 108 | + stream >> loglikelihood; | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + void store(QDataStream &stream) const | ||
| 112 | + { | ||
| 113 | + stream << loglikelihood; | ||
| 114 | + } | ||
| 115 | +}; | ||
| 116 | + | ||
| 117 | +BR_REGISTER(Distance, BayesianQuantizationDistance) | ||
| 118 | + | ||
| 119 | +/*! | ||
| 59 | * \ingroup transforms | 120 | * \ingroup transforms |
| 60 | * \brief Approximate floats as signed bit. | 121 | * \brief Approximate floats as signed bit. |
| 61 | * \author Josh Klontz \cite jklontz | 122 | * \author Josh Klontz \cite jklontz |