Commit b012abb152317fb252ee3ce8a04983701a603723
1 parent
e4d4166c
Few changes to facilitate merge
Showing
7 changed files
with
162 additions
and
11 deletions
openbr/core/cluster.cpp
| ... | ... | @@ -82,7 +82,7 @@ float normalizedROD(const Neighborhood &neighborhood, int a, int b) |
| 82 | 82 | return 1.f * (distanceA + distanceB) / std::min(indexA+1, indexB+1); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | -Neighborhood getNeighborhood(const QStringList &simmats) | |
| 85 | +Neighborhood getNeighborhood(const QList<cv::Mat> &simmats) | |
| 86 | 86 | { |
| 87 | 87 | Neighborhood neighborhood; |
| 88 | 88 | |
| ... | ... | @@ -99,9 +99,7 @@ Neighborhood getNeighborhood(const QStringList &simmats) |
| 99 | 99 | int currentRows = -1; |
| 100 | 100 | int columnOffset = 0; |
| 101 | 101 | for (int j=0; j<numGalleries; j++) { |
| 102 | - QScopedPointer<br::Format> format(br::Factory<br::Format>::make(simmats[i*numGalleries+j])); | |
| 103 | - br::Template t = format->read(); | |
| 104 | - cv::Mat m = t.m(); | |
| 102 | + cv::Mat m = simmats[i * numGalleries + j]; | |
| 105 | 103 | if (j==0) { |
| 106 | 104 | currentRows = m.rows; |
| 107 | 105 | allNeighbors.resize(currentRows); |
| ... | ... | @@ -152,12 +150,11 @@ Neighborhood getNeighborhood(const QStringList &simmats) |
| 152 | 150 | neighbor.second = (neighbor.second - globalMin) / (globalMax - globalMin); |
| 153 | 151 | } |
| 154 | 152 | } |
| 155 | - | |
| 156 | 153 | return neighborhood; |
| 157 | 154 | } |
| 158 | 155 | |
| 159 | 156 | // Zhu et al. "A Rank-Order Distance based Clustering Algorithm for Face Tagging", CVPR 2011 |
| 160 | -br::Clusters br::ClusterGallery(const QStringList &simmats, float aggressiveness, const QString &csv) | |
| 157 | +br::Clusters br::ClusterGallery(const QList<cv::Mat> &simmats, float aggressiveness) | |
| 161 | 158 | { |
| 162 | 159 | qDebug("Clustering %d simmat(s), aggressiveness %f", simmats.size(), aggressiveness); |
| 163 | 160 | |
| ... | ... | @@ -238,6 +235,19 @@ br::Clusters br::ClusterGallery(const QStringList &simmats, float aggressiveness |
| 238 | 235 | clusters = newClusters; |
| 239 | 236 | neighborhood = newNeighborhood; |
| 240 | 237 | } |
| 238 | + return clusters; | |
| 239 | +} | |
| 240 | + | |
| 241 | +br::Clusters br::ClusterGallery(const QStringList &simmats, float aggressiveness, const QString &csv) | |
| 242 | +{ | |
| 243 | + QList<cv::Mat> mats; | |
| 244 | + foreach (const QString &simmat, simmats) { | |
| 245 | + QScopedPointer<br::Format> format(br::Factory<br::Format>::make(simmat)); | |
| 246 | + br::Template t = format->read(); | |
| 247 | + mats.append(t); | |
| 248 | + } | |
| 249 | + | |
| 250 | + Clusters clusters = ClusterGallery(mats, aggressiveness); | |
| 241 | 251 | |
| 242 | 252 | // Save clusters |
| 243 | 253 | if (!csv.isEmpty()) | ... | ... |
openbr/core/cluster.h
| ... | ... | @@ -21,12 +21,14 @@ |
| 21 | 21 | #include <QString> |
| 22 | 22 | #include <QStringList> |
| 23 | 23 | #include <QVector> |
| 24 | +#include <openbr/openbr_plugin.h> | |
| 24 | 25 | |
| 25 | 26 | namespace br |
| 26 | 27 | { |
| 27 | 28 | typedef QList<int> Cluster; // List of indices into galleries |
| 28 | 29 | typedef QVector<Cluster> Clusters; |
| 29 | 30 | |
| 31 | + Clusters ClusterGallery(const QList<cv::Mat> &simmats, float aggressiveness); | |
| 30 | 32 | Clusters ClusterGallery(const QStringList &simmats, float aggressiveness, const QString &csv); |
| 31 | 33 | void EvalClustering(const QString &csv, const QString &input, QString truth_property); |
| 32 | 34 | ... | ... |
openbr/plugins/imgproc/adaptivethreshold.cpp
0 → 100644
| 1 | +#include <opencv2/imgproc/imgproc.hpp> | |
| 2 | + | |
| 3 | +#include <openbr/plugins/openbr_internal.h> | |
| 4 | + | |
| 5 | +using namespace cv; | |
| 6 | + | |
| 7 | +namespace br | |
| 8 | +{ | |
| 9 | + | |
| 10 | +/*! | |
| 11 | + * \ingroup transforms | |
| 12 | + * \brief Wraps OpenCV's adaptive thresholding. | |
| 13 | + * \author Scott Klum \cite sklum | |
| 14 | + */ | |
| 15 | +class AdaptiveThresholdTransform : public UntrainableTransform | |
| 16 | +{ | |
| 17 | + Q_OBJECT | |
| 18 | + | |
| 19 | + Q_ENUMS(Method) | |
| 20 | + Q_ENUMS(Type) | |
| 21 | + Q_PROPERTY(int maxValue READ get_maxValue WRITE set_maxValue RESET reset_maxValue STORED false) | |
| 22 | + Q_PROPERTY(Method method READ get_method WRITE set_method RESET reset_method STORED false) | |
| 23 | + Q_PROPERTY(Type type READ get_type WRITE set_type RESET reset_type STORED false) | |
| 24 | + Q_PROPERTY(int blockSize READ get_blockSize WRITE set_blockSize RESET reset_blockSize STORED false) | |
| 25 | + Q_PROPERTY(int C READ get_C WRITE set_C RESET reset_C STORED false) | |
| 26 | + | |
| 27 | + public: | |
| 28 | + enum Method { Mean = ADAPTIVE_THRESH_MEAN_C, | |
| 29 | + Gaussian = ADAPTIVE_THRESH_GAUSSIAN_C }; | |
| 30 | + | |
| 31 | + enum Type { Binary = THRESH_BINARY, | |
| 32 | + Binary_Inv = THRESH_BINARY_INV }; | |
| 33 | + | |
| 34 | + BR_PROPERTY(int, maxValue, 255) | |
| 35 | + BR_PROPERTY(Method, method, Mean) | |
| 36 | + BR_PROPERTY(Type, type, Binary) | |
| 37 | + BR_PROPERTY(int, blockSize, 3) | |
| 38 | + BR_PROPERTY(int, C, 0) | |
| 39 | + | |
| 40 | + void project(const Template &src, Template &dst) const | |
| 41 | + { | |
| 42 | + dst = src; | |
| 43 | + | |
| 44 | + Mat mask; | |
| 45 | + adaptiveThreshold(src, mask, maxValue, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, blockSize, C); | |
| 46 | + | |
| 47 | + dst.file.set("Mask",QVariant::fromValue(mask)); | |
| 48 | + } | |
| 49 | +}; | |
| 50 | + | |
| 51 | +BR_REGISTER(Transform, AdaptiveThresholdTransform) | |
| 52 | + | |
| 53 | +} // namespace br | |
| 54 | + | |
| 55 | +#include "imgproc/adaptivethreshold.moc" | ... | ... |
openbr/plugins/imgproc/canny.cpp
0 → 100644
| 1 | +#include "opencv2/imgproc/imgproc.hpp" | |
| 2 | + | |
| 3 | +#include <openbr/plugins/openbr_internal.h> | |
| 4 | + | |
| 5 | +using namespace cv; | |
| 6 | + | |
| 7 | +namespace br | |
| 8 | +{ | |
| 9 | + | |
| 10 | +/*! | |
| 11 | + * \ingroup transforms | |
| 12 | + * \brief Warpper to OpenCV Canny edge detector | |
| 13 | + * \author Scott Klum \cite sklum | |
| 14 | + */ | |
| 15 | +class CannyTransform : public UntrainableTransform | |
| 16 | +{ | |
| 17 | + Q_OBJECT | |
| 18 | + Q_PROPERTY(double threshold READ get_threshold WRITE set_threshold RESET reset_threshold STORED false) | |
| 19 | + Q_PROPERTY(double aperatureSize READ get_aperatureSize WRITE set_aperatureSize RESET reset_aperatureSize STORED false) | |
| 20 | + Q_PROPERTY(bool L2Gradient READ get_L2Gradient WRITE set_L2Gradient RESET reset_L2Gradient STORED false) | |
| 21 | + BR_PROPERTY(double, threshold, 5) | |
| 22 | + BR_PROPERTY(double, aperatureSize, 3) | |
| 23 | + BR_PROPERTY(bool, L2Gradient, false) | |
| 24 | + | |
| 25 | + void project(const Template &src, Template &dst) const | |
| 26 | + { | |
| 27 | + Canny(src,dst, threshold, 3*threshold, aperatureSize, L2Gradient); | |
| 28 | + } | |
| 29 | +}; | |
| 30 | + | |
| 31 | +BR_REGISTER(Transform, CannyTransform) | |
| 32 | + | |
| 33 | +} // namespace br | |
| 34 | + | |
| 35 | +#include "imgproc/canny.moc" | ... | ... |
openbr/plugins/imgproc/rndsample.cpp
| ... | ... | @@ -17,7 +17,6 @@ |
| 17 | 17 | #include <numeric> |
| 18 | 18 | |
| 19 | 19 | #include <openbr/plugins/openbr_internal.h> |
| 20 | -#include <openbr/core/common.h> | |
| 21 | 20 | #include <openbr/core/opencvutils.h> |
| 22 | 21 | #include <openbr/core/qtutils.h> |
| 23 | 22 | |
| ... | ... | @@ -57,7 +56,7 @@ class RndSampleTransform : public UntrainableMetaTransform |
| 57 | 56 | void project(const TemplateList &src, TemplateList &dst) const |
| 58 | 57 | { |
| 59 | 58 | foreach(const Template &t, src) { |
| 60 | - QPointF point = t.file.points()[0]; | |
| 59 | + QPointF point = t.file.points()[pointIndex]; | |
| 61 | 60 | QRectF region(point.x()-sampleRadius, point.y()-sampleRadius, sampleRadius*2, sampleRadius*2); |
| 62 | 61 | |
| 63 | 62 | if (region.x() < 0 || |
| ... | ... | @@ -77,6 +76,7 @@ class RndSampleTransform : public UntrainableMetaTransform |
| 77 | 76 | labelCount << 0; |
| 78 | 77 | |
| 79 | 78 | while (std::accumulate(labelCount.begin(),labelCount.end(),0.0) < (sampleOverlapBands.size()-1)*samplesPerOverlapBand) { |
| 79 | + | |
| 80 | 80 | float x = rand() % (sampleFactor*sampleRadius) + region.x() - sampleFactor/2*sampleRadius; |
| 81 | 81 | float y = rand() % (sampleFactor*sampleRadius) + region.y() - sampleFactor/2*sampleRadius; |
| 82 | 82 | |
| ... | ... | @@ -85,13 +85,13 @@ class RndSampleTransform : public UntrainableMetaTransform |
| 85 | 85 | |
| 86 | 86 | QRectF negativeLocation = QRectF(x, y, sampleRadius*2, sampleRadius*2); |
| 87 | 87 | |
| 88 | - float overlap = QtUtils::overlap(region, negativeLocation); | |
| 88 | + float overlap = pow(QtUtils::overlap(region, negativeLocation),overlapPower); | |
| 89 | 89 | |
| 90 | 90 | for (int k = 0; k<sampleOverlapBands.size()-1; k++) { |
| 91 | 91 | if (overlap >= sampleOverlapBands.at(k) && overlap < sampleOverlapBands.at(k+1) && labelCount[k] < samplesPerOverlapBand) { |
| 92 | 92 | Mat m(t.m(),OpenCVUtils::toRect(negativeLocation)); |
| 93 | 93 | dst.append(Template(t.file, m)); |
| 94 | - float label = classification ? 0 : pow(overlap,overlapPower); | |
| 94 | + float label = classification ? 0 : overlap; | |
| 95 | 95 | dst.last().file.set(inputVariable, label); |
| 96 | 96 | labelCount[k]++; |
| 97 | 97 | } | ... | ... |
openbr/plugins/imgproc/samplefrommask.cpp
0 → 100644
| 1 | +#include <openbr/plugins/openbr_internal.h> | |
| 2 | + | |
| 3 | +using namespace cv; | |
| 4 | + | |
| 5 | +namespace br | |
| 6 | +{ | |
| 7 | + | |
| 8 | +/*! | |
| 9 | + * \ingroup transforms | |
| 10 | + * \brief Samples pixels from a mask. | |
| 11 | + * \author Scott Klum \cite sklum | |
| 12 | + */ | |
| 13 | +class SampleFromMaskTransform : public UntrainableTransform | |
| 14 | +{ | |
| 15 | + Q_OBJECT | |
| 16 | + | |
| 17 | + void project(const Template &src, Template &dst) const | |
| 18 | + { | |
| 19 | + Mat mask = src.file.get<Mat>("Mask"); | |
| 20 | + const int count = countNonZero(mask); | |
| 21 | + dst.m() = Mat(1,count,src.m().type()); | |
| 22 | + | |
| 23 | + Mat masked; | |
| 24 | + src.m().copyTo(masked, mask); | |
| 25 | + | |
| 26 | + Mat indices; | |
| 27 | + findNonZero(masked,indices); | |
| 28 | + | |
| 29 | + for (int j=0; j<indices.total(); j++) | |
| 30 | + dst.m().at<uchar>(0,j) = masked.at<uchar>(indices.at<Point>(j).y,indices.at<Point>(j).x); | |
| 31 | + } | |
| 32 | +}; | |
| 33 | + | |
| 34 | +BR_REGISTER(Transform, SampleFromMaskTransform) | |
| 35 | + | |
| 36 | +} // namespace br | |
| 37 | + | |
| 38 | +#include "imgproc/samplefrommask.moc" | ... | ... |
openbr/plugins/imgproc/scale.cpp
| ... | ... | @@ -37,7 +37,18 @@ class ScaleTransform : public UntrainableTransform |
| 37 | 37 | |
| 38 | 38 | void project(const Template &src, Template &dst) const |
| 39 | 39 | { |
| 40 | - resize(src, dst, Size(src.m().cols*scaleFactor,src.m().rows*scaleFactor)); | |
| 40 | + resize(src, dst, Size(src.m().cols*scaleFactor,src.m().rows*scaleFactor)); | |
| 41 | + | |
| 42 | + QList<QRectF> rects = src.file.rects(); | |
| 43 | + for (int i=0; i<rects.size(); i++) | |
| 44 | + rects[i] = QRectF(rects[i].topLeft()*scaleFactor,rects[i].bottomRight()*scaleFactor); | |
| 45 | + dst.file.setRects(rects); | |
| 46 | + | |
| 47 | + QList<QPointF> points = src.file.points(); | |
| 48 | + for (int i=0; i<points.size(); i++) | |
| 49 | + points[i] = points[i] * scaleFactor; | |
| 50 | + dst.file.setPoints(points); | |
| 51 | + | |
| 41 | 52 | } |
| 42 | 53 | }; |
| 43 | 54 | ... | ... |