From b012abb152317fb252ee3ce8a04983701a603723 Mon Sep 17 00:00:00 2001 From: Jordan Cheney Date: Wed, 18 Feb 2015 12:12:14 -0500 Subject: [PATCH] Few changes to facilitate merge --- openbr/core/cluster.cpp | 22 ++++++++++++++++------ openbr/core/cluster.h | 2 ++ openbr/plugins/imgproc/adaptivethreshold.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ openbr/plugins/imgproc/canny.cpp | 35 +++++++++++++++++++++++++++++++++++ openbr/plugins/imgproc/rndsample.cpp | 8 ++++---- openbr/plugins/imgproc/samplefrommask.cpp | 38 ++++++++++++++++++++++++++++++++++++++ openbr/plugins/imgproc/scale.cpp | 13 ++++++++++++- 7 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 openbr/plugins/imgproc/adaptivethreshold.cpp create mode 100644 openbr/plugins/imgproc/canny.cpp create mode 100644 openbr/plugins/imgproc/samplefrommask.cpp diff --git a/openbr/core/cluster.cpp b/openbr/core/cluster.cpp index 44f196a..00bfa0d 100644 --- a/openbr/core/cluster.cpp +++ b/openbr/core/cluster.cpp @@ -82,7 +82,7 @@ float normalizedROD(const Neighborhood &neighborhood, int a, int b) return 1.f * (distanceA + distanceB) / std::min(indexA+1, indexB+1); } -Neighborhood getNeighborhood(const QStringList &simmats) +Neighborhood getNeighborhood(const QList &simmats) { Neighborhood neighborhood; @@ -99,9 +99,7 @@ Neighborhood getNeighborhood(const QStringList &simmats) int currentRows = -1; int columnOffset = 0; for (int j=0; j format(br::Factory::make(simmats[i*numGalleries+j])); - br::Template t = format->read(); - cv::Mat m = t.m(); + cv::Mat m = simmats[i * numGalleries + j]; if (j==0) { currentRows = m.rows; allNeighbors.resize(currentRows); @@ -152,12 +150,11 @@ Neighborhood getNeighborhood(const QStringList &simmats) neighbor.second = (neighbor.second - globalMin) / (globalMax - globalMin); } } - return neighborhood; } // Zhu et al. "A Rank-Order Distance based Clustering Algorithm for Face Tagging", CVPR 2011 -br::Clusters br::ClusterGallery(const QStringList &simmats, float aggressiveness, const QString &csv) +br::Clusters br::ClusterGallery(const QList &simmats, float aggressiveness) { qDebug("Clustering %d simmat(s), aggressiveness %f", simmats.size(), aggressiveness); @@ -238,6 +235,19 @@ br::Clusters br::ClusterGallery(const QStringList &simmats, float aggressiveness clusters = newClusters; neighborhood = newNeighborhood; } + return clusters; +} + +br::Clusters br::ClusterGallery(const QStringList &simmats, float aggressiveness, const QString &csv) +{ + QList mats; + foreach (const QString &simmat, simmats) { + QScopedPointer format(br::Factory::make(simmat)); + br::Template t = format->read(); + mats.append(t); + } + + Clusters clusters = ClusterGallery(mats, aggressiveness); // Save clusters if (!csv.isEmpty()) diff --git a/openbr/core/cluster.h b/openbr/core/cluster.h index 84bb9ba..c1e49ec 100644 --- a/openbr/core/cluster.h +++ b/openbr/core/cluster.h @@ -21,12 +21,14 @@ #include #include #include +#include namespace br { typedef QList Cluster; // List of indices into galleries typedef QVector Clusters; + Clusters ClusterGallery(const QList &simmats, float aggressiveness); Clusters ClusterGallery(const QStringList &simmats, float aggressiveness, const QString &csv); void EvalClustering(const QString &csv, const QString &input, QString truth_property); diff --git a/openbr/plugins/imgproc/adaptivethreshold.cpp b/openbr/plugins/imgproc/adaptivethreshold.cpp new file mode 100644 index 0000000..6927d95 --- /dev/null +++ b/openbr/plugins/imgproc/adaptivethreshold.cpp @@ -0,0 +1,55 @@ +#include + +#include + +using namespace cv; + +namespace br +{ + +/*! + * \ingroup transforms + * \brief Wraps OpenCV's adaptive thresholding. + * \author Scott Klum \cite sklum + */ +class AdaptiveThresholdTransform : public UntrainableTransform +{ + Q_OBJECT + + Q_ENUMS(Method) + Q_ENUMS(Type) + Q_PROPERTY(int maxValue READ get_maxValue WRITE set_maxValue RESET reset_maxValue STORED false) + Q_PROPERTY(Method method READ get_method WRITE set_method RESET reset_method STORED false) + Q_PROPERTY(Type type READ get_type WRITE set_type RESET reset_type STORED false) + Q_PROPERTY(int blockSize READ get_blockSize WRITE set_blockSize RESET reset_blockSize STORED false) + Q_PROPERTY(int C READ get_C WRITE set_C RESET reset_C STORED false) + + public: + enum Method { Mean = ADAPTIVE_THRESH_MEAN_C, + Gaussian = ADAPTIVE_THRESH_GAUSSIAN_C }; + + enum Type { Binary = THRESH_BINARY, + Binary_Inv = THRESH_BINARY_INV }; + + BR_PROPERTY(int, maxValue, 255) + BR_PROPERTY(Method, method, Mean) + BR_PROPERTY(Type, type, Binary) + BR_PROPERTY(int, blockSize, 3) + BR_PROPERTY(int, C, 0) + + void project(const Template &src, Template &dst) const + { + dst = src; + + Mat mask; + adaptiveThreshold(src, mask, maxValue, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, blockSize, C); + + dst.file.set("Mask",QVariant::fromValue(mask)); + } +}; + +BR_REGISTER(Transform, AdaptiveThresholdTransform) + +} // namespace br + +#include "imgproc/adaptivethreshold.moc" diff --git a/openbr/plugins/imgproc/canny.cpp b/openbr/plugins/imgproc/canny.cpp new file mode 100644 index 0000000..fad139a --- /dev/null +++ b/openbr/plugins/imgproc/canny.cpp @@ -0,0 +1,35 @@ +#include "opencv2/imgproc/imgproc.hpp" + +#include + +using namespace cv; + +namespace br +{ + +/*! + * \ingroup transforms + * \brief Warpper to OpenCV Canny edge detector + * \author Scott Klum \cite sklum + */ +class CannyTransform : public UntrainableTransform +{ + Q_OBJECT + Q_PROPERTY(double threshold READ get_threshold WRITE set_threshold RESET reset_threshold STORED false) + Q_PROPERTY(double aperatureSize READ get_aperatureSize WRITE set_aperatureSize RESET reset_aperatureSize STORED false) + Q_PROPERTY(bool L2Gradient READ get_L2Gradient WRITE set_L2Gradient RESET reset_L2Gradient STORED false) + BR_PROPERTY(double, threshold, 5) + BR_PROPERTY(double, aperatureSize, 3) + BR_PROPERTY(bool, L2Gradient, false) + + void project(const Template &src, Template &dst) const + { + Canny(src,dst, threshold, 3*threshold, aperatureSize, L2Gradient); + } +}; + +BR_REGISTER(Transform, CannyTransform) + +} // namespace br + +#include "imgproc/canny.moc" diff --git a/openbr/plugins/imgproc/rndsample.cpp b/openbr/plugins/imgproc/rndsample.cpp index 1c24362..260d297 100644 --- a/openbr/plugins/imgproc/rndsample.cpp +++ b/openbr/plugins/imgproc/rndsample.cpp @@ -17,7 +17,6 @@ #include #include -#include #include #include @@ -57,7 +56,7 @@ class RndSampleTransform : public UntrainableMetaTransform void project(const TemplateList &src, TemplateList &dst) const { foreach(const Template &t, src) { - QPointF point = t.file.points()[0]; + QPointF point = t.file.points()[pointIndex]; QRectF region(point.x()-sampleRadius, point.y()-sampleRadius, sampleRadius*2, sampleRadius*2); if (region.x() < 0 || @@ -77,6 +76,7 @@ class RndSampleTransform : public UntrainableMetaTransform labelCount << 0; while (std::accumulate(labelCount.begin(),labelCount.end(),0.0) < (sampleOverlapBands.size()-1)*samplesPerOverlapBand) { + float x = rand() % (sampleFactor*sampleRadius) + region.x() - sampleFactor/2*sampleRadius; float y = rand() % (sampleFactor*sampleRadius) + region.y() - sampleFactor/2*sampleRadius; @@ -85,13 +85,13 @@ class RndSampleTransform : public UntrainableMetaTransform QRectF negativeLocation = QRectF(x, y, sampleRadius*2, sampleRadius*2); - float overlap = QtUtils::overlap(region, negativeLocation); + float overlap = pow(QtUtils::overlap(region, negativeLocation),overlapPower); for (int k = 0; k= sampleOverlapBands.at(k) && overlap < sampleOverlapBands.at(k+1) && labelCount[k] < samplesPerOverlapBand) { Mat m(t.m(),OpenCVUtils::toRect(negativeLocation)); dst.append(Template(t.file, m)); - float label = classification ? 0 : pow(overlap,overlapPower); + float label = classification ? 0 : overlap; dst.last().file.set(inputVariable, label); labelCount[k]++; } diff --git a/openbr/plugins/imgproc/samplefrommask.cpp b/openbr/plugins/imgproc/samplefrommask.cpp new file mode 100644 index 0000000..cd00a89 --- /dev/null +++ b/openbr/plugins/imgproc/samplefrommask.cpp @@ -0,0 +1,38 @@ +#include + +using namespace cv; + +namespace br +{ + +/*! + * \ingroup transforms + * \brief Samples pixels from a mask. + * \author Scott Klum \cite sklum + */ +class SampleFromMaskTransform : public UntrainableTransform +{ + Q_OBJECT + + void project(const Template &src, Template &dst) const + { + Mat mask = src.file.get("Mask"); + const int count = countNonZero(mask); + dst.m() = Mat(1,count,src.m().type()); + + Mat masked; + src.m().copyTo(masked, mask); + + Mat indices; + findNonZero(masked,indices); + + for (int j=0; j(0,j) = masked.at(indices.at(j).y,indices.at(j).x); + } +}; + +BR_REGISTER(Transform, SampleFromMaskTransform) + +} // namespace br + +#include "imgproc/samplefrommask.moc" diff --git a/openbr/plugins/imgproc/scale.cpp b/openbr/plugins/imgproc/scale.cpp index da7bf1c..aa18d26 100644 --- a/openbr/plugins/imgproc/scale.cpp +++ b/openbr/plugins/imgproc/scale.cpp @@ -37,7 +37,18 @@ class ScaleTransform : public UntrainableTransform void project(const Template &src, Template &dst) const { - resize(src, dst, Size(src.m().cols*scaleFactor,src.m().rows*scaleFactor)); + resize(src, dst, Size(src.m().cols*scaleFactor,src.m().rows*scaleFactor)); + + QList rects = src.file.rects(); + for (int i=0; i points = src.file.points(); + for (int i=0; i