diff --git a/openbr/plugins/cluster.cpp b/openbr/plugins/cluster.cpp index 560709f..23a4bfd 100644 --- a/openbr/plugins/cluster.cpp +++ b/openbr/plugins/cluster.cpp @@ -14,9 +14,9 @@ * limitations under the License. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#include "openbr_internal.h" #include +#include "openbr_internal.h" #include "openbr/core/opencvutils.h" using namespace cv; @@ -26,39 +26,40 @@ namespace br /*! * \ingroup transforms - * \brief Wraps OpenCV kmeans + * \brief Wraps OpenCV kmeans and flann. * \author Josh Klontz \cite jklontz */ class KMeansTransform : public Transform { Q_OBJECT - Q_PROPERTY(int k READ get_k WRITE set_k RESET reset_k) - BR_PROPERTY(int, k, 1) + Q_PROPERTY(int kTrain READ get_kTrain WRITE set_kTrain RESET reset_kTrain STORED false) + Q_PROPERTY(int kSearch READ get_kSearch WRITE set_kSearch RESET reset_kSearch STORED false) + BR_PROPERTY(int, kTrain, 256) + BR_PROPERTY(int, kSearch, 1) Mat centers; - QSharedPointer index; - QSharedPointer indexLock; + mutable QScopedPointer index; + mutable QMutex mutex; void reindex() { - index = QSharedPointer(new flann::Index(centers, flann::LinearIndexParams())); - indexLock = QSharedPointer(new QMutex()); + index.reset(new flann::Index(centers, flann::LinearIndexParams())); } void train(const TemplateList &data) { Mat bestLabels; - const double compactness = kmeans(OpenCVUtils::toMatByRow(data.data()), k, bestLabels, TermCriteria(TermCriteria::MAX_ITER, 10, 0), 3, KMEANS_PP_CENTERS, centers); - reindex(); + const double compactness = kmeans(OpenCVUtils::toMatByRow(data.data()), kTrain, bestLabels, TermCriteria(TermCriteria::MAX_ITER, 10, 0), 3, KMEANS_PP_CENTERS, centers); qDebug("KMeans compactness = %f", compactness); + reindex(); } void project(const Template &src, Template &dst) const { - Mat dists; - indexLock->lock(); - index->knnSearch(src, dst, dists, 1); - indexLock->unlock(); + QMutexLocker locker(&mutex); + Mat dists, indicies; + index->knnSearch(src, indicies, dists, kSearch); + dst = indicies.reshape(1, 1); } void load(QDataStream &stream) diff --git a/openbr/plugins/draw.cpp b/openbr/plugins/draw.cpp index fcedc38..6a6a933 100644 --- a/openbr/plugins/draw.cpp +++ b/openbr/plugins/draw.cpp @@ -31,11 +31,9 @@ namespace br class DrawTransform : public UntrainableTransform { Q_OBJECT - Q_PROPERTY(bool named READ get_named WRITE set_named RESET reset_named STORED false) Q_PROPERTY(bool verbose READ get_verbose WRITE set_verbose RESET reset_verbose STORED false) Q_PROPERTY(bool points READ get_points WRITE set_points RESET reset_points STORED false) Q_PROPERTY(bool rects READ get_rects WRITE set_rects RESET reset_rects STORED false) - BR_PROPERTY(bool, named, true) BR_PROPERTY(bool, verbose, false) BR_PROPERTY(bool, points, true) BR_PROPERTY(bool, rects, true) @@ -47,7 +45,7 @@ class DrawTransform : public UntrainableTransform dst = src.m().clone(); if (points) { - const QList pointsList = OpenCVUtils::toPoints(named ? src.file.namedPoints() : src.file.points()); + const QList pointsList = OpenCVUtils::toPoints(src.file.namedPoints() + src.file.points()); for (int i=0; iclasses.keys()).join(',') + "}\n")); + + const int dimensions = t.m().rows * t.m().cols; + for (int i=0; i 1 ? words.takeLast() : "")); + QStringList words = line.split(regexp); + if (words.size() != headers.size()) continue; + File f; + for (int i=0; i()) { + if ((key == "Label") && !header) { + QString stringLabel = Globals->classes.key(value.value()); + if (stringLabel.isEmpty()) return value.value(); + else return stringLabel; + } else if (value.canConvert()) { if (header) return key; else return value.value(); } else if (value.canConvert()) { diff --git a/openbr/plugins/keypoint.cpp b/openbr/plugins/keypoint.cpp index b857aa7..9e936cb 100644 --- a/openbr/plugins/keypoint.cpp +++ b/openbr/plugins/keypoint.cpp @@ -58,7 +58,7 @@ class KeyPointDetectorTransform : public UntrainableTransform QList rects; foreach (const KeyPoint &keyPoint, keyPoints) - rects.append(Rect(keyPoint.pt.x, keyPoint.pt.y, keyPoint.size, keyPoint.size)); + rects.append(Rect(keyPoint.pt.x-keyPoint.size/2, keyPoint.pt.y-keyPoint.size/2, keyPoint.size, keyPoint.size)); dst.file.setRects(OpenCVUtils::fromRects(rects)); } }; @@ -90,13 +90,12 @@ class KeyPointDescriptorTransform : public UntrainableTransform void project(const Template &src, Template &dst) const { std::vector keyPoints; - if (size == -1) { + if (size == -1) foreach (const QRectF &ROI, src.file.rects()) - keyPoints.push_back(KeyPoint(ROI.x(), ROI.y(), (ROI.width() + ROI.height())/2)); - } else { + keyPoints.push_back(KeyPoint(ROI.x()+ROI.width()/2, ROI.y()+ROI.height()/2, (ROI.width() + ROI.height())/2)); + else foreach (const QPointF &landmark, src.file.points()) keyPoints.push_back(KeyPoint(landmark.x(), landmark.y(), size)); - } descriptorExtractor->compute(src, keyPoints, dst); } }; diff --git a/openbr/plugins/svm.cpp b/openbr/plugins/svm.cpp index 0fc0b0c..470201f 100644 --- a/openbr/plugins/svm.cpp +++ b/openbr/plugins/svm.cpp @@ -75,14 +75,15 @@ private: cv::Mat data = OpenCVUtils::toMat(_data.data()); cv::Mat lab = OpenCVUtils::toMat(_data.labels()); - // Scale labels to [-1,1] - double min, max; - cv::minMaxLoc(lab, &min, &max); - if (max > min) { - a = 2.0/(max-min); - b = -(min*a+1); - lab = (lab * a) + b; + if ((type == EPS_SVR) || (type == NU_SVR)) { + // Scale labels to [-1,1] + double min, max; cv::minMaxLoc(lab, &min, &max); + if (max > min) { + a = 2.0/(max-min); + b = -(min*a+1); + lab = (lab * a) + b; + } } if (data.type() != CV_32FC1)