forked from openframeworks/openFrameworks
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathofxCvHaarFinder.cpp
More file actions
343 lines (271 loc) · 8.83 KB
/
ofxCvHaarFinder.cpp
File metadata and controls
343 lines (271 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "ofxCvHaarFinder.h"
//--------------------------------------------------------------------------------
static bool sort_carea_compare( const ofxCvBlob & a, const ofxCvBlob & b) {
// use opencv to calc size, then sort based on size
float areaa = fabs(a.area);
float areab = fabs(b.area);
//return 0;
return (areaa > areab);
}
ofxCvHaarFinder::ofxCvHaarFinder() {
#ifdef USE_OLD_CV
cascade = NULL;
#endif
scaleHaar = 1.08;
neighbors = 2;
img.setUseTexture(false);
}
ofxCvHaarFinder::ofxCvHaarFinder(const ofxCvHaarFinder& finder) {
#ifdef USE_OLD_CV
cascade = NULL;
#endif
scaleHaar = finder.scaleHaar;
neighbors = finder.neighbors;
img.setUseTexture(false);
setup(finder.haarFile);
}
ofxCvHaarFinder::~ofxCvHaarFinder() {
#ifdef USE_OLD_CV
if(cascade != NULL)
cvReleaseHaarClassifierCascade(&cascade);
#endif
}
// low values - more accurate - eg: 1.01
// high values - faster - eg: 1.06 or 1.09
void ofxCvHaarFinder::setScaleHaar(float scaleHaar) {
this->scaleHaar = scaleHaar;
}
//low values = more false postives
//high values = less faces, but more reliable
void ofxCvHaarFinder::setNeighbors(unsigned neighbors) {
this->neighbors = neighbors;
}
// FIXME: convert to of::filesystem::path
void ofxCvHaarFinder::setup(std::string haarFile) {
this->haarFile = haarFile;
haarFile = ofToDataPath(haarFile);
#ifdef USE_OLD_CV
if(cascade != NULL)
cvReleaseHaarClassifierCascade(&cascade);
cascade = (CvHaarClassifierCascade*) cvLoad(haarFile.c_str(), 0, 0, 0);
#ifdef HAAR_HACK
// http://thread.gmane.org/gmane.comp.lib.opencv/16540/focus=17400
// http://www.openframeworks.cc/forum/viewtopic.php?f=10&t=1853&hilit=haar
ofxCvGrayscaleImage hack;
hack.setUseTexture(false);
hack.allocate(8, 8);
CvMemStorage* storage = cvCreateMemStorage();
cvHaarDetectObjects(hack.getCvImage(), cascade, storage, scaleHaar, 2, CV_HAAR_DO_CANNY_PRUNING);
cvReleaseMemStorage(&storage);
#endif
if (!cascade)
ofLogError("ofxCvHaarFinder") << "setup(): couldn't load Haar cascade file: \"" << haarFile << "\"";
#else
cascade.load( haarFile );
if( cascade.empty() )
ofLogError("ofxCvHaarFinder") << "setup(): couldn't load Haar cascade file: \"" << haarFile << "\"";
#endif //USE_OLD_CV
}
float ofxCvHaarFinder::getWidth() {
return img.width;
}
float ofxCvHaarFinder::getHeight() {
return img.height;
}
int ofxCvHaarFinder::findHaarObjects(ofImage& input, int minWidth, int minHeight) {
ofxCvGrayscaleImage gray;
gray.setUseTexture(false);
gray.allocate(input.getWidth(), input.getHeight());
if( input.getImageType() == OF_IMAGE_COLOR ){
ofxCvColorImage color;
color.setUseTexture(false);
color.allocate(input.getWidth(), input.getHeight());
color = input.getPixels();
gray = color;
}else if( input.getImageType() == OF_IMAGE_GRAYSCALE ){
gray = input.getPixels();
}else{
ofLogError("ofxCvHaarFinder") << "findHaarObjects(): OF_IMAGE_RGBA image type not supported";
return 0;
}
return findHaarObjects(gray, minWidth, minHeight);
}
int ofxCvHaarFinder::findHaarObjects(ofPixels& input, int minWidth, int minHeight){
ofxCvGrayscaleImage gray;
gray.setUseTexture(false);
gray.allocate(input.getWidth(), input.getHeight());
if( input.getImageType() == OF_IMAGE_COLOR ){
ofxCvColorImage color;
color.setUseTexture(false);
color.allocate(input.getWidth(), input.getHeight());
color.setFromPixels(input);
gray = color;
}else if( input.getImageType() == OF_IMAGE_GRAYSCALE ){
gray.setFromPixels(input);
}else{
ofLogError("ofxCvHaarFinder") << "findHaarObjects(): OF_IMAGE_RGBA image type not supported";
return 0;
}
return findHaarObjects(gray, minWidth, minHeight);
}
int ofxCvHaarFinder::findHaarObjects(const ofxCvGrayscaleImage& input,
int minWidth, int minHeight) {
return findHaarObjects(
input, 0, 0, input.width, input.height,
minWidth, minHeight);
}
int ofxCvHaarFinder::findHaarObjects(const ofxCvGrayscaleImage& input,
ofRectangle& roi,
int minWidth, int minHeight) {
return findHaarObjects(
input, (int) roi.x, (int) roi.y, (int) roi.width, (int) roi.height,
minWidth, minHeight);
}
void ofxCvHaarFinder::draw( float x, float y ) {
ofPushStyle();
ofEnableAlphaBlending();
ofSetColor( 255,0,200,100 );
ofPushMatrix();
ofTranslate( x, y, 0.0 );
ofNoFill();
for(unsigned int i=0; i<blobs.size(); i++ ) {
ofDrawRectangle(blobs[i].boundingRect.x, blobs[i].boundingRect.y,
blobs[i].boundingRect.width, blobs[i].boundingRect.height );
}
ofPopMatrix();
ofPopStyle();
}
int ofxCvHaarFinder::findHaarObjects(const ofxCvGrayscaleImage& input,
int x, int y, int w, int h,
int minWidth, int minHeight) {
int nHaarResults = 0;
#ifdef USE_OLD_CV
if (cascade) {
if (!blobs.empty())
blobs.clear();
// we make a copy of the input image here
// because we need to equalize it.
if (img.width == input.width && img.height == input.height) {
img.resetROI();
img = input;
} else {
img.clear();
img.allocate(input.width, input.height);
img = input;
}
img.setROI(x, y, w, h);
cvEqualizeHist(img.getCvImage(), img.getCvImage());
CvMemStorage* storage = cvCreateMemStorage();
/*
Alternative modes:
CV_HAAR_DO_CANNY_PRUNING
Regions without edges are ignored.
CV_HAAR_SCALE_IMAGE
Scale the image rather than the detector
(sometimes yields speed increases).
CV_HAAR_FIND_BIGGEST_OBJECT
Only return the largest result.
CV_HAAR_DO_ROUGH_SEARCH
When BIGGEST_OBJECT is enabled, stop at
the first scale for which multiple results
are found.
*/
CvSeq* haarResults = cvHaarDetectObjects(
img.getCvImage(), cascade, storage, scaleHaar, neighbors, CV_HAAR_DO_CANNY_PRUNING,
cvSize(minWidth, minHeight));
nHaarResults = haarResults->total;
for (int i = 0; i < nHaarResults; i++ ) {
//ofLogNotice("ofxCvHaarFinder") << "findHaarObjects(): " << i << " objects";
ofxCvBlob blob;
CvRect* r = (CvRect*) cvGetSeqElem(haarResults, i);
float area = r->width * r->height;
float length = (r->width * 2) + (r->height * 2);
float centerx = (r->x) + (r->width / 2.0);
float centery = (r->y) + (r->height / 2.0);
blob.area = fabs(area);
blob.hole = area < 0 ? true : false;
blob.length = length;
blob.boundingRect.x = r->x + x;
blob.boundingRect.y = r->y + y;
blob.boundingRect.width = r->width;
blob.boundingRect.height = r->height;
blob.centroid.x = centerx;
blob.centroid.y = centery;
blob.pts.push_back(ofPoint(r->x, r->y));
blob.pts.push_back(ofPoint(r->x + r->width, r->y));
blob.pts.push_back(ofPoint(r->x + r->width, r->y + r->height));
blob.pts.push_back(ofPoint(r->x, r->y + r->height));
blobs.push_back(blob);
}
// sort the pointers based on size
if( blobs.size() > 1 ) {
sort( blobs.begin(), blobs.end(), sort_carea_compare );
}
cvReleaseMemStorage(&storage);
}
#else
if (!blobs.empty()){
blobs.clear();
}
if( cascade.empty() )
return 0;
// we make a copy of the input image here
// because we need to equalize it.
if (img.width == input.width && img.height == input.height) {
img.resetROI();
img = input;
} else {
img.clear();
img.allocate(input.width, input.height);
img = input;
}
img.setROI(x, y, w, h);
cvEqualizeHist(img.getCvImage(), img.getCvImage());
/*
Alternative modes:
cv::CASCADE_DO_CANNY_PRUNING
Regions without edges are ignored.
cv::CASCADE_SCALE_IMAGE
Scale the image rather than the detector
(sometimes yields speed increases).
cv::CASCADE_FIND_BIGGEST_OBJECT
Only return the largest result.
cv::CASCADE_DO_ROUGH_SEARCH
When BIGGEST_OBJECT is enabled, stop at
the first scale for which multiple results
are found.
*/
std::vector<cv::Rect> haarResults;
cascade.detectMultiScale(cv::cvarrToMat(img.getCvImage()), haarResults, scaleHaar, neighbors, cv::CASCADE_DO_CANNY_PRUNING,
cv::Size(minWidth, minHeight) );
nHaarResults = haarResults.size();
for (int i = 0; i < nHaarResults; i++ ) {
//ofLogNotice("ofxCvHaarFinder") << "findHaarObjects(): " << i << " objects";
ofxCvBlob blob;
cv::Rect r = haarResults[i];
float area = r.width * r.height;
float length = (r.width * 2) + (r.height * 2);
float centerx = (r.x) + (r.width / 2.0);
float centery = (r.y) + (r.height / 2.0);
blob.area = fabs(area);
blob.hole = area < 0 ? true : false;
blob.length = length;
blob.boundingRect.x = r.x + x;
blob.boundingRect.y = r.y + y;
blob.boundingRect.width = r.width;
blob.boundingRect.height = r.height;
blob.centroid.x = centerx;
blob.centroid.y = centery;
blob.pts.push_back(ofPoint(r.x, r.y));
blob.pts.push_back(ofPoint(r.x + r.width, r.y));
blob.pts.push_back(ofPoint(r.x + r.width, r.y + r.height));
blob.pts.push_back(ofPoint(r.x, r.y + r.height));
blobs.push_back(blob);
}
// sort the pointers based on size
if( blobs.size() > 1 ) {
sort( blobs.begin(), blobs.end(), sort_carea_compare );
}
#endif // USE_OLD_CV
return nHaarResults;
}