From da38513babcad1e84866d30cd4b2cddb27a36eff Mon Sep 17 00:00:00 2001 From: "N.N." Date: Sat, 29 Aug 2009 23:07:24 +0000 Subject: added pix_opencv_floodfill svn path=/trunk/externals/pix_opencv/; revision=12143 --- Makefile | 2 +- pix_opencv_floodfill-help.pd | 156 ++++++++++++++++ pix_opencv_floodfill.cc | 427 +++++++++++++++++++++++++++++++++++++++++++ pix_opencv_floodfill.h | 111 +++++++++++ 4 files changed, 695 insertions(+), 1 deletion(-) create mode 100755 pix_opencv_floodfill-help.pd create mode 100755 pix_opencv_floodfill.cc create mode 100755 pix_opencv_floodfill.h diff --git a/Makefile b/Makefile index 1deb95f..24c99ae 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ endif .SUFFIXES = $(EXTENSION) -SOURCES = pix_opencv_edge.cc pix_opencv_laplace.cc pix_opencv_morphology.cc pix_opencv_distrans.cc pix_opencv_motempl.cc pix_opencv_haarcascade.cc pix_opencv_contours_boundingrect.cc pix_opencv_bgsubstract.cc pix_opencv_contours_convexity.cc pix_opencv_dft.cc pix_opencv_lk.cc pix_opencv_hist_compare.cc pix_opencv_knear.cc pix_opencv_threshold.cc +SOURCES = pix_opencv_edge.cc pix_opencv_laplace.cc pix_opencv_morphology.cc pix_opencv_distrans.cc pix_opencv_motempl.cc pix_opencv_haarcascade.cc pix_opencv_contours_boundingrect.cc pix_opencv_bgsubstract.cc pix_opencv_contours_convexity.cc pix_opencv_dft.cc pix_opencv_lk.cc pix_opencv_hist_compare.cc pix_opencv_knear.cc pix_opencv_threshold.cc pix_opencv_floodfill.cc all: $(SOURCES:.cc=.$(EXTENSION)) diff --git a/pix_opencv_floodfill-help.pd b/pix_opencv_floodfill-help.pd new file mode 100755 index 0000000..740b1b1 --- /dev/null +++ b/pix_opencv_floodfill-help.pd @@ -0,0 +1,156 @@ +#N canvas 5 105 1192 685 10; +#X obj 396 -37 gemhead; +#X obj 212 501 pix_texture; +#X obj 212 529 square 2; +#X obj 28 132 cnv 15 220 70 empty empty empty 20 12 0 14 -195568 -66577 +0; +#N canvas 0 22 454 304 gemwin 0; +#X obj 132 136 gemwin; +#X obj 67 89 outlet; +#X obj 67 10 inlet; +#X obj 67 41 route create; +#X msg 67 70 set destroy; +#X msg 182 68 set create; +#X msg 132 112 create \, 1; +#X msg 238 112 destroy; +#N canvas 87 154 247 179 Gem.init 0; +#X obj 118 46 loadbang; +#X msg 118 81 reset; +#X obj 118 113 outlet; +#X connect 0 0 1 0; +#X connect 1 0 2 0; +#X restore 289 80 pd Gem.init; +#X connect 2 0 3 0; +#X connect 3 0 4 0; +#X connect 3 0 6 0; +#X connect 3 1 5 0; +#X connect 3 1 7 0; +#X connect 4 0 1 0; +#X connect 5 0 1 0; +#X connect 6 0 0 0; +#X connect 7 0 0 0; +#X connect 8 0 0 0; +#X restore 35 178 pd gemwin; +#X msg 35 154 create; +#X text 36 136 Create window and render; +#X obj 213 404 pix_texture; +#X obj 213 432 square 2; +#X obj 325 71 translateXYZ -2 0 0; +#X obj 326 100 separator; +#X obj 340 181 cnv 15 600 350 empty empty empty 20 12 0 14 -24198 -66577 +0; +#X obj 511 -66 bng 25 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 +-1; +#X obj 524 128 unpack 0 0 0; +#X floatatom 514 151 5 0 0 3 length - -; +#X floatatom 563 151 5 0 0 3 width - -; +#X floatatom 613 151 5 0 0 3 height - -; +#X obj 568 109 bng 15 250 50 0 empty empty end_reached 20 7 0 10 -262144 +-1 -1; +#X floatatom 550 74 5 0 10000 1 frame# - -; +#X obj 511 -37 openpanel; +#X msg 511 -17 open \$1; +#X obj 499 92 pix_film; +#X msg 517 26 auto \$1; +#X obj 517 8 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1 +; +#X obj 326 126 translateXYZ 4 0 0; +#X obj 580 26 loadbang; +#X msg 528 50 colorspace RGBA; +#X obj 771 5 gemmouse; +#X obj 827 112 f; +#X obj 797 111 f; +#X obj 841 83 t b b; +#X obj 797 139 pack f f; +#X obj 817 33 route 1; +#X msg 838 58 bang; +#X obj 367 424 pix_opencv_floodfill; +#X text -38 44 used in tracking algorithms as an object selector; +#X text -38 71 and Yves Degoyon ( ydegoyon@gmail.com ); +#X text -39 21 pdp_opencv_floodfill : mark image components with a +specific color; +#X text -38 57 Written by Lluis Gomez i Bigorda ( lluisgomez@hangar.org +); +#X floatatom 458 376 5 0 0 0 - - -; +#X floatatom 466 397 5 0 0 0 - - -; +#X msg 402 207 mark \$1 \$2; +#X msg 449 356 color \$1; +#X obj 511 357 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X text 537 355 activate color mode ( default : on ); +#X msg 433 282 fillcolor 1 255 0 123; +#X text 571 283 fillcolor of nth component ( 1 < n < 10 ); +#X text 477 193 mark %x %y : mark a component; +#X text 477 205 ( 0 <= %x <= 1 ); +#X text 478 216 ( 0 <= %y <= 1 ); +#X msg 417 235 delete 1; +#X msg 431 257 clear; +#X text 477 235 delete a component ( 1 <= index <= 10 ); +#X text 480 257 delete all components; +#X text 531 318 set connectivity ( default 4 ); +#X text 500 376 lower tolerance ( default 20 ); +#X text 506 396 upper tolerance ( default 20 ); +#X obj 484 452 route 0 1 2 3 4 5 6 7 8 9; +#X floatatom 485 497 5 0 0 0 - - -; +#X floatatom 521 498 5 0 0 0 - - -; +#X floatatom 557 498 5 0 0 0 - - -; +#X floatatom 593 498 5 0 0 0 - - -; +#X obj 485 475 unpack f f f f; +#X text 487 514 X; +#X text 524 514 Y; +#X text 556 515 Width; +#X text 594 515 Height; +#X text 643 453 detected components; +#X msg 437 307 connectivity 4; +#X msg 439 330 connectivity 8; +#X connect 0 0 21 0; +#X connect 1 0 2 0; +#X connect 4 0 5 0; +#X connect 5 0 4 0; +#X connect 7 0 8 0; +#X connect 9 0 7 0; +#X connect 9 0 10 0; +#X connect 10 0 24 0; +#X connect 12 0 19 0; +#X connect 13 0 14 0; +#X connect 13 1 15 0; +#X connect 13 2 16 0; +#X connect 17 0 18 0; +#X connect 18 0 21 1; +#X connect 19 0 20 0; +#X connect 20 0 21 0; +#X connect 21 0 9 0; +#X connect 21 1 13 0; +#X connect 21 2 17 0; +#X connect 22 0 21 0; +#X connect 23 0 22 0; +#X connect 24 0 34 0; +#X connect 25 0 26 0; +#X connect 26 0 21 0; +#X connect 27 0 29 1; +#X connect 27 1 28 1; +#X connect 27 2 32 0; +#X connect 28 0 31 1; +#X connect 29 0 31 0; +#X connect 30 0 29 0; +#X connect 30 1 28 0; +#X connect 31 0 41 0; +#X connect 32 0 33 0; +#X connect 33 0 30 0; +#X connect 34 0 1 0; +#X connect 34 1 57 0; +#X connect 39 0 34 1; +#X connect 40 0 34 2; +#X connect 41 0 34 0; +#X connect 42 0 34 0; +#X connect 43 0 42 0; +#X connect 45 0 34 0; +#X connect 50 0 34 0; +#X connect 51 0 34 0; +#X connect 57 0 62 0; +#X connect 62 0 58 0; +#X connect 62 1 59 0; +#X connect 62 2 60 0; +#X connect 62 3 61 0; +#X connect 68 0 34 0; +#X connect 69 0 34 0; diff --git a/pix_opencv_floodfill.cc b/pix_opencv_floodfill.cc new file mode 100755 index 0000000..3b492c2 --- /dev/null +++ b/pix_opencv_floodfill.cc @@ -0,0 +1,427 @@ + +// +// 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_floodfill.h" +#include + +CPPEXTERN_NEW(pix_opencv_floodfill) + +///////////////////////////////////////////////////////// +// +// pix_opencv_floodfill +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// + +pix_opencv_floodfill :: pix_opencv_floodfill() +{ + int i; + + comp_xsize=320; + comp_ysize=240; + + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("lo_diff")); + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("up_diff")); + m_dataout = outlet_new(this->x_obj, &s_anything); + + x_lo = 20; + x_up = 20; + x_connectivity = 4; + x_color = 1; + + for ( i=0; icomp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) + { + + this->comp_xsize=image.xsize; + this->comp_ysize=image.ysize; + + rgba = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 4 ); + rgb = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 3 ); + grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + } + + memcpy( rgba->imageData, image.data, image.xsize*image.ysize*4 ); + + if ( !x_color ) + { + cvCvtColor(rgba, grey, CV_BGRA2GRAY); + } + + // mark recognized components + for ( i=0; iimageData, image.xsize*image.ysize*4 ); +} + +void pix_opencv_floodfill :: processRGBImage(imageStruct &image) +{ + int i, k; + int im; + int marked; + CvConnectedComp comp; + int flags = x_connectivity + ( 255 << 8 ) + CV_FLOODFILL_FIXED_RANGE; + + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) + { + + this->comp_xsize=image.xsize; + this->comp_ysize=image.ysize; + + rgba = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 4 ); + rgb = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 3 ); + grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + } + + memcpy( rgb->imageData, image.data, image.xsize*image.ysize*3 ); + + if ( !x_color ) + { + cvCvtColor(rgb, grey, CV_BGR2GRAY); + } + + // mark recognized components + for ( i=0; iimageData, image.xsize*image.ysize*3 ); +} + +void pix_opencv_floodfill :: processYUVImage(imageStruct &image) +{ + post( "pix_opencv_floodfill : yuv format not supported" ); +} + +void pix_opencv_floodfill :: processGrayImage(imageStruct &image) +{ + int i, k; + int im; + int marked; + CvConnectedComp comp; + int flags = x_connectivity + ( 255 << 8 ) + CV_FLOODFILL_FIXED_RANGE; + + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) + { + + this->comp_xsize=image.xsize; + this->comp_ysize=image.ysize; + + rgba = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 4 ); + rgb = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 3 ); + grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + } + + memcpy( grey->imageData, image.data, image.xsize*image.ysize ); + + // mark recognized components + for ( i=0; iimageData, image.xsize*image.ysize ); +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// + +void pix_opencv_floodfill :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_floodfill::colorMessCallback, + gensym("color"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_floodfill::fillcolorMessCallback, + gensym("fillcolor"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_floodfill::connectivityMessCallback, + gensym("connectivity"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_floodfill::markMessCallback, + gensym("mark"), A_FLOAT, A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_floodfill::deleteMessCallback, + gensym("delete"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_floodfill::clearMessCallback, + gensym("clear"), A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_floodfill::updiffMessCallback, + gensym("up_diff"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_floodfill::lodiffMessCallback, + gensym("lo_diff"), A_FLOAT, A_NULL); +} + +void pix_opencv_floodfill :: colorMessCallback(void *data, t_floatarg color) +{ + GetMyClass(data)->colorMess((float)color); +} + +void pix_opencv_floodfill :: fillcolorMessCallback(void *data, t_floatarg index, t_floatarg r, t_floatarg g, t_floatarg b) +{ + GetMyClass(data)->fillcolorMess(index, r, g, b); +} + +void pix_opencv_floodfill :: connectivityMessCallback(void *data, t_floatarg connectivity) +{ + GetMyClass(data)->connectivityMess(connectivity); +} + +void pix_opencv_floodfill :: markMessCallback(void *data, t_floatarg perx, t_floatarg pery) +{ + GetMyClass(data)->markMess(perx, pery); +} + +void pix_opencv_floodfill :: deleteMessCallback(void *data, t_floatarg index) +{ + GetMyClass(data)->deleteMess((float)index); +} + +void pix_opencv_floodfill :: clearMessCallback(void *data) +{ + GetMyClass(data)->clearMess(); +} + +void pix_opencv_floodfill :: updiffMessCallback(void *data, t_floatarg updiff) +{ + GetMyClass(data)->updiffMess((float)updiff); +} + +void pix_opencv_floodfill :: lodiffMessCallback(void *data, t_floatarg lodiff) +{ + GetMyClass(data)->lodiffMess((float)lodiff); +} + +void pix_opencv_floodfill :: colorMess(float color) +{ + if ( ( (int)color == 0 ) || ( (int)color == 1 ) ) + { + x_color = (int)color; + } +} + +void pix_opencv_floodfill :: fillcolorMess(float index, float r, float g, float b) +{ + if ( ( (int)index <= 0 ) || ( (int)index > MAX_COMPONENTS ) ) + { + post( "pix_opencv_floodfill : wrong color index : %d", (int)index ); + return; + } + + if ( ( (int)r >= 0 ) || ( (int)r <= 255 ) ) + { + x_r[(int)index-1] = (int)r; + } + + if ( ( (int)g >= 0 ) || ( (int)g <= 255 ) ) + { + x_g[(int)index-1] = (int)g; + } + + if ( ( (int)b >= 0 ) || ( (int)b <= 255 ) ) + { + x_b[(int)index-1] = (int)b; + } +} + +void pix_opencv_floodfill :: connectivityMess(float connectivity) +{ + if ( ( connectivity != 4.0 ) && ( connectivity != 8.0 ) ) + { + return; + } + + x_connectivity = (int)connectivity; +} + +void pix_opencv_floodfill :: markMess(float fperx, float fpery) +{ + int i; + int inserted; + int px, py; + + if ( ( fperx < 0.0 ) || ( fperx > 1.0 ) || ( fpery < 0.0 ) || ( fpery > 1.0 ) ) + { + return; + } + + px = (int)(fperx*comp_xsize); + py = (int)(fpery*comp_ysize); + inserted = 0; + for ( i=0; i MAX_COMPONENTS ) ) + { + return; + } + + x_xcomp[(int)index-1] = -1; + x_ycomp[(int)index-1] = -1; + +} + +void pix_opencv_floodfill :: clearMess(void) +{ + int i; + + for ( i=0; i= 0 ) && ( (int)updiff <= 255 ) ) + { + x_up = (int)updiff; + } +} + +void pix_opencv_floodfill :: lodiffMess(float lodiff) +{ + if ( ( (int)lodiff >= 0 ) && ( (int)lodiff <= 255 ) ) + { + x_lo = (int)lodiff; + } +} diff --git a/pix_opencv_floodfill.h b/pix_opencv_floodfill.h new file mode 100755 index 0000000..c265f10 --- /dev/null +++ b/pix_opencv_floodfill.h @@ -0,0 +1,111 @@ +/*----------------------------------------------------------------- +LOG + GEM - Graphics Environment for Multimedia + + Change pix to greyscale + + Copyright (c) 1997-1999 Mark Danks. mark@danks.org + Copyright (c) Günther Geiger. geiger@epy.co.at + Copyright (c) 2001-2002 IOhannes m zmoelnig. forum::für::umläute. IEM. zmoelnig@iem.kug.ac.at + 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. + +-----------------------------------------------------------------*/ + +#ifndef INCLUDE_PIX_OPENCV_LK_H_ +#define INCLUDE_PIX_OPENCV_LK_H_ + +#include "Base/GemPixObj.h" + +#ifndef _EiC +#include "cv.h" +#endif + +#define MAX_COMPONENTS 10 + +/*----------------------------------------------------------------- +------------------------------------------------------------------- +CLASS + pix_opencv_floodfill + + Color blobs tracker + +KEYWORDS + pix + +DESCRIPTION + +-----------------------------------------------------------------*/ +class GEM_EXTERN pix_opencv_floodfill : public GemPixObj +{ + CPPEXTERN_HEADER(pix_opencv_floodfill, GemPixObj) + + public: + + ////////// + // Constructor + pix_opencv_floodfill(); + + protected: + + ////////// + // Destructor + virtual ~pix_opencv_floodfill(); + + ////////// + // Do the processing + virtual void processRGBAImage(imageStruct &image); + virtual void processRGBImage(imageStruct &image); + virtual void processYUVImage(imageStruct &image); + virtual void processGrayImage(imageStruct &image); + + void colorMess(float color); + void fillcolorMess(float index, float r, float g, float b); + void connectivityMess(float connectivity); + void markMess(float px, float py); + void deleteMess(float index); + void clearMess(void); + void updiffMess(float updiff); + void lodiffMess(float lodiff); + + int comp_xsize; + int comp_ysize; + + t_outlet *m_dataout; + t_atom x_list[5]; + + int x_up; + int x_lo; + int x_connectivity; + int x_color; + + + private: + + ////////// + // Static member functions + static void colorMessCallback(void *data, float color); + static void fillcolorMessCallback(void *data, float index, float r, float g, float b); + static void connectivityMessCallback(void *data, float connectivity); + static void markMessCallback(void *data, float px, float py); + static void deleteMessCallback(void *data, float index); + static void clearMessCallback(void *data); + static void updiffMessCallback(void *data, float updiff); + static void lodiffMessCallback(void *data, float lodiff); + + // Internal Open CV data + // tracked components + int x_xcomp[MAX_COMPONENTS]; + int x_ycomp[MAX_COMPONENTS]; + + // fill color + int x_r[MAX_COMPONENTS]; + int x_g[MAX_COMPONENTS]; + int x_b[MAX_COMPONENTS]; + + IplImage *rgba, *rgb, *grey; + +}; + +#endif // for header file -- cgit v1.2.1