diff --git a/openbr/plugins/representation/haar.cpp b/openbr/plugins/representation/haar.cpp deleted file mode 100644 index d711731..0000000 --- a/openbr/plugins/representation/haar.cpp +++ /dev/null @@ -1,188 +0,0 @@ -#include - -#include -#include - -using namespace cv; - -namespace br -{ - -#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step ) \ - /* (x, y) */ \ - (p0) = (rect).x + (step) * (rect).y; \ - /* (x + w, y) */ \ - (p1) = (rect).x + (rect).width + (step) * (rect).y; \ - /* (x + w, y) */ \ - (p2) = (rect).x + (step) * ((rect).y + (rect).height); \ - /* (x + w, y + h) */ \ - (p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height); - -/*! - * \brief An implementation of Haar Features for Viola-Jones cascade object detection - * \author Jordan Cheney \cite jcheney - * \br_property int winWidth The width of the input image. The total feature space is based on this and the winHeight - * \br_property int winHeight The height of the input image. The total feature space is based on this and the winWidth. - * \br_paper Paul Viola, Michael Jones - * Rapid Object Detection using a Boosted Cascade of Simple Features - * CVPR, 2001 - * \br_link Rapid Object Detection using a Boosted Cascade of Simple Features https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf - */ -class HaarRepresentation : public Representation -{ - Q_OBJECT - - Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false) - Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false) - BR_PROPERTY(int, winWidth, 24) - BR_PROPERTY(int, winHeight, 24) - - void init() - { - if (features.isEmpty()) { - // Pre-determine the size of features to avoid reallocations - int numFeatures = 0; - for (int x = 0; x < winWidth; x++) - for (int y = 0; y < winHeight; y++) - for (int dx = 1; dx <= winWidth; dx++) - for (int dy = 1; dy <= winHeight; dy++) - numFeatures += ((x+dx*2 <= winWidth) && (y+dy <= winHeight)) - + ((x+dx <= winWidth) && (y+dy*2 <= winHeight)) - + ((x+dx*3 <= winWidth) && (y+dy <= winHeight)) - + ((x+dx <= winWidth) && (y+dy*3 <= winHeight)) - + ((x+dx*2 <= winWidth) && (y+dy*2 <= winHeight)); - features.reserve(numFeatures); - - const int offset = winWidth + 1; - int index = 0; - for (int x = 0; x < winWidth; x++) { - for (int y = 0; y < winHeight; y++) { - for (int dx = 1; dx <= winWidth; dx++) { - for (int dy = 1; dy <= winHeight; dy++) { - // haar_x2 - if ((x+dx*2 <= winWidth) && (y+dy <= winHeight)) - features[index++] = Feature(offset, - x, y, dx*2, dy, -1, - x+dx, y, dx , dy, +2); - // haar_y2 - if ((x+dx <= winWidth) && (y+dy*2 <= winHeight)) - features[index++] = Feature(offset, - x, y, dx, dy*2, -1, - x, y+dy, dx, dy, +2); - // haar_x3 - if ((x+dx*3 <= winWidth) && (y+dy <= winHeight)) - features[index++] = Feature(offset, - x, y, dx*3, dy, -1, - x+dx, y, dx , dy, +3); - // haar_y3 - if ((x+dx <= winWidth) && (y+dy*3 <= winHeight)) - features[index++] = Feature(offset, - x, y, dx, dy*3, -1, - x, y+dy, dx, dy, +3); - // x2_y2 - if ((x+dx*2 <= winWidth) && (y+dy*2 <= winHeight)) - features[index++] = Feature(offset, - x, y, dx*2, dy*2, -1, - x, y, dx, dy, +2, - x+dx, y+dy, dx, dy, +2); - - - } - } - } - } - } - } - - Template preprocess(const Template &src) const - { - Template dst; - integral(src, dst); - return dst; - } - - float evaluate(const Template &src, int idx) const - { - return features[idx].calc(src.m()); - } - - Mat evaluate(const Template &src, const QList &indices) const - { - int size = indices.empty() ? numFeatures() : indices.size(); - - Mat result(1, size, CV_32FC1); - for (int i = 0; i < size; i++) - result.at(i) = evaluate(src, indices.empty() ? i : indices[i]); - return result; - } - - int numFeatures() const - { - return features.size(); - } - - Size windowSize(int *dx, int *dy) const - { - if (dx && dy) - *dx = *dy = 1; - return Size(winWidth, winHeight); - } - - int maxCatCount() const { return 0; } - - struct Feature - { - Feature(); - Feature( int offset, - int x0, int y0, int w0, int h0, float wt0, - int x1, int y1, int w1, int h1, float wt1, - int x2 = 0, int y2 = 0, int w2 = 0, int h2 = 0, float wt2 = 0.0F ); - float calc(const Mat &img) const; - - struct { - float weight; - int p0, p1, p2, p3; - } fastRect[3]; - }; - - QVector features; -}; - -BR_REGISTER(Representation, HaarRepresentation) - -HaarRepresentation::Feature::Feature() -{ - fastRect[0].p0 = fastRect[1].p0 = fastRect[2].p0 = 0; - fastRect[0].p1 = fastRect[1].p1 = fastRect[2].p1 = 0; - fastRect[0].p2 = fastRect[1].p2 = fastRect[2].p2 = 0; - fastRect[0].p3 = fastRect[1].p3 = fastRect[2].p3 = 0; - fastRect[0].weight = fastRect[1].weight = fastRect[2].weight = 0; -} - -HaarRepresentation::Feature::Feature(int offset, - int x0, int y0, int w0, int h0, float wt0, - int x1, int y1, int w1, int h1, float wt1, - int x2, int y2, int w2, int h2, float wt2) -{ - CV_SUM_OFFSETS(fastRect[0].p0, fastRect[0].p1, fastRect[0].p2, fastRect[0].p3, Rect(x0, y0, w0, h0), offset) - CV_SUM_OFFSETS(fastRect[1].p0, fastRect[1].p1, fastRect[1].p2, fastRect[1].p3, Rect(x1, y1, w1, h1), offset) - CV_SUM_OFFSETS(fastRect[2].p0, fastRect[2].p1, fastRect[2].p2, fastRect[2].p3, Rect(x2, y2, w2, h2), offset) - fastRect[0].weight = wt0; - fastRect[1].weight = wt1; - fastRect[2].weight = wt2; -} - -inline float HaarRepresentation::Feature::calc(const Mat &img) const -{ - const int* ptr = img.ptr(); - float ret = fastRect[0].weight * (ptr[fastRect[0].p0] - ptr[fastRect[0].p1] - ptr[fastRect[0].p2] + ptr[fastRect[0].p3]) + - fastRect[1].weight * (ptr[fastRect[1].p0] - ptr[fastRect[1].p1] - ptr[fastRect[1].p2] + ptr[fastRect[1].p3]); - if (fastRect[2].weight != 0.0f) - ret += fastRect[2].weight * (ptr[fastRect[2].p0] - ptr[fastRect[2].p1] - ptr[fastRect[2].p2] + ptr[fastRect[2].p3]); - return ret; -} - -} // namespace br - -#include "representation/haar.moc" - diff --git a/openbr/plugins/representation/mblbp.cpp b/openbr/plugins/representation/mblbp.cpp deleted file mode 100644 index cb32324..0000000 --- a/openbr/plugins/representation/mblbp.cpp +++ /dev/null @@ -1,140 +0,0 @@ -#include - -#include -#include - -using namespace cv; - -namespace br -{ - -#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step ) \ - /* (x, y) */ \ - (p0) = (rect).x + (step) * (rect).y; \ - /* (x + w, y) */ \ - (p1) = (rect).x + (rect).width + (step) * (rect).y; \ - /* (x + w, y) */ \ - (p2) = (rect).x + (step) * ((rect).y + (rect).height); \ - /* (x + w, y + h) */ \ - (p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height); - -/*! - * \brief An implementation of MBLBP as an OpenBR Representation - * \author Jordan Cheney \cite jcheney - * \br_property int winWidth The width of the input image. The total feature space is based on this and the winHeight - * \br_property int winHeight The height of the input image. The total feature space is based on this and the winWidth. - * \br_paper Shengcai Liao, Xiangxin Zhu, Zhen Lei, Lun Zhang, Stan Z. Li - * Learning Multi-scale Block Local Binary Patterns for Face Recognition - * ICB, 2007 - * \br_link Learning Multi-scale Block Local Binary Patterns for Face Recognition http://www.cbsr.ia.ac.cn/users/lzhang/papers/ICB07/ICB07_Liao.pdf - */ -class MBLBPRepresentation : public Representation -{ - Q_OBJECT - - Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false) - Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false) - BR_PROPERTY(int, winWidth, 24) - BR_PROPERTY(int, winHeight, 24) - - void init() - { - if (features.isEmpty()) { - int offset = winWidth + 1; - for (int x = 0; x < winWidth; x++ ) - for (int y = 0; y < winHeight; y++ ) - for (int w = 1; w <= winWidth / 3; w++ ) - for (int h = 1; h <= winHeight / 3; h++ ) - if ((x+3*w <= winWidth) && (y+3*h <= winHeight) ) - features.append(Feature(offset, x, y, w, h ) ); - } - } - - Template preprocess(const Template &src) const - { - Template dst; - integral(src, dst); - return dst; - } - - float evaluate(const Template &src, int idx) const - { - return (float)features[idx].calc(src.m()); - } - - Mat evaluate(const Template &src, const QList &indices) const - { - int size = indices.empty() ? numFeatures() : indices.size(); - - Mat result(1, size, CV_32FC1); - for (int i = 0; i < size; i++) - result.at(i) = evaluate(src, indices.empty() ? i : indices[i]); - return result; - } - - Size windowSize(int *dx, int *dy) const - { - if (dx && dy) - *dx = *dy = 1; - return Size(winWidth, winHeight); - } - - int numFeatures() const { return features.size(); } - int maxCatCount() const { return 256; } - - struct Feature - { - Feature() { rect = Rect(0, 0, 0, 0); } - Feature( int offset, int x, int y, int _block_w, int _block_h ); - uchar calc(const Mat &img) const; - - Rect rect; - int p[16]; - }; - QList features; -}; - -BR_REGISTER(Representation, MBLBPRepresentation) - -static inline void calcOffset(int &p0, int &p1, int &p2, int &p3, Rect rect, int offset) -{ - /* (x, y) */ - p0 = rect.x + offset * rect.y; - /* (x + w, y) */ - p1 = rect.x + rect.width + offset * rect.y; - /* (x + w, y) */ - p2 = rect.x + offset * (rect.y + rect.height); - /* (x + w, y + h) */ - p3 = rect.x + rect.width + offset * (rect.y + rect.height); -} - -MBLBPRepresentation::Feature::Feature( int offset, int x, int y, int _blockWidth, int _blockHeight ) -{ - Rect tr = rect = Rect(x, y, _blockWidth, _blockHeight); - calcOffset(p[0], p[1], p[4], p[5], tr, offset); - tr.x += 2*rect.width; - calcOffset(p[2], p[3], p[6], p[7], tr, offset); - tr.y +=2*rect.height; - calcOffset(p[10], p[11], p[14], p[15], tr, offset); - tr.x -= 2*rect.width; - calcOffset(p[8], p[9], p[12], p[13], tr, offset); -} - -inline uchar MBLBPRepresentation::Feature::calc(const Mat &img) const -{ - const int* ptr = img.ptr(); - int cval = ptr[p[5]] - ptr[p[6]] - ptr[p[9]] + ptr[p[10]]; - - return (uchar)((ptr[p[0]] - ptr[p[1]] - ptr[p[4]] + ptr[p[5]] >= cval ? 128 : 0) | // 0 - (ptr[p[1]] - ptr[p[2]] - ptr[p[5]] + ptr[p[6]] >= cval ? 64 : 0) | // 1 - (ptr[p[2]] - ptr[p[3]] - ptr[p[6]] + ptr[p[7]] >= cval ? 32 : 0) | // 2 - (ptr[p[6]] - ptr[p[7]] - ptr[p[10]] + ptr[p[11]] >= cval ? 16 : 0) | // 5 - (ptr[p[10]] - ptr[p[11]] - ptr[p[14]] + ptr[p[15]] >= cval ? 8 : 0) | // 8 - (ptr[p[9]] - ptr[p[10]] - ptr[p[13]] + ptr[p[14]] >= cval ? 4 : 0) | // 7 - (ptr[p[8]] - ptr[p[9]] - ptr[p[12]] + ptr[p[13]] >= cval ? 2 : 0) | // 6 - (ptr[p[4]] - ptr[p[5]] - ptr[p[8]] + ptr[p[9]] >= cval ? 1 : 0)); // 3 -} - -} // namespace br - -#include "representation/mblbp.moc" diff --git a/openbr/plugins/representation/npd.cpp b/openbr/plugins/representation/npd.cpp deleted file mode 100644 index 90655f8..0000000 --- a/openbr/plugins/representation/npd.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include - -using namespace cv; - -namespace br -{ - -class NPDRepresentation : public Representation -{ - Q_OBJECT - - Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false) - Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false) - BR_PROPERTY(int, winWidth, 24) - BR_PROPERTY(int, winHeight, 24) - - void init() - { - if (features.isEmpty()) { - for (int p1 = 0; p1 < (winWidth * winHeight); p1++) - for (int p2 = p1; p2 < (winWidth * winHeight); p2++) - features.append(Feature(p1, p2)); - } - } - - float evaluate(const Template &src, int idx) const - { - return features[idx].calc(src.m()); - } - - Mat evaluate(const Template &src, const QList &indices) const - { - int size = indices.empty() ? numFeatures() : indices.size(); - - Mat result(1, size, CV_32FC1); - for (int i = 0; i < size; i++) - result.at(i) = evaluate(src, indices.empty() ? i : indices[i]); - return result; - } - - Size windowSize(int *dx, int *dy) const - { - if (dx && dy) - *dx = *dy = 0; - return Size(winWidth, winHeight); - } - - int numFeatures() const { return features.size(); } - int maxCatCount() const { return 0; } - - struct Feature - { - Feature() {} - Feature(int p0, int p1) : p0(p0), p1(p1) {} - float calc(const Mat &image) const; - - int p0, p1; - }; - QList features; -}; - -BR_REGISTER(Representation, NPDRepresentation) - -inline float NPDRepresentation::Feature::calc(const Mat &image) const -{ - const uchar *ptr = image.ptr(); - int v1 = (int)ptr[p0], v2 = (int)ptr[p1]; - return (v1 + v2) == 0 ? 0 : (1.0 * (v1 - v2)) / (v1 + v2); -} - -} // namespace br - -#include "representation/npd.moc" diff --git a/openbr/plugins/representation/random.cpp b/openbr/plugins/representation/random.cpp deleted file mode 100644 index 1fc56bd..0000000 --- a/openbr/plugins/representation/random.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include - -#include -#include -#include - -using namespace cv; - -namespace br -{ - -/*! - * \ingroup representations - * \brief A meta Representation that creates a smaller, random feature space from the full feature space of a given representation. - * \author Scott Klum \cite sklum - * \br_property Representation* representation The representation from which to create the random feature space - * \br_property int count The size of the random feature space - */ -class RandomRepresentation : public Representation -{ - Q_OBJECT - - Q_PROPERTY(br::Representation* representation READ get_representation WRITE set_representation RESET reset_representation STORED false) - Q_PROPERTY(int count READ get_count WRITE set_count RESET reset_count STORED false) - BR_PROPERTY(br::Representation*, representation, NULL) - BR_PROPERTY(int, count, 20000) - - QList features; - - void train(const TemplateList &data) - { - representation->train(data); - - const int nFeatures = representation->numFeatures(); - - if (Globals->verbose) - qDebug() << "Randomly sampling from" << nFeatures << "features."; - - features = Common::RandSample(count,nFeatures,0,true); - } - - Template preprocess(const Template &src) const - { - return representation->preprocess(src); - } - - float evaluate(const Template &src, int idx) const - { - return representation->evaluate(src, features[idx]); - } - - Mat evaluate(const Template &src, const QList &indices) const - { - QList newIndices; - if (indices.empty()) - newIndices = features; - else - for (int i = 0; i < indices.size(); i++) - newIndices.append(features[indices[i]]); - - return representation->evaluate(src, newIndices); - } - - int numFeatures() const - { - return features.size(); - } - - int numChannels() const - { - return representation->numChannels(); - } - - Size windowSize(int *dx, int *dy) const - { - return representation->windowSize(dx,dy); - } - - int maxCatCount() const - { - return representation->maxCatCount(); - } - - void load(QDataStream &stream) - { - representation->load(stream); - - int numFeatures; stream >> numFeatures; - for (int i=0; i> feature; - features.append(feature); - } - } - - void store(QDataStream &stream) const - { - representation->store(stream); - - stream << features.size(); - for (int i=0; i