Commit 7669f3257a41ff37c415b7dd6ca53b67a262b639
1 parent
27f6b386
Abstracted AverageDistance to support the dreaded score-level fusion
Showing
1 changed file
with
33 additions
and
8 deletions
openbr/plugins/distance.cpp
| @@ -16,6 +16,7 @@ | @@ -16,6 +16,7 @@ | ||
| 16 | 16 | ||
| 17 | #include <QFutureSynchronizer> | 17 | #include <QFutureSynchronizer> |
| 18 | #include <QtConcurrentRun> | 18 | #include <QtConcurrentRun> |
| 19 | +#include <numeric> | ||
| 19 | #include <opencv2/imgproc/imgproc.hpp> | 20 | #include <opencv2/imgproc/imgproc.hpp> |
| 20 | #include "openbr_internal.h" | 21 | #include "openbr_internal.h" |
| 21 | 22 | ||
| @@ -178,14 +179,24 @@ BR_REGISTER(Distance, PipeDistance) | @@ -178,14 +179,24 @@ BR_REGISTER(Distance, PipeDistance) | ||
| 178 | 179 | ||
| 179 | /*! | 180 | /*! |
| 180 | * \ingroup distances | 181 | * \ingroup distances |
| 181 | - * \brief Average distance of multiple matrices | 182 | + * \brief Computes an operation on distances across multiple matrices of compared templates |
| 182 | * \author Scott Klum \cite sklum | 183 | * \author Scott Klum \cite sklum |
| 184 | + * \note Operation: Mean, sum, min, max are supported. | ||
| 183 | */ | 185 | */ |
| 184 | -class AverageDistance : public Distance | 186 | +class OperationDistance : public Distance |
| 185 | { | 187 | { |
| 186 | Q_OBJECT | 188 | Q_OBJECT |
| 189 | + Q_ENUMS(Operation) | ||
| 187 | Q_PROPERTY(br::Distance* distance READ get_distance WRITE set_distance RESET reset_distance STORED false) | 190 | Q_PROPERTY(br::Distance* distance READ get_distance WRITE set_distance RESET reset_distance STORED false) |
| 191 | + Q_PROPERTY(Operation operation READ get_operation WRITE set_operation RESET reset_operation STORED false) | ||
| 192 | + | ||
| 193 | +public: | ||
| 194 | + /*!< */ | ||
| 195 | + enum Operation {Mean, Sum, Max, Min}; | ||
| 196 | + | ||
| 197 | +private: | ||
| 188 | BR_PROPERTY(br::Distance*, distance, make("Dist(L2)")) | 198 | BR_PROPERTY(br::Distance*, distance, make("Dist(L2)")) |
| 199 | + BR_PROPERTY(Operation, operation, Mean) | ||
| 189 | 200 | ||
| 190 | void train(const TemplateList &src) | 201 | void train(const TemplateList &src) |
| 191 | { | 202 | { |
| @@ -196,16 +207,30 @@ class AverageDistance : public Distance | @@ -196,16 +207,30 @@ class AverageDistance : public Distance | ||
| 196 | { | 207 | { |
| 197 | if (a.size() != b.size()) qFatal("Comparison size mismatch"); | 208 | if (a.size() != b.size()) qFatal("Comparison size mismatch"); |
| 198 | 209 | ||
| 199 | - float score = 0; | ||
| 200 | - for (int i = 0; i < a.size(); i++) { | ||
| 201 | - score += distance->compare(a[i],b[i]); | ||
| 202 | - } | 210 | + QList<float> distances; |
| 211 | + for (int i = 0; i < a.size(); i++) | ||
| 212 | + distances.append(distance->compare(a[i],b[i])); | ||
| 203 | 213 | ||
| 204 | - return score/(float)a.size(); | 214 | + switch (operation) { |
| 215 | + case Mean: | ||
| 216 | + return std::accumulate(distances.begin(),distances.end(),0)/(float)distances.size(); | ||
| 217 | + break; | ||
| 218 | + case Sum: | ||
| 219 | + return std::accumulate(distances.begin(),distances.end(),0); | ||
| 220 | + break; | ||
| 221 | + case Min: | ||
| 222 | + return *std::min_element(distances.begin(),distances.end()); | ||
| 223 | + break; | ||
| 224 | + case Max: | ||
| 225 | + return *std::max_element(distances.begin(),distances.end()); | ||
| 226 | + break; | ||
| 227 | + default: | ||
| 228 | + qFatal("Invalid operation."); | ||
| 229 | + } | ||
| 205 | } | 230 | } |
| 206 | }; | 231 | }; |
| 207 | 232 | ||
| 208 | -BR_REGISTER(Distance, AverageDistance) | 233 | +BR_REGISTER(Distance, OperationDistance) |
| 209 | 234 | ||
| 210 | /*! | 235 | /*! |
| 211 | * \ingroup distances | 236 | * \ingroup distances |