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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.at
//
// Implementation file
//
// Copyright (c) 1997-2000 Mark Danks.
// Copyright (c) Günther Geiger.
// Copyright (c) 2001-2002 IOhannes m zmoelnig. forum::für::umläute. IEM
// Copyright (c) 2002 James Tittle & Chris Clepper
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
#include "pix_opencv_contours_boundingrect.h"
#include <stdio.h>
CPPEXTERN_NEW(pix_opencv_contours_boundingrect)
/////////////////////////////////////////////////////////
//
// pix_opencv_contours_boundingrect
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
pix_opencv_contours_boundingrect :: pix_opencv_contours_boundingrect()
{
inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("minarea"));
inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("maxarea"));
m_dataout = outlet_new(this->x_obj, 0);
m_countout = outlet_new(this->x_obj, 0);
minarea = 1;
maxarea = 320*240;
comp_xsize = 0;
comp_ysize = 0;
orig = NULL;
gray = NULL;
cnt_img = NULL;
rgb = NULL;
x_ftolerance = 5;
x_mmove = 10;
x_cmode = CV_RETR_LIST;
x_cmethod = CV_CHAIN_APPROX_SIMPLE;
// initialize font
cvInitFont( &font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0, 1.0, 1, 8 );
}
/////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
pix_opencv_contours_boundingrect :: ~pix_opencv_contours_boundingrect()
{
//Destroy cv_images to clean memory
cvReleaseImage(&orig);
cvReleaseImage(&gray);
cvReleaseImage(&cnt_img);
cvReleaseImage(&rgb);
}
/////////////////////////////////////////////////////////
// Mark a contour
//
/////////////////////////////////////////////////////////
int pix_opencv_contours_boundingrect :: mark(float fx, float fy )
{
int i;
if ( ( fx < 0.0 ) || ( fx > this->comp_xsize ) || ( fy < 0 ) || ( fy > this->comp_ysize ) )
{
return -1;
}
for ( i=0; i<MAX_MARKERS; i++)
{
if ( x_xmark[i] == -1 )
{
x_xmark[i] = (int)fx;
x_ymark[i] = (int)fy;
x_found[i] = x_ftolerance;
return i;
}
}
// post( "pix_opencv_contours_boundingrect : max markers reached" );
return -1;
}
/////////////////////////////////////////////////////////
// processImage
//
/////////////////////////////////////////////////////////
void pix_opencv_contours_boundingrect :: processRGBAImage(imageStruct &image)
{
unsigned char *pixels = image.data;
char tindex[4];
int im = 0; // Indicator of markers.
if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!orig)) {
this->comp_xsize = image.xsize;
this->comp_ysize = image.ysize;
//Destroy cv_images to clean memory
cvReleaseImage(&orig);
cvReleaseImage(&gray);
cvReleaseImage(&cnt_img);
cvReleaseImage(&rgb);
//create the orig image with new size
orig = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4);
// Create the output images with new sizes
rgb = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 3);
gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1);
cnt_img = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1);
}
// Here we make a copy of the pixel data from image to orig->imageData
// orig is a IplImage struct, the default image type in openCV, take a look on the IplImage data structure here
// http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html
memcpy( orig->imageData, image.data, image.xsize*image.ysize*4 );
// Convert to grayscale
cvCvtColor(orig, gray, CV_RGBA2GRAY);
CvSeq* contours;
CvMemStorage* stor02;
stor02 = cvCreateMemStorage(0);
cvFindContours( gray, stor02, &contours, sizeof(CvContour), x_cmode, x_cmethod, cvPoint(0,0) );
if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 );
for ( im=0; im<MAX_MARKERS; im++ )
{
if ( x_xmark[im] != -1.0 )
{
x_found[im]--;
}
}
int i = 0; // Indicator of cycles.
for( ; contours != 0; contours = contours->h_next )
{
int count = contours->total; // This is number point in contour
CvRect rect;
int oi, found;
rect = cvContourBoundingRect( contours, 1);
if ( ( (rect.width*rect.height) > minarea ) && ( (rect.width*rect.height) < maxarea ) ) {
found = 0;
oi = -1;
for ( im=0; im<MAX_MARKERS; im++ )
{
// check if the object is already known
if ( ( abs( rect.x - x_xmark[im] ) < x_mmove ) && ( abs( rect.y - x_ymark[im] ) < x_mmove ) )
{
oi=im;
found=1;
x_found[im] = x_ftolerance;
x_xmark[im] = rect.x;
x_ymark[im] = rect.y;
break;
}
}
// new object detected
if ( !found )
{
oi = this->mark(rect.x, rect.y );
}
cvRectangle( orig, cvPoint(rect.x,rect.y), cvPoint(rect.x+rect.width,rect.y+rect.height), CV_RGB(255,0,0), 2, 8 , 0 );
sprintf( tindex, "%d", oi );
cvPutText( orig, tindex, cvPoint(rect.x,rect.y), &font, CV_RGB(255,255,255));
t_atom rlist[4];
SETFLOAT(&rlist[0], oi);
SETFLOAT(&rlist[1], rect.x);
SETFLOAT(&rlist[2], rect.y);
SETFLOAT(&rlist[3], rect.width);
SETFLOAT(&rlist[4], rect.height);
outlet_list( m_dataout, 0, 5, rlist );
i++;
}
outlet_float( m_countout, i );
}
// delete lost objects
for ( im=0; im<MAX_MARKERS; im++ )
{
if ( x_found[im] < 0 )
{
x_xmark[im] = -1.0;
x_ymark[im] = -1,0;
x_found[im] = x_ftolerance;
}
}
cvReleaseMemStorage( &stor02 );
//copy back the processed frame to image
memcpy( image.data, orig->imageData, image.xsize*image.ysize*4 );
}
void pix_opencv_contours_boundingrect :: processRGBImage(imageStruct &image)
{
unsigned char *pixels = image.data;
char tindex[4];
int im = 0; // Indicator of markers.
if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgb)) {
this->comp_xsize = image.xsize;
this->comp_ysize = image.ysize;
//Destroy cv_images to clean memory
cvReleaseImage(&orig);
cvReleaseImage(&gray);
cvReleaseImage(&cnt_img);
cvReleaseImage(&rgb);
//create the orig image with new size
rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);
gray = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 1);
cnt_img = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 1);
}
// FEM UNA COPIA DEL PACKET A image->imageData ... http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html aqui veiem la estructura de IplImage
memcpy( rgb->imageData, image.data, image.xsize*image.ysize*3 );
// Convert to grayscale
cvCvtColor(rgb, gray, CV_RGB2GRAY);
CvSeq* contours;
CvMemStorage* stor02;
stor02 = cvCreateMemStorage(0);
cvFindContours( gray, stor02, &contours, sizeof(CvContour), x_cmode, x_cmethod, cvPoint(0,0) );
if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 );
for ( im=0; im<MAX_MARKERS; im++ )
{
if ( x_xmark[im] != -1.0 )
{
x_found[im]--;
}
}
int i = 0; // Indicator of cycles.
for( ; contours != 0; contours = contours->h_next )
{
int count = contours->total; // This is number point in contour
CvRect rect;
int oi, found;
rect = cvContourBoundingRect( contours, 1);
if ( ( (rect.width*rect.height) > minarea ) && ( (rect.width*rect.height) < maxarea ) ) {
found = 0;
oi = -1;
for ( im=0; im<MAX_MARKERS; im++ )
{
// check if the object is already known
if ( ( abs( rect.x - x_xmark[im] ) < x_mmove ) && ( abs( rect.y - x_ymark[im] ) < x_mmove ) )
{
oi=im;
found=1;
x_found[im] = x_ftolerance;
x_xmark[im] = rect.x;
x_ymark[im] = rect.y;
break;
}
}
// new object detected
if ( !found )
{
oi = this->mark(rect.x, rect.y );
}
cvRectangle( rgb, cvPoint(rect.x,rect.y), cvPoint(rect.x+rect.width,rect.y+rect.height), CV_RGB(255,0,0), 2, 8 , 0 );
sprintf( tindex, "%d", oi );
cvPutText( rgb, tindex, cvPoint(rect.x,rect.y), &font, CV_RGB(255,255,255));
t_atom rlist[4];
SETFLOAT(&rlist[0], oi);
SETFLOAT(&rlist[1], rect.x);
SETFLOAT(&rlist[2], rect.y);
SETFLOAT(&rlist[3], rect.width);
SETFLOAT(&rlist[4], rect.height);
outlet_list( m_dataout, 0, 5, rlist );
i++;
}
outlet_float( m_countout, i );
}
// delete lost objects
for ( im=0; im<MAX_MARKERS; im++ )
{
if ( x_found[im] < 0 )
{
x_xmark[im] = -1.0;
x_ymark[im] = -1,0;
x_found[im] = x_ftolerance;
}
}
cvReleaseMemStorage( &stor02 );
memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 );
}
void pix_opencv_contours_boundingrect :: processYUVImage(imageStruct &image)
{
post( "pix_opencv_contours_boundingrect : yuv format not supported" );
}
void pix_opencv_contours_boundingrect :: processGrayImage(imageStruct &image)
{
char tindex[4];
int im = 0; // Indicator of markers.
if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!orig)) {
this->comp_xsize = image.xsize;
this->comp_ysize = image.ysize;
//Destroy cv_images to clean memory
cvReleaseImage(&orig);
cvReleaseImage(&gray);
cvReleaseImage(&cnt_img);
cvReleaseImage(&rgb);
//create the orig image with new size
orig = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4);
// Create the output images with new sizes
rgb = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 3);
gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1);
cnt_img = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1);
}
// Here we make a copy of the pixel data from image to orig->imageData
// orig is a IplImage struct, the default image type in openCV, take a look on the IplImage data structure here
// http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html
memcpy( gray->imageData, image.data, image.xsize*image.ysize );
memcpy( cnt_img->imageData, image.data, image.xsize*image.ysize );
CvSeq* contours;
CvMemStorage* stor02;
stor02 = cvCreateMemStorage(0);
cvFindContours( gray, stor02, &contours, sizeof(CvContour), x_cmode, x_cmethod, cvPoint(0,0) );
if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 );
for ( im=0; im<MAX_MARKERS; im++ )
{
if ( x_xmark[im] != -1.0 )
{
x_found[im]--;
}
}
int i = 0; // Indicator of cycles.
for( ; contours != 0; contours = contours->h_next )
{
int count = contours->total; // This is number point in contour
CvRect rect;
int oi, found;
rect = cvContourBoundingRect( contours, 1);
if ( ( (rect.width*rect.height) > minarea ) && ( (rect.width*rect.height) < maxarea ) ) {
found = 0;
oi = -1;
for ( im=0; im<MAX_MARKERS; im++ )
{
// check if the object is already known
if ( ( abs( rect.x - x_xmark[im] ) < x_mmove ) && ( abs( rect.y - x_ymark[im] ) < x_mmove ) )
{
oi=im;
found=1;
x_found[im] = x_ftolerance;
x_xmark[im] = rect.x;
x_ymark[im] = rect.y;
break;
}
}
// new object detected
if ( !found )
{
oi = this->mark(rect.x, rect.y );
}
cvRectangle( cnt_img, cvPoint(rect.x,rect.y), cvPoint(rect.x+rect.width,rect.y+rect.height), cvScalarAll(255), 2, 8 , 0 );
sprintf( tindex, "%d", oi );
cvPutText( cnt_img, tindex, cvPoint(rect.x,rect.y), &font, cvScalarAll(255));
t_atom rlist[4];
SETFLOAT(&rlist[0], oi);
SETFLOAT(&rlist[1], rect.x);
SETFLOAT(&rlist[2], rect.y);
SETFLOAT(&rlist[3], rect.width);
SETFLOAT(&rlist[4], rect.height);
outlet_list( m_dataout, 0, 5, rlist );
i++;
}
outlet_float( m_countout, i );
}
// delete lost objects
for ( im=0; im<MAX_MARKERS; im++ )
{
if ( x_found[im] < 0 )
{
x_xmark[im] = -1.0;
x_ymark[im] = -1,0;
x_found[im] = x_ftolerance;
}
}
cvReleaseMemStorage( &stor02 );
//copy back the processed frame to image
memcpy( image.data, cnt_img->imageData, image.xsize*image.ysize );
}
/////////////////////////////////////////////////////////
// floatThreshMess
//
/////////////////////////////////////////////////////////
void pix_opencv_contours_boundingrect :: floatMinAreaMess (float minarea)
{
if (minarea>0) this->minarea = (int)minarea;
}
void pix_opencv_contours_boundingrect :: floatMaxAreaMess (float maxarea)
{
if (maxarea>0) this->maxarea = (int)maxarea;
}
void pix_opencv_contours_boundingrect :: floatFToleranceMess (float ftolerance)
{
if ((int)ftolerance>=1) x_ftolerance = (int)ftolerance;
}
void pix_opencv_contours_boundingrect :: floatMMoveMess (float mmove)
{
if ((int)mmove>=1) x_mmove = (int)mmove;
}
void pix_opencv_contours_boundingrect :: floatCModeMess (float cmode)
{
// CV_RETR_EXTERNAL || CV_RETR_LIST || CV_RETR_CCOMP || CV_RETR_TREE
int mode = (int)cmode;
if ( mode == CV_RETR_EXTERNAL )
{
x_cmode = CV_RETR_EXTERNAL;
post( "pix_opencv_contours_boundingrect : mode set to CV_RETR_EXTERNAL" );
}
if ( mode == CV_RETR_LIST )
{
x_cmode = CV_RETR_LIST;
post( "pix_opencv_contours_boundingrect : mode set to CV_RETR_LIST" );
}
if ( mode == CV_RETR_CCOMP )
{
x_cmode = CV_RETR_CCOMP;
post( "pix_opencv_contours_boundingrect : mode set to CV_RETR_CCOMP" );
}
if ( mode == CV_RETR_TREE )
{
x_cmode = CV_RETR_TREE;
post( "pix_opencv_contours_boundingrect : mode set to CV_RETR_TREE" );
}
}
void pix_opencv_contours_boundingrect :: floatCMethodMess (float cmethod)
{
int method = (int)cmethod;
// CV_CHAIN_CODE || CV_CHAIN_APPROX_NONE || CV_CHAIN_APPROX_SIMPLE || CV_CHAIN_APPROX_TC89_L1 || CV_CHAIN_APPROX_TC89_KCOS || CV_LINK_RUNS
if ( method == CV_CHAIN_CODE )
{
post( "pix_opencv_contours_boundingrect : not supported method : CV_CHAIN_CODE" );
}
if ( method == CV_CHAIN_APPROX_NONE )
{
x_cmethod = CV_CHAIN_APPROX_NONE;
post( "pix_opencv_contours_boundingrect : method set to CV_CHAIN_APPROX_NONE" );
}
if ( method == CV_CHAIN_APPROX_SIMPLE )
{
x_cmethod = CV_CHAIN_APPROX_SIMPLE;
post( "pix_opencv_contours_boundingrect : method set to CV_CHAIN_APPROX_SIMPLE" );
}
if ( method == CV_CHAIN_APPROX_TC89_L1 )
{
x_cmethod = CV_CHAIN_APPROX_TC89_L1;
post( "pix_opencv_contours_boundingrect : method set to CV_CHAIN_APPROX_TC89_L1" );
}
if ( method == CV_CHAIN_APPROX_TC89_KCOS )
{
x_cmethod = CV_CHAIN_APPROX_TC89_KCOS;
post( "pix_opencv_contours_boundingrect : method set to CV_CHAIN_APPROX_TC89_KCOS" );
}
if ( ( method == CV_LINK_RUNS ) && ( x_cmode == CV_RETR_LIST ) )
{
x_cmethod = CV_LINK_RUNS;
post( "pix_opencv_contours_boundingrect : method set to CV_LINK_RUNS" );
}
}
void pix_opencv_contours_boundingrect :: deleteMark(t_floatarg findex )
{
int i;
if ( ( findex < 1.0 ) || ( findex > MAX_MARKERS ) )
{
return;
}
x_xmark[(int)findex-1] = -1;
x_ymark[(int)findex-1] = -1;
}
void pix_opencv_contours_boundingrect :: floatClearMess (void)
{
int i;
for ( i=0; i<MAX_MARKERS; i++)
{
x_xmark[i] = -1;
x_ymark[i] = -1;
x_found[i] = x_ftolerance;
}
}
/////////////////////////////////////////////////////////
// static member function
//
/////////////////////////////////////////////////////////
void pix_opencv_contours_boundingrect :: obj_setupCallback(t_class *classPtr)
{
class_addmethod(classPtr, (t_method)&pix_opencv_contours_boundingrect::floatMinAreaMessCallback,
gensym("minarea"), A_FLOAT, A_NULL);
class_addmethod(classPtr, (t_method)&pix_opencv_contours_boundingrect::floatMaxAreaMessCallback,
gensym("maxarea"), A_FLOAT, A_NULL);
class_addmethod(classPtr, (t_method)&pix_opencv_contours_boundingrect::floatFToleranceMessCallback,
gensym("ftolerance"), A_FLOAT, A_NULL);
class_addmethod(classPtr, (t_method)&pix_opencv_contours_boundingrect::floatMMoveMessCallback,
gensym("mmove"), A_FLOAT, A_NULL);
class_addmethod(classPtr, (t_method)&pix_opencv_contours_boundingrect::floatCModeMessCallback,
gensym("cmode"), A_FLOAT, A_NULL);
class_addmethod(classPtr, (t_method)&pix_opencv_contours_boundingrect::floatCMethodMessCallback,
gensym("cmethod"), A_FLOAT, A_NULL);
class_addmethod(classPtr, (t_method)&pix_opencv_contours_boundingrect::floatClearMessCallback,
gensym("clear"), A_FLOAT, A_NULL);
}
void pix_opencv_contours_boundingrect :: floatMaxAreaMessCallback(void *data, t_floatarg maxarea)
{
GetMyClass(data)->floatMaxAreaMess((float)maxarea);
}
void pix_opencv_contours_boundingrect :: floatMinAreaMessCallback(void *data, t_floatarg minarea)
{
GetMyClass(data)->floatMinAreaMess((float)minarea);
}
void pix_opencv_contours_boundingrect :: floatFToleranceMessCallback(void *data, t_floatarg ftolerance)
{
GetMyClass(data)->floatFToleranceMess((float)ftolerance);
}
void pix_opencv_contours_boundingrect :: floatMMoveMessCallback(void *data, t_floatarg mmove)
{
GetMyClass(data)->floatMMoveMess((float)mmove);
}
void pix_opencv_contours_boundingrect :: floatCModeMessCallback(void *data, t_floatarg cmode)
{
GetMyClass(data)->floatCModeMess((float)cmode);
}
void pix_opencv_contours_boundingrect :: floatCMethodMessCallback(void *data, t_floatarg cmethod)
{
GetMyClass(data)->floatCMethodMess((float)cmethod);
}
void pix_opencv_contours_boundingrect :: floatClearMessCallback(void *data)
{
GetMyClass(data)->floatClearMess();
}
|