Commit 426e45ca3bc57af0333cc1bfe4c1c4f25c14d357
1 parent
7b0e83a1
Added PermuteDistance
Showing
1 changed file
with
93 additions
and
0 deletions
openbr/plugins/distance/permute.cpp
0 → 100644
| 1 | +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
| 2 | + * Copyright 2012 The MITRE Corporation * | |
| 3 | + * * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); * | |
| 5 | + * you may not use this file except in compliance with the License. * | |
| 6 | + * You may obtain a copy of the License at * | |
| 7 | + * * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 * | |
| 9 | + * * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software * | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, * | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * | |
| 13 | + * See the License for the specific language governing permissions and * | |
| 14 | + * limitations under the License. * | |
| 15 | + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
| 16 | + | |
| 17 | +#include <numeric> | |
| 18 | +#include <algorithm> | |
| 19 | + | |
| 20 | +#include <openbr/plugins/openbr_internal.h> | |
| 21 | + | |
| 22 | +#include <QtConcurrent> | |
| 23 | + | |
| 24 | +using namespace std; | |
| 25 | + | |
| 26 | +namespace br | |
| 27 | +{ | |
| 28 | + | |
| 29 | +/*! | |
| 30 | + * \ingroup distances | |
| 31 | + * \brief Compares all permutations of matrices from one template to the other, and fuses the scores via the operation specified. | |
| 32 | + * \author Scott Klum \cite sklum | |
| 33 | + * \note Operation: Mean, sum, min, max are supported. | |
| 34 | + */ | |
| 35 | +class PermuteDistance : public Distance | |
| 36 | +{ | |
| 37 | + Q_OBJECT | |
| 38 | + Q_ENUMS(Operation) | |
| 39 | + Q_PROPERTY(br::Distance* distance READ get_distance WRITE set_distance RESET reset_distance STORED false) | |
| 40 | + Q_PROPERTY(Operation operation READ get_operation WRITE set_operation RESET reset_operation STORED false) | |
| 41 | +public: | |
| 42 | + /*!< */ | |
| 43 | + enum Operation {Mean, Sum, Max, Min}; | |
| 44 | + | |
| 45 | +private: | |
| 46 | + BR_PROPERTY(br::Distance*, distance, NULL) | |
| 47 | + BR_PROPERTY(Operation, operation, Mean) | |
| 48 | + | |
| 49 | + void train(const TemplateList &src) | |
| 50 | + { | |
| 51 | + distance->train(src); | |
| 52 | + } | |
| 53 | + | |
| 54 | + float compare(const Template &a, const Template &b) const | |
| 55 | + { | |
| 56 | + QList<int> indices; | |
| 57 | + for (int i=0; i<a.size(); i++) | |
| 58 | + indices << i; | |
| 59 | + | |
| 60 | + QList<float> scores; | |
| 61 | + do { | |
| 62 | + QList<float> permScores; | |
| 63 | + for (int i=0; i<a.size(); i++) | |
| 64 | + permScores.append(distance->compare(Template(a.file, a[indices[i]]),Template(b.file, b[i]))); | |
| 65 | + scores.append(std::accumulate(permScores.begin(),permScores.end(),0.0)); | |
| 66 | + } while ( next_permutation(indices.begin(),indices.end()) ); | |
| 67 | + | |
| 68 | + switch (operation) { | |
| 69 | + case Mean: | |
| 70 | + return std::accumulate(scores.begin(),scores.end(),0.0)/(float)scores.size(); | |
| 71 | + break; | |
| 72 | + case Sum: | |
| 73 | + return std::accumulate(scores.begin(),scores.end(),0.0); | |
| 74 | + break; | |
| 75 | + case Min: | |
| 76 | + return *std::min_element(scores.begin(),scores.end()); | |
| 77 | + break; | |
| 78 | + case Max: | |
| 79 | + return *std::max_element(scores.begin(),scores.end()); | |
| 80 | + break; | |
| 81 | + default: | |
| 82 | + qFatal("Invalid operation."); | |
| 83 | + } | |
| 84 | + return 0; | |
| 85 | + } | |
| 86 | +}; | |
| 87 | + | |
| 88 | +BR_REGISTER(Distance, PermuteDistance) | |
| 89 | + | |
| 90 | +} // namespace br | |
| 91 | + | |
| 92 | +#include "distance/permute.moc" | |
| 93 | + | ... | ... |