Commit 3c99d0905921176e9251b6d10fd1561f2d93472b

Authored by Josh Klontz
1 parent 8f693acf

remove representations

openbr/plugins/representation/haar.cpp deleted
1   -#include <opencv2/imgproc/imgproc.hpp>
2   -
3   -#include <openbr/plugins/openbr_internal.h>
4   -#include <openbr/core/opencvutils.h>
5   -
6   -using namespace cv;
7   -
8   -namespace br
9   -{
10   -
11   -#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step ) \
12   - /* (x, y) */ \
13   - (p0) = (rect).x + (step) * (rect).y; \
14   - /* (x + w, y) */ \
15   - (p1) = (rect).x + (rect).width + (step) * (rect).y; \
16   - /* (x + w, y) */ \
17   - (p2) = (rect).x + (step) * ((rect).y + (rect).height); \
18   - /* (x + w, y + h) */ \
19   - (p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height);
20   -
21   -/*!
22   - * \brief An implementation of Haar Features for Viola-Jones cascade object detection
23   - * \author Jordan Cheney \cite jcheney
24   - * \br_property int winWidth The width of the input image. The total feature space is based on this and the winHeight
25   - * \br_property int winHeight The height of the input image. The total feature space is based on this and the winWidth.
26   - * \br_paper Paul Viola, Michael Jones
27   - * Rapid Object Detection using a Boosted Cascade of Simple Features
28   - * CVPR, 2001
29   - * \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
30   - */
31   -class HaarRepresentation : public Representation
32   -{
33   - Q_OBJECT
34   -
35   - Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false)
36   - Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false)
37   - BR_PROPERTY(int, winWidth, 24)
38   - BR_PROPERTY(int, winHeight, 24)
39   -
40   - void init()
41   - {
42   - if (features.isEmpty()) {
43   - // Pre-determine the size of features to avoid reallocations
44   - int numFeatures = 0;
45   - for (int x = 0; x < winWidth; x++)
46   - for (int y = 0; y < winHeight; y++)
47   - for (int dx = 1; dx <= winWidth; dx++)
48   - for (int dy = 1; dy <= winHeight; dy++)
49   - numFeatures += ((x+dx*2 <= winWidth) && (y+dy <= winHeight))
50   - + ((x+dx <= winWidth) && (y+dy*2 <= winHeight))
51   - + ((x+dx*3 <= winWidth) && (y+dy <= winHeight))
52   - + ((x+dx <= winWidth) && (y+dy*3 <= winHeight))
53   - + ((x+dx*2 <= winWidth) && (y+dy*2 <= winHeight));
54   - features.reserve(numFeatures);
55   -
56   - const int offset = winWidth + 1;
57   - int index = 0;
58   - for (int x = 0; x < winWidth; x++) {
59   - for (int y = 0; y < winHeight; y++) {
60   - for (int dx = 1; dx <= winWidth; dx++) {
61   - for (int dy = 1; dy <= winHeight; dy++) {
62   - // haar_x2
63   - if ((x+dx*2 <= winWidth) && (y+dy <= winHeight))
64   - features[index++] = Feature(offset,
65   - x, y, dx*2, dy, -1,
66   - x+dx, y, dx , dy, +2);
67   - // haar_y2
68   - if ((x+dx <= winWidth) && (y+dy*2 <= winHeight))
69   - features[index++] = Feature(offset,
70   - x, y, dx, dy*2, -1,
71   - x, y+dy, dx, dy, +2);
72   - // haar_x3
73   - if ((x+dx*3 <= winWidth) && (y+dy <= winHeight))
74   - features[index++] = Feature(offset,
75   - x, y, dx*3, dy, -1,
76   - x+dx, y, dx , dy, +3);
77   - // haar_y3
78   - if ((x+dx <= winWidth) && (y+dy*3 <= winHeight))
79   - features[index++] = Feature(offset,
80   - x, y, dx, dy*3, -1,
81   - x, y+dy, dx, dy, +3);
82   - // x2_y2
83   - if ((x+dx*2 <= winWidth) && (y+dy*2 <= winHeight))
84   - features[index++] = Feature(offset,
85   - x, y, dx*2, dy*2, -1,
86   - x, y, dx, dy, +2,
87   - x+dx, y+dy, dx, dy, +2);
88   -
89   -
90   - }
91   - }
92   - }
93   - }
94   - }
95   - }
96   -
97   - Template preprocess(const Template &src) const
98   - {
99   - Template dst;
100   - integral(src, dst);
101   - return dst;
102   - }
103   -
104   - float evaluate(const Template &src, int idx) const
105   - {
106   - return features[idx].calc(src.m());
107   - }
108   -
109   - Mat evaluate(const Template &src, const QList<int> &indices) const
110   - {
111   - int size = indices.empty() ? numFeatures() : indices.size();
112   -
113   - Mat result(1, size, CV_32FC1);
114   - for (int i = 0; i < size; i++)
115   - result.at<float>(i) = evaluate(src, indices.empty() ? i : indices[i]);
116   - return result;
117   - }
118   -
119   - int numFeatures() const
120   - {
121   - return features.size();
122   - }
123   -
124   - Size windowSize(int *dx, int *dy) const
125   - {
126   - if (dx && dy)
127   - *dx = *dy = 1;
128   - return Size(winWidth, winHeight);
129   - }
130   -
131   - int maxCatCount() const { return 0; }
132   -
133   - struct Feature
134   - {
135   - Feature();
136   - Feature( int offset,
137   - int x0, int y0, int w0, int h0, float wt0,
138   - int x1, int y1, int w1, int h1, float wt1,
139   - int x2 = 0, int y2 = 0, int w2 = 0, int h2 = 0, float wt2 = 0.0F );
140   - float calc(const Mat &img) const;
141   -
142   - struct {
143   - float weight;
144   - int p0, p1, p2, p3;
145   - } fastRect[3];
146   - };
147   -
148   - QVector<Feature> features;
149   -};
150   -
151   -BR_REGISTER(Representation, HaarRepresentation)
152   -
153   -HaarRepresentation::Feature::Feature()
154   -{
155   - fastRect[0].p0 = fastRect[1].p0 = fastRect[2].p0 = 0;
156   - fastRect[0].p1 = fastRect[1].p1 = fastRect[2].p1 = 0;
157   - fastRect[0].p2 = fastRect[1].p2 = fastRect[2].p2 = 0;
158   - fastRect[0].p3 = fastRect[1].p3 = fastRect[2].p3 = 0;
159   - fastRect[0].weight = fastRect[1].weight = fastRect[2].weight = 0;
160   -}
161   -
162   -HaarRepresentation::Feature::Feature(int offset,
163   - int x0, int y0, int w0, int h0, float wt0,
164   - int x1, int y1, int w1, int h1, float wt1,
165   - int x2, int y2, int w2, int h2, float wt2)
166   -{
167   - CV_SUM_OFFSETS(fastRect[0].p0, fastRect[0].p1, fastRect[0].p2, fastRect[0].p3, Rect(x0, y0, w0, h0), offset)
168   - CV_SUM_OFFSETS(fastRect[1].p0, fastRect[1].p1, fastRect[1].p2, fastRect[1].p3, Rect(x1, y1, w1, h1), offset)
169   - CV_SUM_OFFSETS(fastRect[2].p0, fastRect[2].p1, fastRect[2].p2, fastRect[2].p3, Rect(x2, y2, w2, h2), offset)
170   - fastRect[0].weight = wt0;
171   - fastRect[1].weight = wt1;
172   - fastRect[2].weight = wt2;
173   -}
174   -
175   -inline float HaarRepresentation::Feature::calc(const Mat &img) const
176   -{
177   - const int* ptr = img.ptr<int>();
178   - float ret = fastRect[0].weight * (ptr[fastRect[0].p0] - ptr[fastRect[0].p1] - ptr[fastRect[0].p2] + ptr[fastRect[0].p3]) +
179   - fastRect[1].weight * (ptr[fastRect[1].p0] - ptr[fastRect[1].p1] - ptr[fastRect[1].p2] + ptr[fastRect[1].p3]);
180   - if (fastRect[2].weight != 0.0f)
181   - ret += fastRect[2].weight * (ptr[fastRect[2].p0] - ptr[fastRect[2].p1] - ptr[fastRect[2].p2] + ptr[fastRect[2].p3]);
182   - return ret;
183   -}
184   -
185   -} // namespace br
186   -
187   -#include "representation/haar.moc"
188   -
openbr/plugins/representation/mblbp.cpp deleted
1   -#include <opencv2/imgproc/imgproc.hpp>
2   -
3   -#include <openbr/plugins/openbr_internal.h>
4   -#include <openbr/core/opencvutils.h>
5   -
6   -using namespace cv;
7   -
8   -namespace br
9   -{
10   -
11   -#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step ) \
12   - /* (x, y) */ \
13   - (p0) = (rect).x + (step) * (rect).y; \
14   - /* (x + w, y) */ \
15   - (p1) = (rect).x + (rect).width + (step) * (rect).y; \
16   - /* (x + w, y) */ \
17   - (p2) = (rect).x + (step) * ((rect).y + (rect).height); \
18   - /* (x + w, y + h) */ \
19   - (p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height);
20   -
21   -/*!
22   - * \brief An implementation of MBLBP as an OpenBR Representation
23   - * \author Jordan Cheney \cite jcheney
24   - * \br_property int winWidth The width of the input image. The total feature space is based on this and the winHeight
25   - * \br_property int winHeight The height of the input image. The total feature space is based on this and the winWidth.
26   - * \br_paper Shengcai Liao, Xiangxin Zhu, Zhen Lei, Lun Zhang, Stan Z. Li
27   - * Learning Multi-scale Block Local Binary Patterns for Face Recognition
28   - * ICB, 2007
29   - * \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
30   - */
31   -class MBLBPRepresentation : public Representation
32   -{
33   - Q_OBJECT
34   -
35   - Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false)
36   - Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false)
37   - BR_PROPERTY(int, winWidth, 24)
38   - BR_PROPERTY(int, winHeight, 24)
39   -
40   - void init()
41   - {
42   - if (features.isEmpty()) {
43   - int offset = winWidth + 1;
44   - for (int x = 0; x < winWidth; x++ )
45   - for (int y = 0; y < winHeight; y++ )
46   - for (int w = 1; w <= winWidth / 3; w++ )
47   - for (int h = 1; h <= winHeight / 3; h++ )
48   - if ((x+3*w <= winWidth) && (y+3*h <= winHeight) )
49   - features.append(Feature(offset, x, y, w, h ) );
50   - }
51   - }
52   -
53   - Template preprocess(const Template &src) const
54   - {
55   - Template dst;
56   - integral(src, dst);
57   - return dst;
58   - }
59   -
60   - float evaluate(const Template &src, int idx) const
61   - {
62   - return (float)features[idx].calc(src.m());
63   - }
64   -
65   - Mat evaluate(const Template &src, const QList<int> &indices) const
66   - {
67   - int size = indices.empty() ? numFeatures() : indices.size();
68   -
69   - Mat result(1, size, CV_32FC1);
70   - for (int i = 0; i < size; i++)
71   - result.at<float>(i) = evaluate(src, indices.empty() ? i : indices[i]);
72   - return result;
73   - }
74   -
75   - Size windowSize(int *dx, int *dy) const
76   - {
77   - if (dx && dy)
78   - *dx = *dy = 1;
79   - return Size(winWidth, winHeight);
80   - }
81   -
82   - int numFeatures() const { return features.size(); }
83   - int maxCatCount() const { return 256; }
84   -
85   - struct Feature
86   - {
87   - Feature() { rect = Rect(0, 0, 0, 0); }
88   - Feature( int offset, int x, int y, int _block_w, int _block_h );
89   - uchar calc(const Mat &img) const;
90   -
91   - Rect rect;
92   - int p[16];
93   - };
94   - QList<Feature> features;
95   -};
96   -
97   -BR_REGISTER(Representation, MBLBPRepresentation)
98   -
99   -static inline void calcOffset(int &p0, int &p1, int &p2, int &p3, Rect rect, int offset)
100   -{
101   - /* (x, y) */
102   - p0 = rect.x + offset * rect.y;
103   - /* (x + w, y) */
104   - p1 = rect.x + rect.width + offset * rect.y;
105   - /* (x + w, y) */
106   - p2 = rect.x + offset * (rect.y + rect.height);
107   - /* (x + w, y + h) */
108   - p3 = rect.x + rect.width + offset * (rect.y + rect.height);
109   -}
110   -
111   -MBLBPRepresentation::Feature::Feature( int offset, int x, int y, int _blockWidth, int _blockHeight )
112   -{
113   - Rect tr = rect = Rect(x, y, _blockWidth, _blockHeight);
114   - calcOffset(p[0], p[1], p[4], p[5], tr, offset);
115   - tr.x += 2*rect.width;
116   - calcOffset(p[2], p[3], p[6], p[7], tr, offset);
117   - tr.y +=2*rect.height;
118   - calcOffset(p[10], p[11], p[14], p[15], tr, offset);
119   - tr.x -= 2*rect.width;
120   - calcOffset(p[8], p[9], p[12], p[13], tr, offset);
121   -}
122   -
123   -inline uchar MBLBPRepresentation::Feature::calc(const Mat &img) const
124   -{
125   - const int* ptr = img.ptr<int>();
126   - int cval = ptr[p[5]] - ptr[p[6]] - ptr[p[9]] + ptr[p[10]];
127   -
128   - return (uchar)((ptr[p[0]] - ptr[p[1]] - ptr[p[4]] + ptr[p[5]] >= cval ? 128 : 0) | // 0
129   - (ptr[p[1]] - ptr[p[2]] - ptr[p[5]] + ptr[p[6]] >= cval ? 64 : 0) | // 1
130   - (ptr[p[2]] - ptr[p[3]] - ptr[p[6]] + ptr[p[7]] >= cval ? 32 : 0) | // 2
131   - (ptr[p[6]] - ptr[p[7]] - ptr[p[10]] + ptr[p[11]] >= cval ? 16 : 0) | // 5
132   - (ptr[p[10]] - ptr[p[11]] - ptr[p[14]] + ptr[p[15]] >= cval ? 8 : 0) | // 8
133   - (ptr[p[9]] - ptr[p[10]] - ptr[p[13]] + ptr[p[14]] >= cval ? 4 : 0) | // 7
134   - (ptr[p[8]] - ptr[p[9]] - ptr[p[12]] + ptr[p[13]] >= cval ? 2 : 0) | // 6
135   - (ptr[p[4]] - ptr[p[5]] - ptr[p[8]] + ptr[p[9]] >= cval ? 1 : 0)); // 3
136   -}
137   -
138   -} // namespace br
139   -
140   -#include "representation/mblbp.moc"
openbr/plugins/representation/npd.cpp deleted
1   -#include <openbr/plugins/openbr_internal.h>
2   -
3   -using namespace cv;
4   -
5   -namespace br
6   -{
7   -
8   -class NPDRepresentation : public Representation
9   -{
10   - Q_OBJECT
11   -
12   - Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false)
13   - Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false)
14   - BR_PROPERTY(int, winWidth, 24)
15   - BR_PROPERTY(int, winHeight, 24)
16   -
17   - void init()
18   - {
19   - if (features.isEmpty()) {
20   - for (int p1 = 0; p1 < (winWidth * winHeight); p1++)
21   - for (int p2 = p1; p2 < (winWidth * winHeight); p2++)
22   - features.append(Feature(p1, p2));
23   - }
24   - }
25   -
26   - float evaluate(const Template &src, int idx) const
27   - {
28   - return features[idx].calc(src.m());
29   - }
30   -
31   - Mat evaluate(const Template &src, const QList<int> &indices) const
32   - {
33   - int size = indices.empty() ? numFeatures() : indices.size();
34   -
35   - Mat result(1, size, CV_32FC1);
36   - for (int i = 0; i < size; i++)
37   - result.at<float>(i) = evaluate(src, indices.empty() ? i : indices[i]);
38   - return result;
39   - }
40   -
41   - Size windowSize(int *dx, int *dy) const
42   - {
43   - if (dx && dy)
44   - *dx = *dy = 0;
45   - return Size(winWidth, winHeight);
46   - }
47   -
48   - int numFeatures() const { return features.size(); }
49   - int maxCatCount() const { return 0; }
50   -
51   - struct Feature
52   - {
53   - Feature() {}
54   - Feature(int p0, int p1) : p0(p0), p1(p1) {}
55   - float calc(const Mat &image) const;
56   -
57   - int p0, p1;
58   - };
59   - QList<Feature> features;
60   -};
61   -
62   -BR_REGISTER(Representation, NPDRepresentation)
63   -
64   -inline float NPDRepresentation::Feature::calc(const Mat &image) const
65   -{
66   - const uchar *ptr = image.ptr();
67   - int v1 = (int)ptr[p0], v2 = (int)ptr[p1];
68   - return (v1 + v2) == 0 ? 0 : (1.0 * (v1 - v2)) / (v1 + v2);
69   -}
70   -
71   -} // namespace br
72   -
73   -#include "representation/npd.moc"
openbr/plugins/representation/random.cpp deleted
1   -#include <opencv2/imgproc/imgproc.hpp>
2   -#include <opencv2/highgui/highgui.hpp>
3   -
4   -#include <openbr/plugins/openbr_internal.h>
5   -#include <openbr/core/opencvutils.h>
6   -#include <openbr/core/common.h>
7   -
8   -using namespace cv;
9   -
10   -namespace br
11   -{
12   -
13   -/*!
14   - * \ingroup representations
15   - * \brief A meta Representation that creates a smaller, random feature space from the full feature space of a given representation.
16   - * \author Scott Klum \cite sklum
17   - * \br_property Representation* representation The representation from which to create the random feature space
18   - * \br_property int count The size of the random feature space
19   - */
20   -class RandomRepresentation : public Representation
21   -{
22   - Q_OBJECT
23   -
24   - Q_PROPERTY(br::Representation* representation READ get_representation WRITE set_representation RESET reset_representation STORED false)
25   - Q_PROPERTY(int count READ get_count WRITE set_count RESET reset_count STORED false)
26   - BR_PROPERTY(br::Representation*, representation, NULL)
27   - BR_PROPERTY(int, count, 20000)
28   -
29   - QList<int> features;
30   -
31   - void train(const TemplateList &data)
32   - {
33   - representation->train(data);
34   -
35   - const int nFeatures = representation->numFeatures();
36   -
37   - if (Globals->verbose)
38   - qDebug() << "Randomly sampling from" << nFeatures << "features.";
39   -
40   - features = Common::RandSample(count,nFeatures,0,true);
41   - }
42   -
43   - Template preprocess(const Template &src) const
44   - {
45   - return representation->preprocess(src);
46   - }
47   -
48   - float evaluate(const Template &src, int idx) const
49   - {
50   - return representation->evaluate(src, features[idx]);
51   - }
52   -
53   - Mat evaluate(const Template &src, const QList<int> &indices) const
54   - {
55   - QList<int> newIndices;
56   - if (indices.empty())
57   - newIndices = features;
58   - else
59   - for (int i = 0; i < indices.size(); i++)
60   - newIndices.append(features[indices[i]]);
61   -
62   - return representation->evaluate(src, newIndices);
63   - }
64   -
65   - int numFeatures() const
66   - {
67   - return features.size();
68   - }
69   -
70   - int numChannels() const
71   - {
72   - return representation->numChannels();
73   - }
74   -
75   - Size windowSize(int *dx, int *dy) const
76   - {
77   - return representation->windowSize(dx,dy);
78   - }
79   -
80   - int maxCatCount() const
81   - {
82   - return representation->maxCatCount();
83   - }
84   -
85   - void load(QDataStream &stream)
86   - {
87   - representation->load(stream);
88   -
89   - int numFeatures; stream >> numFeatures;
90   - for (int i=0; i<numFeatures; i++) {
91   - int feature; stream >> feature;
92   - features.append(feature);
93   - }
94   - }
95   -
96   - void store(QDataStream &stream) const
97   - {
98   - representation->store(stream);
99   -
100   - stream << features.size();
101   - for (int i=0; i<features.size(); i++)
102   - stream << features[i];
103   - }
104   -};
105   -
106   -BR_REGISTER(Representation, RandomRepresentation)
107   -
108   -} // namespace br
109   -
110   -#include "representation/random.moc"