diff options
author | N.N. <sevyves@users.sourceforge.net> | 2009-09-17 19:54:43 +0000 |
---|---|---|
committer | N.N. <sevyves@users.sourceforge.net> | 2009-09-17 19:54:43 +0000 |
commit | e047e8c34d5eceda971be0729d7dee72062f1a26 (patch) | |
tree | e36fd1cd9ff34bd6d81ba1d3500959e3ed246d48 | |
parent | 292ca6f2aec2ddbd3c771a53a146fde3805071b0 (diff) |
added pdp_opencv_colorfilt
svn path=/trunk/externals/pdp_opencv/; revision=12386
-rw-r--r-- | Makefile | 2 | ||||
-rwxr-xr-x | Makefile.in | 2 | ||||
-rwxr-xr-x | pdp_opencv_colorfilt-help.pd | 111 | ||||
-rwxr-xr-x | pdp_opencv_colorfilt.c | 332 |
4 files changed, 445 insertions, 2 deletions
@@ -26,7 +26,7 @@ endif .SUFFIXES = $(EXTENSION) -SOURCES = pdp_opencv_threshold.c pdp_opencv_edge.c pdp_opencv_distrans.c pdp_opencv_laplace.c pdp_opencv_motempl.c pdp_opencv_morphology.c pdp_opencv_haarcascade.c pdp_opencv_contours_convexity.c pdp_opencv_contours_boundingrect.c pdp_opencv_bgsubstract.c pdp_opencv_lk.c pdp_opencv_floodfill.c pdp_opencv_hist_compare.c pdp_opencv_dft.c pdp_opencv_knear.cc pdp_opencv_hu_moments.c pdp_opencv_hu_compare.cc pdp_opencv_pgh_compare.cc pdp_opencv_bgstats.cc pdp_opencv_athreshold.c pdp_opencv_hough_lines.cc pdp_opencv_channels.cc pdp_opencv_hough_circles.cc pdp_opencv_camshift.cc pdp_opencv_surf.cc +SOURCES = pdp_opencv_threshold.c pdp_opencv_edge.c pdp_opencv_distrans.c pdp_opencv_laplace.c pdp_opencv_motempl.c pdp_opencv_morphology.c pdp_opencv_haarcascade.c pdp_opencv_contours_convexity.c pdp_opencv_contours_boundingrect.c pdp_opencv_bgsubstract.c pdp_opencv_lk.c pdp_opencv_floodfill.c pdp_opencv_hist_compare.c pdp_opencv_dft.c pdp_opencv_knear.cc pdp_opencv_hu_moments.c pdp_opencv_hu_compare.cc pdp_opencv_pgh_compare.cc pdp_opencv_bgstats.cc pdp_opencv_athreshold.c pdp_opencv_hough_lines.cc pdp_opencv_channels.cc pdp_opencv_hough_circles.cc pdp_opencv_camshift.cc pdp_opencv_colorfilt.c SOURCES_OPT = pdp_opencv_surf.cc all: $(SOURCES:.c=.$(EXTENSION)) $(SOURCES:.cc=.$(EXTENSION)) $(SOURCES_OPT:.c=.$(EXTENSION)) $(SOURCES_OPT:.cc=.$(EXTENSION)) diff --git a/Makefile.in b/Makefile.in index 6f47b83..79b1433 100755 --- a/Makefile.in +++ b/Makefile.in @@ -26,7 +26,7 @@ endif .SUFFIXES = $(EXTENSION) -SOURCES = pdp_opencv_threshold.c pdp_opencv_edge.c pdp_opencv_distrans.c pdp_opencv_laplace.c pdp_opencv_motempl.c pdp_opencv_morphology.c pdp_opencv_haarcascade.c pdp_opencv_contours_convexity.c pdp_opencv_contours_boundingrect.c pdp_opencv_bgsubstract.c pdp_opencv_lk.c pdp_opencv_floodfill.c pdp_opencv_hist_compare.c pdp_opencv_dft.c pdp_opencv_knear.cc pdp_opencv_hu_moments.c pdp_opencv_hu_compare.cc pdp_opencv_pgh_compare.cc pdp_opencv_bgstats.cc pdp_opencv_athreshold.c pdp_opencv_hough_lines.cc pdp_opencv_channels.cc pdp_opencv_hough_circles.cc pdp_opencv_camshift.cc pdp_opencv_surf.cc +SOURCES = pdp_opencv_threshold.c pdp_opencv_edge.c pdp_opencv_distrans.c pdp_opencv_laplace.c pdp_opencv_motempl.c pdp_opencv_morphology.c pdp_opencv_haarcascade.c pdp_opencv_contours_convexity.c pdp_opencv_contours_boundingrect.c pdp_opencv_bgsubstract.c pdp_opencv_lk.c pdp_opencv_floodfill.c pdp_opencv_hist_compare.c pdp_opencv_dft.c pdp_opencv_knear.cc pdp_opencv_hu_moments.c pdp_opencv_hu_compare.cc pdp_opencv_pgh_compare.cc pdp_opencv_bgstats.cc pdp_opencv_athreshold.c pdp_opencv_hough_lines.cc pdp_opencv_channels.cc pdp_opencv_hough_circles.cc pdp_opencv_camshift.cc pdp_opencv_colorfilt.c SOURCES_OPT = @SOURCES_OPT@ all: $(SOURCES:.c=.$(EXTENSION)) $(SOURCES:.cc=.$(EXTENSION)) $(SOURCES_OPT:.c=.$(EXTENSION)) $(SOURCES_OPT:.cc=.$(EXTENSION)) diff --git a/pdp_opencv_colorfilt-help.pd b/pdp_opencv_colorfilt-help.pd new file mode 100755 index 0000000..8e60085 --- /dev/null +++ b/pdp_opencv_colorfilt-help.pd @@ -0,0 +1,111 @@ +#N canvas 367 49 810 785 10; +#X obj -15 -31 cnv 15 621 250 empty empty empty 20 12 0 14 -260097 +-66577 0; +#X obj 223 -15 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X obj 223 12 metro 40; +#X msg 313 39 close; +#X obj 243 110 pdp_v4l; +#X msg 300 7 open /dev/video0; +#X obj 412 -20 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 +1; +#X obj 412 7 metro 40; +#X msg 493 26 close; +#X msg 489 2 open /dev/video0; +#X obj 433 106 pdp_v4l2; +#X msg 498 81 format \$1; +#X obj 499 59 hradio 15 1 0 4 empty empty empty 0 -8 0 10 -262144 -1 +-1 0; +#X obj 15 184 pdp_qt; +#X obj 48 120 metro 40; +#X msg 48 92 bang; +#X msg 89 92 stop; +#X obj 15 23 openpanel; +#X msg 15 48 open \$1; +#X msg 15 -5 bang; +#X msg 49 147 loop \$1; +#X obj 129 147 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1 +1; +#X obj 129 65 loadbang; +#X text 58 -2 playing a video file; +#X text 253 -17 Camera input; +#X obj 75 185 hsl 128 15 0 2000 0 0 empty empty empty -2 -8 0 10 -262144 +-1 -1 12700 1; +#X obj -15 222 cnv 15 621 350 empty empty empty 20 12 0 14 -258113 +-66577 0; +#X text -13 -98 can be useful to isolate and track colors; +#X obj 10 583 pdp_xv; +#X obj 8 492 pdp_opencv_colorfilt; +#X obj 244 161 pdp_xv; +#X text -14 -111 pdp_opencv_colrfilt : RGB color filter; +#X text -16 -63 written by Yves Degoyon ( ydegoyon@gmail.com ); +#X text -14 -87 it's very similar to pdp_binary for PiDiP but works +in RGB colorspace; +#X obj 110 384 pdp_colorgrid pdp_colorgrid1 256 0 256 50 0 50 0 1 1 +10 10 149 417; +#X floatatom 108 442 5 0 0 0 - - -; +#X floatatom 229 443 5 0 0 0 - - -; +#X floatatom 342 444 5 0 0 0 - - -; +#X text 374 403 select a color; +#X msg 87 334 pick \$1 \$2; +#X text 160 336 <-- pick a color in the video window; +#X floatatom 66 531 5 0 0 0 - - -; +#X floatatom 113 530 5 0 0 0 - - -; +#X floatatom 159 530 5 0 0 0 - - -; +#X text 68 552 R; +#X text 114 553 G; +#X text 163 550 B; +#X text 214 529 <-- selected color; +#X text 253 491 ( if set to -1 the component is ignored ); +#X text 256 480 RGB components; +#X msg 76 291 tolerance \$1; +#X obj 244 184 route press drag release; +#X msg 290 160 cursor 1; +#X floatatom 166 293 5 0 0 0 - - -; +#X text 215 290 color tolerance ( default : 50 ); +#X connect 1 0 2 0; +#X connect 2 0 4 0; +#X connect 3 0 4 0; +#X connect 4 0 29 0; +#X connect 4 0 30 0; +#X connect 4 0 52 0; +#X connect 5 0 4 0; +#X connect 6 0 7 0; +#X connect 7 0 10 0; +#X connect 8 0 10 0; +#X connect 9 0 10 0; +#X connect 10 0 29 0; +#X connect 10 0 30 0; +#X connect 10 0 52 0; +#X connect 11 0 10 0; +#X connect 12 0 11 0; +#X connect 13 0 30 0; +#X connect 13 0 52 0; +#X connect 13 0 29 0; +#X connect 14 0 13 0; +#X connect 15 0 14 0; +#X connect 16 0 14 0; +#X connect 17 0 18 0; +#X connect 18 0 13 0; +#X connect 19 0 17 0; +#X connect 20 0 13 0; +#X connect 21 0 20 0; +#X connect 22 0 21 0; +#X connect 22 0 15 0; +#X connect 25 0 13 1; +#X connect 29 0 28 0; +#X connect 29 1 41 0; +#X connect 29 2 42 0; +#X connect 29 3 43 0; +#X connect 30 0 51 0; +#X connect 34 0 35 0; +#X connect 34 1 36 0; +#X connect 34 2 37 0; +#X connect 35 0 29 1; +#X connect 36 0 29 2; +#X connect 37 0 29 3; +#X connect 39 0 29 0; +#X connect 50 0 29 0; +#X connect 51 0 39 0; +#X connect 52 0 30 0; +#X connect 53 0 50 0; diff --git a/pdp_opencv_colorfilt.c b/pdp_opencv_colorfilt.c new file mode 100755 index 0000000..b2d4092 --- /dev/null +++ b/pdp_opencv_colorfilt.c @@ -0,0 +1,332 @@ +/* + * Pd OpenCV module + * Copyright (c) by Yves Degoyon ( ydegoyon@free.fr ) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +/* This object is a color filter + */ + +#include "pdp.h" +#include <math.h> +#include <stdio.h> +#include "g_canvas.h" + +static char *pdp_opencv_colorfilt_version = "pdp_opencv_colorfilt: a color filter"; + +typedef struct pdp_opencv_colorfilt_struct +{ + t_object x_obj; + + int x_packet0; + int x_packet1; + int x_queue_id; + int x_dropped; + + int x_width; + int x_height; + int x_size; + unsigned char x_colorR; // RGB components of binary mask + unsigned char x_colorG; + unsigned char x_colorB; + int x_tolerance; // tolerance + unsigned char *x_frame; // keep a copy of current frame for picking color + + t_outlet *x_pdp_output; // output packets + t_outlet *x_R; // output R component of selected color + t_outlet *x_G; // output G component of selected color + t_outlet *x_B; // output B component of selected color + + t_canvas *x_canvas; + +} t_pdp_opencv_colorfilt; + +static void pdp_opencv_colorfilt_draw_color(t_pdp_opencv_colorfilt *x) +{ + int width, height; + char color[32]; + + sprintf( color, "#%.2X%.2X%.2X", x->x_colorR, x->x_colorG, x->x_colorB ); + width = rtext_width( glist_findrtext( (t_glist*)x->x_canvas, (t_text *)x ) ); + height = rtext_height( glist_findrtext( (t_glist*)x->x_canvas, (t_text *)x ) ); + sys_vgui(".x%x.c delete rectangle %xCOLOR\n", x->x_canvas, x ); + sys_vgui(".x%x.c create rectangle %d %d %d %d -fill %s -tags %xCOLOR\n", + x->x_canvas, x->x_obj.te_xpix+width+5, x->x_obj.te_ypix, + x->x_obj.te_xpix+width+height+5, + x->x_obj.te_ypix+height, color, x ); +} + + +static void pdp_opencv_colorfilt_r(t_pdp_opencv_colorfilt *x, t_floatarg fr ) +{ + if ( fr <= 255. && fr >= 0. ) + { + x->x_colorR = (int)fr; + outlet_float( x->x_R, x->x_colorR ); + if (glist_isvisible(x->x_canvas)) pdp_opencv_colorfilt_draw_color( x ); + } +} + +static void pdp_opencv_colorfilt_g(t_pdp_opencv_colorfilt *x, t_floatarg fg ) +{ + if ( fg <= 255. && fg >= 0. ) + { + x->x_colorG = (int)fg; + outlet_float( x->x_G, x->x_colorG ); + if (glist_isvisible(x->x_canvas)) pdp_opencv_colorfilt_draw_color( x ); + } +} + +static void pdp_opencv_colorfilt_b(t_pdp_opencv_colorfilt *x, t_floatarg fb ) +{ + if ( fb < 255 && fb >= 0. ) + { + x->x_colorB = (int)fb; + outlet_float( x->x_B, x->x_colorB ); + if (glist_isvisible(x->x_canvas)) pdp_opencv_colorfilt_draw_color( x ); + } +} + +static void pdp_opencv_colorfilt_tolerance(t_pdp_opencv_colorfilt *x, t_floatarg ftolerance ) +{ + if ( ftolerance >= 0 ) + { + x->x_tolerance = (int)ftolerance; + } +} + +static void pdp_opencv_colorfilt_pick(t_pdp_opencv_colorfilt *x, t_floatarg px, t_floatarg py ) +{ + if ( x->x_frame && ( px >= 0. ) && ( px <= 1.0 ) + && ( py > 0. ) && ( py < 1.0 ) ) + { + int xcur = px*x->x_width; + int ycur = py*x->x_height; + + x->x_colorR = x->x_frame[(ycur*x->x_width*3)+xcur*3]; + x->x_colorG = x->x_frame[(ycur*x->x_width*3)+xcur*3+1]; + x->x_colorB = x->x_frame[(ycur*x->x_width*3)+xcur*3+2]; + outlet_float( x->x_R, x->x_colorR ); + outlet_float( x->x_G, x->x_colorG ); + outlet_float( x->x_B, x->x_colorB ); + post( "pdp_opencv_colorfilt : picked up color : x=%d y=%d r=%d g=%d b=%d", xcur, ycur, x->x_colorR, x->x_colorG, x->x_colorB ); + + if (glist_isvisible(x->x_canvas)) pdp_opencv_colorfilt_draw_color( x ); + } +} + +static void pdp_opencv_colorfilt_allocate(t_pdp_opencv_colorfilt *x) +{ + x->x_frame = (unsigned char *) getbytes ( x->x_size*3*sizeof(unsigned char) ); + + if ( !x->x_frame ) + { + post( "pdp_binary : severe error : cannot allocate buffer !!! "); + return; + } +} + +static void pdp_opencv_colorfilt_free_ressources(t_pdp_opencv_colorfilt *x) +{ + if ( x->x_frame ) freebytes ( x->x_frame, x->x_size*3*sizeof(unsigned char) ); +} + +static void pdp_opencv_colorfilt_process_rgb(t_pdp_opencv_colorfilt *x) +{ + t_pdp *header = pdp_packet_header(x->x_packet0); + unsigned char *data = (unsigned char *)pdp_packet_data(x->x_packet0); + t_pdp *newheader = pdp_packet_header(x->x_packet1); + unsigned char *newdata = (unsigned char *)pdp_packet_data(x->x_packet1); + int i; + int px=0, py=0; + unsigned char r=0, g=0, b=0; + unsigned char *pf; + int diff; + + /* allocate all ressources */ + if ( ( (int)header->info.image.width != x->x_width ) || + ( (int)header->info.image.height != x->x_height ) ) + { + pdp_opencv_colorfilt_free_ressources( x ); + x->x_width = header->info.image.width; + x->x_height = header->info.image.height; + x->x_size = x->x_width*x->x_height; + pdp_opencv_colorfilt_allocate( x ); + post( "pdp_opencv_colorfilt : reallocated buffers" ); + } + + memcpy(x->x_frame, data, x->x_size*3*sizeof(unsigned char)); + memcpy(newdata, data, x->x_size*3*sizeof(unsigned char)); + + newheader->info.image.encoding = header->info.image.encoding; + newheader->info.image.width = x->x_width; + newheader->info.image.height = x->x_height; + + // filter + pf = newdata; + for ( py=0; py<x->x_height; py++ ) + { + for ( px=0; px<x->x_width; px++ ) + { + r = *(pf); + g = *(pf+1); + b = *(pf+2); + + diff = 0; + diff = abs(r-x->x_colorR ); + diff += abs(g-x->x_colorG ); + diff += abs(b-x->x_colorB ); + + if ( diff > x->x_tolerance ) + { + *(pf) = 0x0; + *(pf+1) = 0x0; + *(pf+2) = 0x0; + } + + pf+=3; + } + } + + return; +} + +static void pdp_opencv_colorfilt_sendpacket(t_pdp_opencv_colorfilt *x) +{ + /* release the packet */ + pdp_packet_mark_unused(x->x_packet0); + x->x_packet0 = -1; + + /* unregister and propagate if valid dest packet */ + pdp_packet_pass_if_valid(x->x_pdp_output, &x->x_packet1); +} + +static void pdp_opencv_colorfilt_process(t_pdp_opencv_colorfilt *x) +{ + int encoding; + t_pdp *header = 0; + + /* check if image data packets are compatible */ + if ( (header = pdp_packet_header(x->x_packet0)) + && (PDP_BITMAP == header->type)){ + + /* pdp_opencv_colorfilt_process inputs and write into active inlet */ + switch(pdp_packet_header(x->x_packet0)->info.image.encoding) + { + + case PDP_BITMAP_RGB: + x->x_packet1 = pdp_packet_clone_rw(x->x_packet0); + pdp_queue_add(x, pdp_opencv_colorfilt_process_rgb, pdp_opencv_colorfilt_sendpacket, &x->x_queue_id); + break; + + default: + /* don't know the type, so dont pdp_opencv_colorfilt_process */ + break; + + } + } + +} + +static void pdp_opencv_colorfilt_input_0(t_pdp_opencv_colorfilt *x, t_symbol *s, t_floatarg f) +{ + /* if this is a register_ro message or register_rw message, register with packet factory */ + + if (s== gensym("register_rw")) + { + x->x_dropped = pdp_packet_convert_ro_or_drop(&x->x_packet0, (int)f, pdp_gensym("bitmap/rgb/*") ); + } + + if ((s == gensym("process")) && (-1 != x->x_packet0) && (!x->x_dropped)) + { + pdp_opencv_colorfilt_process(x); + } +} + +static void pdp_opencv_colorfilt_free(t_pdp_opencv_colorfilt *x) +{ + int i; + + pdp_packet_mark_unused(x->x_packet0); + pdp_opencv_colorfilt_free_ressources( x ); +} + +t_class *pdp_opencv_colorfilt_class; + +void *pdp_opencv_colorfilt_new(void) +{ + int i; + + t_pdp_opencv_colorfilt *x = (t_pdp_opencv_colorfilt *)pd_new(pdp_opencv_colorfilt_class); + inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("R")); + inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("G")); + inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("B")); + + x->x_pdp_output = outlet_new(&x->x_obj, &s_anything); + x->x_R = outlet_new(&x->x_obj, &s_float); + x->x_G = outlet_new(&x->x_obj, &s_float); + x->x_B = outlet_new(&x->x_obj, &s_float); + + x->x_colorR = 128; + x->x_colorG = 128; + x->x_colorB = 128; + + outlet_float( x->x_R, x->x_colorR ); + outlet_float( x->x_G, x->x_colorG ); + outlet_float( x->x_B, x->x_colorB ); + + x->x_packet0 = -1; + x->x_packet1 = -1; + + x->x_tolerance = 50; + + x->x_width = -1; + x->x_height = -1; + x->x_size = -1; + x->x_frame = NULL; + + x->x_canvas = canvas_getcurrent(); + + return (void *)x; +} + + +#ifdef __cplusplus +extern "C" +{ +#endif + + +void pdp_opencv_colorfilt_setup(void) +{ + // post( pdp_opencv_colorfilt_version ); + pdp_opencv_colorfilt_class = class_new(gensym("pdp_opencv_colorfilt"), (t_newmethod)pdp_opencv_colorfilt_new, + (t_method)pdp_opencv_colorfilt_free, sizeof(t_pdp_opencv_colorfilt), 0, A_NULL); + + class_addmethod(pdp_opencv_colorfilt_class, (t_method)pdp_opencv_colorfilt_input_0, gensym("pdp"), A_SYMBOL, A_DEFFLOAT, A_NULL); + class_addmethod(pdp_opencv_colorfilt_class, (t_method)pdp_opencv_colorfilt_r, gensym("R"), A_FLOAT, A_NULL); + class_addmethod(pdp_opencv_colorfilt_class, (t_method)pdp_opencv_colorfilt_g, gensym("G"), A_FLOAT, A_NULL); + class_addmethod(pdp_opencv_colorfilt_class, (t_method)pdp_opencv_colorfilt_b, gensym("B"), A_FLOAT, A_NULL); + class_addmethod(pdp_opencv_colorfilt_class, (t_method)pdp_opencv_colorfilt_pick, gensym("pick"), A_FLOAT, A_FLOAT, A_NULL); + class_addmethod(pdp_opencv_colorfilt_class, (t_method)pdp_opencv_colorfilt_tolerance, gensym("tolerance"), A_FLOAT, A_NULL); + + class_sethelpsymbol( pdp_opencv_colorfilt_class, gensym("pdp_opencv_colorfilt.pd") ); +} + +#ifdef __cplusplus +} +#endif |