Commit 52cf180afbcfacf18dff412a59db6d29d44c81ab
1 parent
4d4fe90d
Slight re-organization of some code in core/cluster.cpp
put "neighbor" class definitions in openbr_internal.h, export a couple additional functions in openbr_plugin.h. Add a function to get the comparison transformation used by an algorithm
Showing
5 changed files
with
60 additions
and
10 deletions
openbr/core/cluster.cpp
| @@ -25,13 +25,12 @@ | @@ -25,13 +25,12 @@ | ||
| 25 | 25 | ||
| 26 | #include "openbr/core/bee.h" | 26 | #include "openbr/core/bee.h" |
| 27 | #include "openbr/core/cluster.h" | 27 | #include "openbr/core/cluster.h" |
| 28 | +#include "openbr/plugins/openbr_internal.h" | ||
| 28 | 29 | ||
| 29 | -typedef QPair<int,float> Neighbor; // QPair<id,similarity> | ||
| 30 | -typedef QList<Neighbor> Neighbors; | ||
| 31 | -typedef QVector<Neighbors> Neighborhood; | 30 | +using namespace br; |
| 32 | 31 | ||
| 33 | // Compare function used to order neighbors from highest to lowest similarity | 32 | // Compare function used to order neighbors from highest to lowest similarity |
| 34 | -static bool compareNeighbors(const Neighbor &a, const Neighbor &b) | 33 | +bool br::compareNeighbors(const Neighbor &a, const Neighbor &b) |
| 35 | { | 34 | { |
| 36 | if (a.second == b.second) | 35 | if (a.second == b.second) |
| 37 | return a.first < b.first; | 36 | return a.first < b.first; |
openbr/core/core.cpp
| @@ -715,6 +715,11 @@ void br::Deduplicate(const File &inputGallery, const File &outputGallery, const | @@ -715,6 +715,11 @@ void br::Deduplicate(const File &inputGallery, const File &outputGallery, const | ||
| 715 | else qFatal("Unable to convert deduplication threshold to float."); | 715 | else qFatal("Unable to convert deduplication threshold to float."); |
| 716 | } | 716 | } |
| 717 | 717 | ||
| 718 | +QSharedPointer<br::Transform> br::Transform::fromComparison(const QString &algorithm) | ||
| 719 | +{ | ||
| 720 | + return AlgorithmManager::getAlgorithm(algorithm)->comparison; | ||
| 721 | +} | ||
| 722 | + | ||
| 718 | QSharedPointer<br::Transform> br::Transform::fromAlgorithm(const QString &algorithm, bool preprocess) | 723 | QSharedPointer<br::Transform> br::Transform::fromAlgorithm(const QString &algorithm, bool preprocess) |
| 719 | { | 724 | { |
| 720 | if (!preprocess) | 725 | if (!preprocess) |
openbr/openbr_plugin.h
| @@ -461,9 +461,9 @@ struct TemplateList : public QList<Template> | @@ -461,9 +461,9 @@ struct TemplateList : public QList<Template> | ||
| 461 | /*!< \brief Ensure labels are in the range [0,numClasses-1]. */ | 461 | /*!< \brief Ensure labels are in the range [0,numClasses-1]. */ |
| 462 | BR_EXPORT static TemplateList relabel(const TemplateList &tl, const QString &propName, bool preserveIntegers); | 462 | BR_EXPORT static TemplateList relabel(const TemplateList &tl, const QString &propName, bool preserveIntegers); |
| 463 | 463 | ||
| 464 | - QList<int> indexProperty(const QString &propName, QHash<QString, int> * valueMap=NULL,QHash<int, QVariant> * reverseLookup = NULL) const; | ||
| 465 | - QList<int> indexProperty(const QString &propName, QHash<QString, int> &valueMap, QHash<int, QVariant> &reverseLookup) const; | ||
| 466 | - QList<int> applyIndex(const QString &propName, const QHash<QString, int> &valueMap) const; | 464 | + BR_EXPORT QList<int> indexProperty(const QString &propName, QHash<QString, int> * valueMap=NULL,QHash<int, QVariant> * reverseLookup = NULL) const; |
| 465 | + BR_EXPORT QList<int> indexProperty(const QString &propName, QHash<QString, int> &valueMap, QHash<int, QVariant> &reverseLookup) const; | ||
| 466 | + BR_EXPORT QList<int> applyIndex(const QString &propName, const QHash<QString, int> &valueMap) const; | ||
| 467 | 467 | ||
| 468 | /*! | 468 | /*! |
| 469 | * \brief Returns the total number of bytes in all the templates. | 469 | * \brief Returns the total number of bytes in all the templates. |
| @@ -1163,6 +1163,7 @@ public: | @@ -1163,6 +1163,7 @@ public: | ||
| 1163 | virtual ~Transform() {} | 1163 | virtual ~Transform() {} |
| 1164 | static Transform *make(QString str, QObject *parent); /*!< \brief Make a transform from a string. */ | 1164 | static Transform *make(QString str, QObject *parent); /*!< \brief Make a transform from a string. */ |
| 1165 | static QSharedPointer<Transform> fromAlgorithm(const QString &algorithm, bool preprocess=true); /*!< \brief Retrieve an algorithm's transform. If preprocess is true, attaches a stream transform as the root of the algorithm*/ | 1165 | static QSharedPointer<Transform> fromAlgorithm(const QString &algorithm, bool preprocess=true); /*!< \brief Retrieve an algorithm's transform. If preprocess is true, attaches a stream transform as the root of the algorithm*/ |
| 1166 | + static QSharedPointer<Transform> fromComparison(const QString &algorithm); | ||
| 1166 | 1167 | ||
| 1167 | virtual Transform *clone() const; /*!< \brief Copy the transform. */ | 1168 | virtual Transform *clone() const; /*!< \brief Copy the transform. */ |
| 1168 | 1169 | ||
| @@ -1446,6 +1447,10 @@ BR_EXPORT void Cat(const QStringList &inputGalleries, const QString &outputGalle | @@ -1446,6 +1447,10 @@ BR_EXPORT void Cat(const QStringList &inputGalleries, const QString &outputGalle | ||
| 1446 | */ | 1447 | */ |
| 1447 | BR_EXPORT void Deduplicate(const File &inputGallery, const File &outputGallery, const QString &threshold); | 1448 | BR_EXPORT void Deduplicate(const File &inputGallery, const File &outputGallery, const QString &threshold); |
| 1448 | 1449 | ||
| 1450 | +BR_EXPORT Transform *wrapTransform(Transform *base, const QString &target); | ||
| 1451 | + | ||
| 1452 | +BR_EXPORT Transform *pipeTransforms(QList<Transform *> &transforms); | ||
| 1453 | + | ||
| 1449 | /*! @}*/ | 1454 | /*! @}*/ |
| 1450 | 1455 | ||
| 1451 | } // namespace br | 1456 | } // namespace br |
openbr/plugins/cluster.cpp
| @@ -204,6 +204,44 @@ class RandomCentroidsTransform : public Transform | @@ -204,6 +204,44 @@ class RandomCentroidsTransform : public Transform | ||
| 204 | 204 | ||
| 205 | BR_REGISTER(Transform, RandomCentroidsTransform) | 205 | BR_REGISTER(Transform, RandomCentroidsTransform) |
| 206 | 206 | ||
| 207 | +class RegInitializer : public Initializer | ||
| 208 | +{ | ||
| 209 | + Q_OBJECT | ||
| 210 | + | ||
| 211 | + void initialize() const | ||
| 212 | + { | ||
| 213 | + qRegisterMetaType<br::Neighbors>(); | ||
| 214 | + } | ||
| 215 | +}; | ||
| 216 | +BR_REGISTER(Initializer, RegInitializer) | ||
| 217 | + | ||
| 218 | +class CollectNNTransform : public UntrainableMetaTransform | ||
| 219 | +{ | ||
| 220 | + Q_OBJECT | ||
| 221 | + | ||
| 222 | + Q_PROPERTY(int keep READ get_keep WRITE set_keep RESET reset_keep STORED false) | ||
| 223 | + BR_PROPERTY(int, keep, 20) | ||
| 224 | + | ||
| 225 | + void project(const Template &src, Template &dst) const | ||
| 226 | + { | ||
| 227 | + dst.file = src.file; | ||
| 228 | + dst.m() = cv::Mat(); | ||
| 229 | + Neighbors neighbors; | ||
| 230 | + for (int i=0; i < src.m().cols;i++) { | ||
| 231 | + // skip self compares | ||
| 232 | + if (i == src.file.get<int>("FrameNumber")) | ||
| 233 | + continue; | ||
| 234 | + neighbors.append(Neighbor(i, src.m().at<float>(0,i))); | ||
| 235 | + } | ||
| 236 | + int actuallyKeep = std::min(keep, neighbors.size()); | ||
| 237 | + std::partial_sort(neighbors.begin(), neighbors.begin()+actuallyKeep, neighbors.end(), compareNeighbors); | ||
| 238 | + | ||
| 239 | + Neighbors selected = neighbors.mid(0, actuallyKeep); | ||
| 240 | + dst.file.set("neighbors", QVariant::fromValue(selected)); | ||
| 241 | + } | ||
| 242 | +}; | ||
| 243 | +BR_REGISTER(Transform, CollectNNTransform) | ||
| 244 | + | ||
| 207 | } // namespace br | 245 | } // namespace br |
| 208 | 246 | ||
| 209 | #include "cluster.moc" | 247 | #include "cluster.moc" |
openbr/plugins/openbr_internal.h
| @@ -477,9 +477,6 @@ public: | @@ -477,9 +477,6 @@ public: | ||
| 477 | 477 | ||
| 478 | void applyAdditionalProperties(const File &temp, Transform *target); | 478 | void applyAdditionalProperties(const File &temp, Transform *target); |
| 479 | 479 | ||
| 480 | -Transform *wrapTransform(Transform *base, const QString &target); | ||
| 481 | - | ||
| 482 | -Transform *pipeTransforms(QList<Transform *> &transforms); | ||
| 483 | 480 | ||
| 484 | inline void splitFTEs(TemplateList &src, TemplateList &ftes) | 481 | inline void splitFTEs(TemplateList &src, TemplateList &ftes) |
| 485 | { | 482 | { |
| @@ -494,6 +491,12 @@ inline void splitFTEs(TemplateList &src, TemplateList &ftes) | @@ -494,6 +491,12 @@ inline void splitFTEs(TemplateList &src, TemplateList &ftes) | ||
| 494 | } | 491 | } |
| 495 | } | 492 | } |
| 496 | 493 | ||
| 494 | +typedef QPair<int,float> Neighbor; // QPair<id,similarity> | ||
| 495 | +typedef QList<Neighbor> Neighbors; | ||
| 496 | +typedef QVector<Neighbors> Neighborhood; | ||
| 497 | + | ||
| 498 | +BR_EXPORT bool compareNeighbors(const Neighbor &a, const Neighbor &b); | ||
| 499 | + | ||
| 497 | } | 500 | } |
| 498 | 501 | ||
| 499 | #endif // OPENBR_INTERNAL_H | 502 | #endif // OPENBR_INTERNAL_H |