Commit 23bd6de45a37ef8b25a3f6b9d3edf469dd952312

Authored by Jordan Cheney
1 parent a231cbed

Smarter use of pointers

openbr/core/boost.cpp
@@ -144,7 +144,7 @@ void FeatureEvaluator::init(Representation *_representation, int _maxSampleCount @@ -144,7 +144,7 @@ void FeatureEvaluator::init(Representation *_representation, int _maxSampleCount
144 representation = _representation; 144 representation = _representation;
145 145
146 int dx, dy; 146 int dx, dy;
147 - Size windowSize = representation->windowSize(dx, dy); 147 + Size windowSize = representation->windowSize(&dx, &dy);
148 data.create((int)_maxSampleCount, Size(windowSize.width + dx, windowSize.height + dy).area(), CV_32SC1); 148 data.create((int)_maxSampleCount, Size(windowSize.width + dx, windowSize.height + dy).area(), CV_32SC1);
149 cls.create( (int)_maxSampleCount, 1, CV_32FC1 ); 149 cls.create( (int)_maxSampleCount, 1, CV_32FC1 );
150 } 150 }
@@ -154,7 +154,7 @@ void FeatureEvaluator::setImage(const Mat &img, uchar clsLabel, int idx) @@ -154,7 +154,7 @@ void FeatureEvaluator::setImage(const Mat &img, uchar clsLabel, int idx)
154 cls.ptr<float>(idx)[0] = clsLabel; 154 cls.ptr<float>(idx)[0] = clsLabel;
155 155
156 int dx, dy; 156 int dx, dy;
157 - Size windowSize = representation->windowSize(dx, dy); 157 + Size windowSize = representation->windowSize(&dx, &dy);
158 Mat integralImg(Size(windowSize.width + dx, windowSize.height + dy), data.type(), data.ptr<int>(idx)); 158 Mat integralImg(Size(windowSize.width + dx, windowSize.height + dy), data.type(), data.ptr<int>(idx));
159 representation->preprocess(img, integralImg); 159 representation->preprocess(img, integralImg);
160 } 160 }
openbr/core/cascade.cpp
@@ -232,7 +232,7 @@ void _CascadeClassifier::detectMultiScale(const Mat&amp; image, vector&lt;Rect&gt;&amp; object @@ -232,7 +232,7 @@ void _CascadeClassifier::detectMultiScale(const Mat&amp; image, vector&lt;Rect&gt;&amp; object
232 232
233 for (double factor = 1; ; factor *= scaleFactor) { 233 for (double factor = 1; ; factor *= scaleFactor) {
234 int dx, dy; 234 int dx, dy;
235 - Size originalWindowSize = representation->windowSize(dx, dy); 235 + Size originalWindowSize = representation->windowSize(&dx, &dy);
236 236
237 Size windowSize(cvRound(originalWindowSize.width*factor), cvRound(originalWindowSize.height*factor) ); 237 Size windowSize(cvRound(originalWindowSize.width*factor), cvRound(originalWindowSize.height*factor) );
238 Size scaledImageSize(cvRound(image.cols/factor ), cvRound(image.rows/factor)); 238 Size scaledImageSize(cvRound(image.cols/factor ), cvRound(image.rows/factor));
openbr/openbr_plugin.h
@@ -1407,7 +1407,7 @@ public: @@ -1407,7 +1407,7 @@ public:
1407 // and returned. 1407 // and returned.
1408 virtual cv::Mat evaluate(const cv::Mat &image, const QList<int> &indices = QList<int>()) const = 0; 1408 virtual cv::Mat evaluate(const cv::Mat &image, const QList<int> &indices = QList<int>()) const = 0;
1409 1409
1410 - virtual cv::Size windowSize(int &dx, int &dy) const = 0; // dx and dy should indicate the change to the original window size after preprocessing 1410 + virtual cv::Size windowSize(int *dx = NULL, int *dy = NULL) const = 0; // dx and dy should indicate the change to the original window size after preprocessing
1411 virtual int numFeatures() const = 0; 1411 virtual int numFeatures() const = 0;
1412 virtual int maxCatCount() const = 0; 1412 virtual int maxCatCount() const = 0;
1413 }; 1413 };
@@ -1422,11 +1422,11 @@ public: @@ -1422,11 +1422,11 @@ public:
1422 static Classifier *make(QString str, QObject *parent); /*!< \brief Make a classifier from a string. */ 1422 static Classifier *make(QString str, QObject *parent); /*!< \brief Make a classifier from a string. */
1423 1423
1424 virtual void train(const QList<cv::Mat> &images, const QList<float> &labels) = 0; 1424 virtual void train(const QList<cv::Mat> &images, const QList<float> &labels) = 0;
1425 - virtual float classify(const cv::Mat &image, float &confidence) const = 0; 1425 + virtual float classify(const cv::Mat &image, float *confidence = NULL) const = 0;
1426 1426
1427 // Slots for representation 1427 // Slots for representation
1428 virtual cv::Mat preprocess(const cv::Mat &image) const = 0; 1428 virtual cv::Mat preprocess(const cv::Mat &image) const = 0;
1429 - virtual cv::Size windowSize(int &dx, int &dy) const = 0; 1429 + virtual cv::Size windowSize(int *dx = NULL, int *dy = NULL) const = 0;
1430 1430
1431 // OpenCV compatibility 1431 // OpenCV compatibility
1432 virtual int numFeatures() const = 0; 1432 virtual int numFeatures() const = 0;
openbr/plugins/classification/boostedforest.cpp
@@ -129,9 +129,9 @@ class BoostedForestClassifier : public Classifier @@ -129,9 +129,9 @@ class BoostedForestClassifier : public Classifier
129 } 129 }
130 } 130 }
131 131
132 - float classify(const Mat &image, float &confidence) const 132 + float classify(const Mat &image, float *confidence) const
133 { 133 {
134 - confidence = 0; 134 + float sum = 0;
135 for (int i = 0; i < classifiers.size(); i++) { 135 for (int i = 0; i < classifiers.size(); i++) {
136 Node *node = classifiers[i]; 136 Node *node = classifiers[i];
137 137
@@ -144,10 +144,12 @@ class BoostedForestClassifier : public Classifier @@ -144,10 +144,12 @@ class BoostedForestClassifier : public Classifier
144 node = val <= node->threshold ? node->left : node->right; 144 node = val <= node->threshold ? node->left : node->right;
145 } 145 }
146 } 146 }
147 - confidence += node->value; 147 + sum += node->value;
148 } 148 }
149 149
150 - return confidence < threshold - THRESHOLD_EPS ? 0.0f : 1.0f; 150 + if (confidence)
  151 + *confidence = sum;
  152 + return sum < threshold - THRESHOLD_EPS ? 0.0f : 1.0f;
151 } 153 }
152 154
153 int numFeatures() const 155 int numFeatures() const
@@ -167,7 +169,7 @@ class BoostedForestClassifier : public Classifier @@ -167,7 +169,7 @@ class BoostedForestClassifier : public Classifier
167 return dst; 169 return dst;
168 } 170 }
169 171
170 - Size windowSize(int &dx, int &dy) const 172 + Size windowSize(int *dx, int *dy) const
171 { 173 {
172 return representation->windowSize(dx, dy); 174 return representation->windowSize(dx, dy);
173 } 175 }
openbr/plugins/classification/cascade.cpp
@@ -152,23 +152,18 @@ class CascadeClassifier : public Classifier @@ -152,23 +152,18 @@ class CascadeClassifier : public Classifier
152 } 152 }
153 } 153 }
154 154
155 - float classify(const Mat &image, float &confidence) const 155 + float classify(const Mat &image, float *confidence) const
156 { 156 {
157 - if (stages.empty()) {  
158 - confidence = 0.0f;  
159 - return 1.0f;  
160 - }  
161 -  
162 - for (int i = 0; i < stages.size(); i++) {  
163 - float result = stages[i]->classify(image, confidence); 157 + float stageConf = 0.0f;
  158 + foreach (const Classifier *stage, stages) {
  159 + float result = stage->classify(image, &stageConf);
164 if (result == 0.0f) { 160 if (result == 0.0f) {
165 - //confidence *= i;  
166 - return i; 161 + if (confidence)
  162 + *confidence += stageConf;
  163 + return 0.0f;
167 } 164 }
168 } 165 }
169 -  
170 - //confidence *= stages.size();  
171 - return stages.size(); 166 + return 1.0f;
172 } 167 }
173 168
174 int numFeatures() const 169 int numFeatures() const
@@ -186,7 +181,7 @@ class CascadeClassifier : public Classifier @@ -186,7 +181,7 @@ class CascadeClassifier : public Classifier
186 return stages.first()->preprocess(image); 181 return stages.first()->preprocess(image);
187 } 182 }
188 183
189 - Size windowSize(int &dx, int &dy) const 184 + Size windowSize(int *dx, int *dy) const
190 { 185 {
191 return stages.first()->windowSize(dx, dy); 186 return stages.first()->windowSize(dx, dy);
192 } 187 }
@@ -217,14 +212,14 @@ private: @@ -217,14 +212,14 @@ private:
217 { 212 {
218 imgHandler.restart(); 213 imgHandler.restart();
219 214
220 - float confidence = 0.0f; // not used; 215 + float confidence = 0.0f;
221 216
222 while (images.size() < numPos) { 217 while (images.size() < numPos) {
223 Mat pos(imgHandler.winSize, CV_8UC1); 218 Mat pos(imgHandler.winSize, CV_8UC1);
224 if (!imgHandler.getPos(pos)) 219 if (!imgHandler.getPos(pos))
225 qFatal("Cannot get another positive sample!"); 220 qFatal("Cannot get another positive sample!");
226 221
227 - if (classify(pos, confidence) > 0.0f) { 222 + if (classify(pos, &confidence) > 0.0f) {
228 printf("POS current samples: %d\r", images.size()); 223 printf("POS current samples: %d\r", images.size());
229 images.append(pos); 224 images.append(pos);
230 labels.append(1.0f); 225 labels.append(1.0f);
@@ -240,7 +235,7 @@ private: @@ -240,7 +235,7 @@ private:
240 if (!imgHandler.getNeg(neg)) 235 if (!imgHandler.getNeg(neg))
241 qFatal("Cannot get another negative sample!"); 236 qFatal("Cannot get another negative sample!");
242 237
243 - if (classify(neg, confidence) > 0.0f) { 238 + if (classify(neg, &confidence) > 0.0f) {
244 printf("NEG current samples: %d\r", images.size() - posCount); 239 printf("NEG current samples: %d\r", images.size() - posCount);
245 images.append(neg); 240 images.append(neg);
246 labels.append(0.0f); 241 labels.append(0.0f);
openbr/plugins/imgproc/slidingwindow.cpp
@@ -100,7 +100,7 @@ class SlidingWindowTransform : public MetaTransform @@ -100,7 +100,7 @@ class SlidingWindowTransform : public MetaTransform
100 100
101 for (double factor = 1; ; factor *= scaleFactor) { 101 for (double factor = 1; ; factor *= scaleFactor) {
102 int dx, dy; 102 int dx, dy;
103 - Size originalWindowSize = classifier->windowSize(dx, dy); 103 + Size originalWindowSize = classifier->windowSize(&dx, &dy);
104 104
105 Size windowSize(cvRound(originalWindowSize.width*factor), cvRound(originalWindowSize.height*factor) ); 105 Size windowSize(cvRound(originalWindowSize.width*factor), cvRound(originalWindowSize.height*factor) );
106 Size scaledImageSize(cvRound(m.cols/factor ), cvRound(m.rows/factor)); 106 Size scaledImageSize(cvRound(m.cols/factor ), cvRound(m.rows/factor));
@@ -124,7 +124,7 @@ class SlidingWindowTransform : public MetaTransform @@ -124,7 +124,7 @@ class SlidingWindowTransform : public MetaTransform
124 Mat window = repImage(Rect(Point(x, y), Size(originalWindowSize.width + dx, originalWindowSize.height + dy))).clone(); 124 Mat window = repImage(Rect(Point(x, y), Size(originalWindowSize.width + dx, originalWindowSize.height + dy))).clone();
125 125
126 float gypWeight; 126 float gypWeight;
127 - int result = classifier->classify(window, gypWeight); 127 + int result = classifier->classify(window, &gypWeight);
128 128
129 if (12 - result < 4) { 129 if (12 - result < 4) {
130 rects.push_back(Rect(cvRound(x*factor), cvRound(y*factor), windowSize.width, windowSize.height)); 130 rects.push_back(Rect(cvRound(x*factor), cvRound(y*factor), windowSize.width, windowSize.height));
openbr/plugins/representation/haar.cpp
@@ -89,7 +89,14 @@ class HaarRepresentation : public Representation @@ -89,7 +89,14 @@ class HaarRepresentation : public Representation
89 } 89 }
90 90
91 int numFeatures() const { return features.size(); } 91 int numFeatures() const { return features.size(); }
92 - Size windowSize(int &dx, int &dy) const { dx = dy = 1; return Size(winWidth, winHeight); } 92 +
  93 + Size windowSize(int *dx, int *dy) const
  94 + {
  95 + if (dx && dy)
  96 + *dx = *dy = 1;
  97 + return Size(winWidth, winHeight);
  98 + }
  99 +
93 int maxCatCount() const { return 0; } 100 int maxCatCount() const { return 0; }
94 101
95 struct Feature 102 struct Feature
openbr/plugins/representation/mblbp.cpp
@@ -58,7 +58,13 @@ class MBLBPRepresentation : public Representation @@ -58,7 +58,13 @@ class MBLBPRepresentation : public Representation
58 return result; 58 return result;
59 } 59 }
60 60
61 - Size windowSize(int &dx, int &dy) const { dx = dy = 1; return Size(winWidth, winHeight); } 61 + Size windowSize(int *dx, int *dy) const
  62 + {
  63 + if (dx && dy)
  64 + *dx = *dy = 1;
  65 + return Size(winWidth, winHeight);
  66 + }
  67 +
62 int numFeatures() const { return features.size(); } 68 int numFeatures() const { return features.size(); }
63 int maxCatCount() const { return 256; } 69 int maxCatCount() const { return 256; }
64 70
openbr/plugins/representation/npd.cpp 0 → 100644
  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 + for (int p1 = 0; p1 < (winWidth * winHeight); p1++)
  20 + for (int p2 = p1; p2 < (winWidth * winHeight); p2++)
  21 + features.append(Feature(p1, p2));
  22 + }
  23 +
  24 + float evaluate(const Mat &image, int idx) const
  25 + {
  26 + return features[idx].calc(image);
  27 + }
  28 +
  29 + Size windowSize(int *dx, int *dy) const
  30 + {
  31 + if (dx && dy)
  32 + *dx = *dy = 0;
  33 + return Size(winWidth, winHeight);
  34 + }
  35 +
  36 + struct Feature
  37 + {
  38 + Feature() {}
  39 + Feature(int p1, int p2) { p[0] = p1; p[1] = p2; }
  40 + float calc(const Mat &image) const;
  41 +
  42 + int p[2];
  43 + };
  44 + QList<Feature> features;
  45 +};
  46 +
  47 +BR_REGISTER(Representation, NPDRepresentation)
  48 +
  49 +} // namespace br
  50 +
  51 +#include "representation/npd.moc"