diff --git a/openbr/plugins/algorithms.cpp b/openbr/plugins/algorithms.cpp index 4463eeb..113e59d 100644 --- a/openbr/plugins/algorithms.cpp +++ b/openbr/plugins/algorithms.cpp @@ -50,7 +50,7 @@ class AlgorithmsInitializer : public Initializer Globals->abbreviations.insert("SmallSIFT", "Open+LimitSize(512)+KeyPointDetector(SIFT)+KeyPointDescriptor(SIFT):KeyPointMatcher(BruteForce)"); Globals->abbreviations.insert("SmallSURF", "Open+LimitSize(512)+KeyPointDetector(SURF)+KeyPointDescriptor(SURF):KeyPointMatcher(BruteForce)"); Globals->abbreviations.insert("ColorHist", "Open+LimitSize(512)!EnsureChannels(3)+SplitChannels+Hist(256,0,8)+Cat+Normalize(L1):L2"); - Globals->abbreviations.insert("ImageClassification", "Open+CropSquare+LimitSize(256)+Cvt(Gray)+Gradient+Bin(0,360,9,true)+Merge+Integral+RecursiveIntegralSampler(4,2,8,Singleton(KMeans(1024)[fraction=0.5]))+Cat+CvtFloat+Hist(1024):Dist(L1)"); + Globals->abbreviations.insert("ImageClassification", "Open+CropSquare+LimitSize(256)+Cvt(Gray)+Gradient+Bin(0,360,9,true)+Merge+Integral+RecursiveIntegralSampler(4,2,8,Singleton(KMeans(1024)[fraction=0.5]))+Cat+CvtFloat+Hist(1024)+KNS(5,Dist(L1))"); // Hash Globals->abbreviations.insert("FileName", "Name+Identity:Identical"); diff --git a/openbr/plugins/cluster.cpp b/openbr/plugins/cluster.cpp index 23a4bfd..a21c34a 100644 --- a/openbr/plugins/cluster.cpp +++ b/openbr/plugins/cluster.cpp @@ -17,6 +17,7 @@ #include #include "openbr_internal.h" +#include "openbr/core/common.h" #include "openbr/core/opencvutils.h" using namespace cv; @@ -76,6 +77,51 @@ class KMeansTransform : public Transform BR_REGISTER(Transform, KMeansTransform) -} +/*! + * \ingroup transforms + * \brief K nearest subjects. + * \author Josh Klontz \cite jklontz + */ +class KNSTransform : public Transform +{ + Q_OBJECT + Q_PROPERTY(int k READ get_k WRITE set_k RESET reset_k STORED false) + Q_PROPERTY(br::Distance *distance READ get_distance WRITE set_distance RESET reset_distance STORED false) + BR_PROPERTY(int, k, 1) + BR_PROPERTY(br::Distance*, distance, NULL) + + TemplateList gallery; + + void train(const TemplateList &data) + { + gallery = data; + } + + void project(const Template &src, Template &dst) const + { + const QList< QPair > sortedScores = Common::Sort(distance->compare(gallery, src), true); + QSet subjects; + int i = 0; + while ((subjects.size() < k) && (i < sortedScores.size())) { + subjects.insert(gallery[sortedScores[i].second].file.subject()); + i++; + } + dst.file.set("KNS", QStringList(subjects.toList()).join(", ")); + } + + void store(QDataStream &stream) const + { + stream << gallery; + } + + void load(QDataStream &stream) + { + stream >> gallery; + } +}; + +BR_REGISTER(Transform, KNSTransform) + +} // namespace br #include "cluster.moc"