Commit 40cad71c9a41fbeba16a04dcb223cbad6cc74a23

Authored by Scott Klum
1 parent 5c72bcd4

Various cascade improvements

openbr/plugins/classification/cascade.cpp
1 #include <opencv2/imgproc/imgproc.hpp> 1 #include <opencv2/imgproc/imgproc.hpp>
2 2
3 #include <openbr/plugins/openbr_internal.h> 3 #include <openbr/plugins/openbr_internal.h>
  4 +#include <openbr/core/common.h>
4 5
5 using namespace cv; 6 using namespace cv;
6 7
@@ -25,6 +26,8 @@ struct ImageHandler @@ -25,6 +26,8 @@ struct ImageHandler
25 stepFactor = 0.5F; 26 stepFactor = 0.5F;
26 round = 0; 27 round = 0;
27 28
  29 + indices = Common::RandSample(posImages.size(),posImages.size(),true);
  30 +
28 return true; 31 return true;
29 } 32 }
30 33
@@ -85,7 +88,7 @@ struct ImageHandler @@ -85,7 +88,7 @@ struct ImageHandler
85 if (posIdx >= posImages.size()) 88 if (posIdx >= posImages.size())
86 return false; 89 return false;
87 90
88 - posImages[posIdx++].copyTo(_img); 91 + posImages[indices[posIdx++]].copyTo(_img);
89 return true; 92 return true;
90 } 93 }
91 94
@@ -100,6 +103,8 @@ struct ImageHandler @@ -100,6 +103,8 @@ struct ImageHandler
100 float stepFactor; 103 float stepFactor;
101 size_t round; 104 size_t round;
102 Size winSize; 105 Size winSize;
  106 +
  107 + QList<int> indices;
103 }; 108 };
104 109
105 /*! 110 /*!
@@ -139,10 +144,15 @@ class CascadeClassifier : public Classifier @@ -139,10 +144,15 @@ class CascadeClassifier : public Classifier
139 for (int i = 0; i < images.size(); i++) 144 for (int i = 0; i < images.size(); i++)
140 labels[i] == 1 ? posImages.append(images[i]) : negImages.append(images[i]); 145 labels[i] == 1 ? posImages.append(images[i]) : negImages.append(images[i]);
141 146
  147 + stages.reserve(numStages);
  148 + for (int i = 0; i < numStages; i++) {
  149 + Classifier *next_stage = Classifier::make(stageDescription, NULL);
  150 + stages.append(next_stage);
  151 + }
  152 +
142 ImageHandler imgHandler; 153 ImageHandler imgHandler;
143 - imgHandler.create(posImages, negImages, Size(24, 24)); 154 + imgHandler.create(posImages, negImages, windowSize());
144 155
145 - stages.reserve(numStages);  
146 for (int i = 0; i < numStages; i++) { 156 for (int i = 0; i < numStages; i++) {
147 qDebug() << "===== TRAINING" << i << "stage ====="; 157 qDebug() << "===== TRAINING" << i << "stage =====";
148 qDebug() << "<BEGIN"; 158 qDebug() << "<BEGIN";
@@ -157,9 +167,7 @@ class CascadeClassifier : public Classifier @@ -157,9 +167,7 @@ class CascadeClassifier : public Classifier
157 return; 167 return;
158 } 168 }
159 169
160 - Classifier *next_stage = Classifier::make(stageDescription, NULL);  
161 - next_stage->train(trainingImages, trainingLabels);  
162 - stages.append(next_stage); 170 + stages[i]->train(trainingImages, trainingLabels);
163 171
164 qDebug() << "END>"; 172 qDebug() << "END>";
165 } 173 }
@@ -193,7 +201,7 @@ class CascadeClassifier : public Classifier @@ -193,7 +201,7 @@ class CascadeClassifier : public Classifier
193 return stages.first()->preprocess(image); 201 return stages.first()->preprocess(image);
194 } 202 }
195 203
196 - Size windowSize(int *dx, int *dy) const 204 + Size windowSize(int *dx = NULL, int *dy = NULL) const
197 { 205 {
198 return stages.first()->windowSize(dx, dy); 206 return stages.first()->windowSize(dx, dy);
199 } 207 }