aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--pdp_opencv_camshift-help.pd124
-rw-r--r--pdp_opencv_camshift.cc370
3 files changed, 495 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 9526f70..8405cb1 100644
--- a/Makefile
+++ b/Makefile
@@ -37,7 +37,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_surf.cc pdp_opencv_athreshold.c pdp_opencv_hough_lines.cc pdp_opencv_channels.cc pdp_opencv_hough_circles.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_surf.cc pdp_opencv_athreshold.c pdp_opencv_hough_lines.cc pdp_opencv_channels.cc pdp_opencv_hough_circles.cc pdp_opencv_camshift.cc
all: $(SOURCES:.c=.$(EXTENSION)) $(SOURCES:.cc=.$(EXTENSION))
diff --git a/pdp_opencv_camshift-help.pd b/pdp_opencv_camshift-help.pd
new file mode 100644
index 0000000..15f9574
--- /dev/null
+++ b/pdp_opencv_camshift-help.pd
@@ -0,0 +1,124 @@
+#N canvas 20 96 1039 729 10;
+#X obj 11 -57 cnv 15 750 250 empty empty empty 20 12 0 14 -260097 -66577
+0;
+#X obj 251 -17 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0
+1;
+#X obj 251 10 metro 40;
+#X msg 341 37 close;
+#X obj 271 108 pdp_v4l;
+#X msg 328 5 open /dev/video0;
+#X obj 440 -22 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1
+1;
+#X obj 440 5 metro 40;
+#X msg 521 24 close;
+#X msg 517 0 open /dev/video0;
+#X obj 461 104 pdp_v4l2;
+#X msg 526 79 format \$1;
+#X obj 527 57 hradio 15 1 0 4 empty empty empty 0 -8 0 10 -262144 -1
+-1 0;
+#X obj 41 158 pdp_qt;
+#X obj 74 94 metro 40;
+#X msg 74 66 bang;
+#X msg 115 66 stop;
+#X obj 41 -3 openpanel;
+#X msg 41 22 open \$1;
+#X msg 41 -31 bang;
+#X msg 75 121 loop \$1;
+#X obj 155 121 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1
+1;
+#X obj 155 39 loadbang;
+#X text 84 -28 playing a video file;
+#X text 281 -19 Camera input;
+#X obj 101 159 hsl 128 15 0 500 0 0 empty empty empty -2 -8 0 10 -262144
+-1 -1 0 1;
+#X obj 12 201 cnv 15 750 300 empty empty empty 20 12 0 14 -258113 -66577
+0;
+#X obj 40 519 pdp_xv;
+#X text 13 -74 written by Yves Degoyon ( ydegoyon@gmail.com );
+#X obj 42 410 pdp_opencv_camshift;
+#X msg 127 307 backproject \$1;
+#X text 246 308 show the backproject flow image;
+#X obj 224 308 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1
+1;
+#X msg 85 224 vmin \$1;
+#X floatatom 141 226 5 0 0 0 - - -;
+#X floatatom 157 250 5 0 0 0 - - -;
+#X msg 103 250 vmax \$1;
+#X floatatom 170 281 5 0 0 0 - - -;
+#X msg 116 280 smin \$1;
+#X text 195 250 V pre-filtering maximal value ( default : 256 );
+#X text 209 279 S pre-filtering minimal value ( default : 30 );
+#X obj 40 542 route press;
+#X msg 159 387 track \$1 \$2;
+#X msg 91 518 cursor 1;
+#X text 236 385 track %x %y : mark the object to track;
+#X text 163 517 <-- select a point to start tracking an object;
+#X floatatom 203 340 5 0 0 0 - - -;
+#X floatatom 220 364 5 0 0 0 - - -;
+#X msg 138 338 rwidth \$1;
+#X msg 147 362 rheight \$1;
+#X text 239 338 initial search region width ( default : 20 );
+#X text 258 363 initial search region height ( default : 20 );
+#X text 186 226 V pre-filtering minimal value ( default : 50 );
+#X obj 170 427 unpack f f f f f;
+#X floatatom 171 457 5 0 0 0 - - -;
+#X floatatom 210 457 5 0 0 0 - - -;
+#X floatatom 248 457 5 0 0 0 - - -;
+#X floatatom 287 457 5 0 0 0 - - -;
+#X floatatom 328 457 5 0 0 0 - - -;
+#X text 198 481 center Y;
+#X text 248 471 width;
+#X text 283 482 height;
+#X text 158 471 center X;
+#X text 326 473 angle;
+#X text 13 -110 pdp_opencv_camshift : continuously adaptive mean-shift
+algorithm to track objects based on a hidden histogram of the hue component
+of pixels.;
+#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 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 11 0 10 0;
+#X connect 12 0 11 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 27 0 41 0;
+#X connect 29 0 27 0;
+#X connect 29 0 43 0;
+#X connect 29 1 53 0;
+#X connect 30 0 29 0;
+#X connect 32 0 30 0;
+#X connect 33 0 29 0;
+#X connect 34 0 33 0;
+#X connect 35 0 36 0;
+#X connect 36 0 29 0;
+#X connect 37 0 38 0;
+#X connect 38 0 29 0;
+#X connect 41 0 42 0;
+#X connect 42 0 29 0;
+#X connect 43 0 27 0;
+#X connect 46 0 48 0;
+#X connect 47 0 49 0;
+#X connect 48 0 29 0;
+#X connect 49 0 29 0;
+#X connect 53 0 54 0;
+#X connect 53 1 55 0;
+#X connect 53 2 56 0;
+#X connect 53 3 57 0;
+#X connect 53 4 58 0;
diff --git a/pdp_opencv_camshift.cc b/pdp_opencv_camshift.cc
new file mode 100644
index 0000000..e06aa57
--- /dev/null
+++ b/pdp_opencv_camshift.cc
@@ -0,0 +1,370 @@
+/*
+ * Pure Data Packet module.
+ * Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
+ *
+ * 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.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dirent.h>
+#include <limits.h>
+#include <dlfcn.h>
+
+#include "pdp.h"
+
+#ifndef _EiC
+#include "cv.h"
+#endif
+
+typedef struct pdp_opencv_camshift_struct
+{
+ t_object x_obj;
+ t_float x_f;
+ t_atom x_list[5];
+
+ t_outlet *x_outlet0;
+ t_outlet *x_outlet1;
+ int x_packet0;
+ int x_packet1;
+ int x_dropped;
+ int x_queue_id;
+
+ int x_width;
+ int x_height;
+ int x_size;
+
+ int x_track;
+ int x_init;
+ int x_rwidth;
+ int x_rheight;
+ int x_backproject;
+ int x_vmin;
+ int x_vmax;
+ int x_smin;
+
+ IplImage *image, *hsv, *hue, *mask, *backproject;
+ CvHistogram *hist;
+ CvPoint origin;
+ CvRect selection;
+ CvRect trackwindow;
+ CvBox2D trackbox;
+ CvConnectedComp trackcomp;
+
+} t_pdp_opencv_camshift;
+
+static CvScalar pdp_opencv_camshift_hsv2rgb( float hue )
+{
+ int rgb[3], p, sector;
+ static const int sector_data[][3]= {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
+ hue *= 0.033333333333333333333333333333333f;
+ sector = cvFloor(hue);
+ p = cvRound(255*(hue - sector));
+ p ^= sector & 1 ? 255 : 0;
+
+ rgb[sector_data[sector][0]] = 255;
+ rgb[sector_data[sector][1]] = 0;
+ rgb[sector_data[sector][2]] = p;
+
+ return cvScalar(rgb[2], rgb[1], rgb[0],0);
+}
+
+static void pdp_opencv_camshift_process_rgb(t_pdp_opencv_camshift *x)
+{
+ t_pdp *header = pdp_packet_header(x->x_packet0);
+ short int *data = (short int *)pdp_packet_data(x->x_packet0);
+ t_pdp *newheader = pdp_packet_header(x->x_packet1);
+ short int *newdata = (short int *)pdp_packet_data(x->x_packet1);
+ int hdims = 16;
+ float hranges_arr[] = {0,180};
+ float* hranges = hranges_arr;
+
+ if ((x->x_width != (t_int)header->info.image.width) ||
+ (x->x_height != (t_int)header->info.image.height))
+ {
+
+ post("pdp_opencv_camshift :: resizing");
+
+ x->x_width = header->info.image.width;
+ x->x_height = header->info.image.height;
+ x->x_size = x->x_width*x->x_height;
+
+ //Destroy cv_images
+ cvReleaseImage(&x->image);
+ cvReleaseImage(&x->hsv);
+ cvReleaseImage(&x->hue);
+ cvReleaseImage(&x->mask);
+ cvReleaseImage(&x->backproject);
+ cvReleaseHist(&x->hist);
+
+ x->image = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 3);
+ x->hsv = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 3);
+ x->hue = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 1);
+ x->mask = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 1);
+ x->backproject = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 1);
+ x->hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
+ }
+
+ newheader->info.image.encoding = header->info.image.encoding;
+ newheader->info.image.width = x->x_width;
+ newheader->info.image.height = x->x_height;
+
+ memcpy( newdata, data, x->x_size*3 );
+
+ memcpy( x->image->imageData, data, x->x_size*3 );
+
+ // Convert to hsv
+ cvCvtColor(x->image, x->hsv, CV_BGR2HSV);
+
+ if ( x->x_track )
+ {
+ cvInRangeS( x->hsv, cvScalar(0,x->x_smin,MIN(x->x_vmin,x->x_vmax),0), cvScalar(180,256,MAX(x->x_vmin,x->x_vmax),0), x->mask );
+ cvSplit( x->hsv, x->hue, 0, 0, 0 );
+
+ if ( x->x_init )
+ {
+ float max_val = 0.f;
+ x->x_init = 0;
+ cvSetImageROI( x->hue, x->selection );
+ cvSetImageROI( x->mask, x->selection );
+ cvCalcHist( &x->hue, x->hist, 0, x->mask );
+ cvGetMinMaxHistValue( x->hist, 0, &max_val, 0, 0 );
+ cvConvertScale( x->hist->bins, x->hist->bins, max_val ? 255. / max_val : 0., 0 );
+ cvResetImageROI( x->hue );
+ cvResetImageROI( x->mask );
+ x->trackwindow = x->selection;
+ }
+
+ cvCalcBackProject( (IplImage**)&(x->hue), (CvArr*)x->backproject, (const CvHistogram*)x->hist );
+ cvAnd( x->backproject, x->mask, x->backproject, 0 );
+ cvCamShift( x->backproject, x->trackwindow,
+ cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
+ &x->trackcomp, &x->trackbox );
+ x->trackwindow = x->trackcomp.rect;
+
+ if( x->x_backproject )
+ cvCvtColor( x->backproject, x->image, CV_GRAY2BGR );
+ if( !x->image->origin )
+ x->trackbox.angle = -x->trackbox.angle;
+ cvEllipseBox( x->image, x->trackbox, CV_RGB(255,0,0), 3, CV_AA, 0 );
+ SETFLOAT(&x->x_list[0], x->trackbox.center.x);
+ SETFLOAT(&x->x_list[1], x->trackbox.center.y);
+ SETFLOAT(&x->x_list[2], x->trackbox.size.width);
+ SETFLOAT(&x->x_list[3], x->trackbox.size.height);
+ SETFLOAT(&x->x_list[4], x->trackbox.angle);
+ outlet_list( x->x_outlet1, 0, 5, x->x_list );
+ }
+
+ memcpy( newdata, x->image->imageData, x->x_size*3 );
+
+ return;
+}
+
+static void pdp_opencv_camshift_backproject(t_pdp_opencv_camshift *x, t_floatarg f)
+{
+ if ( ( (int)f==0 ) || ( (int)f==1 ) ) x->x_backproject = (int)f;
+}
+
+static void pdp_opencv_camshift_vmin(t_pdp_opencv_camshift *x, t_floatarg f)
+{
+ if ( ( (int)f>=0 ) || ( (int)f<256 ) ) x->x_vmin = (int)f;
+}
+
+static void pdp_opencv_camshift_vmax(t_pdp_opencv_camshift *x, t_floatarg f)
+{
+ if ( ( (int)f>=0 ) || ( (int)f<256 ) ) x->x_vmax = (int)f;
+}
+
+static void pdp_opencv_camshift_smin(t_pdp_opencv_camshift *x, t_floatarg f)
+{
+ if ( ( (int)f>=0 ) || ( (int)f<256 ) ) x->x_smin = (int)f;
+}
+
+static void pdp_opencv_camshift_track(t_pdp_opencv_camshift *x, t_floatarg perx, t_floatarg pery)
+{
+ int ox,oy;
+ int rx,ry;
+ int w,h;
+
+ if ( ( perx<0.0 ) || ( perx>1.0 ) || ( pery<0.0 ) || ( pery>1.0 ) ) return;
+
+ ox = (int)(perx*x->x_width);
+ oy = (int)(pery*x->x_height);
+ x->origin = cvPoint(ox,oy);
+ rx = ( ox-(x->x_rwidth/2) < 0 )? 0:ox-(x->x_rwidth/2);
+ ry = ( oy-(x->x_rheight/2) < 0 )? 0:oy-(x->x_rheight/2);
+ w = (rx+x->x_rwidth>x->x_width ) ? ( x->x_width - rx ):x->x_rwidth;
+ h = (ry+x->x_rheight>x->x_height ) ? ( x->x_height - ry ):x->x_rheight;
+ x->selection = cvRect(rx,ry,w,h);
+ x->x_track = 1;
+ x->x_init = 1;
+}
+
+static void pdp_opencv_camshift_rwidth(t_pdp_opencv_camshift *x, t_floatarg f)
+{
+ if ( (int)f>=0 ) x->x_rwidth = (int)f;
+ // refresh selection zone
+ pdp_opencv_camshift_track( x, ((float)x->origin.x)/((float)x->x_width), ((float)x->origin.y)/((float)x->x_height) );
+}
+
+static void pdp_opencv_camshift_rheight(t_pdp_opencv_camshift *x, t_floatarg f)
+{
+ if ( (int)f>=0 ) x->x_rheight = (int)f;
+ // refresh selection zone
+ pdp_opencv_camshift_track( x, ((float)x->origin.x)/((float)x->x_width), ((float)x->origin.y)/((float)x->x_height) );
+}
+
+static void pdp_opencv_camshift_sendpacket(t_pdp_opencv_camshift *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_outlet0, &x->x_packet1);
+}
+
+static void pdp_opencv_camshift_process(t_pdp_opencv_camshift *x)
+{
+ int encoding;
+ t_pdp *header = 0;
+ char *parname;
+ unsigned pi;
+ int partype;
+ float pardefault;
+ t_atom plist[2];
+ t_atom tlist[2];
+ t_atom vlist[2];
+
+ /* check if image data packets are compatible */
+ if ( (header = pdp_packet_header(x->x_packet0))
+ && (PDP_BITMAP == header->type)){
+
+ /* pdp_opencv_camshift_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, (void*)pdp_opencv_camshift_process_rgb, (void*)pdp_opencv_camshift_sendpacket, &x->x_queue_id);
+ break;
+
+ default:
+ /* don't know the type, so dont pdp_opencv_camshift_process */
+ break;
+
+ }
+ }
+}
+
+static void pdp_opencv_camshift_input_0(t_pdp_opencv_camshift *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((char*)"bitmap/rgb/*") );
+
+ if ((s == gensym("process")) && (-1 != x->x_packet0) && (!x->x_dropped))
+ {
+ /* add the process method and callback to the process queue */
+ pdp_opencv_camshift_process(x);
+ }
+}
+
+static void pdp_opencv_camshift_free(t_pdp_opencv_camshift *x)
+{
+ int i;
+
+ pdp_queue_finish(x->x_queue_id);
+ pdp_packet_mark_unused(x->x_packet0);
+
+ //Destroy cv_images
+ cvReleaseImage(&x->image);
+ cvReleaseImage(&x->hsv);
+ cvReleaseImage(&x->hue);
+ cvReleaseImage(&x->mask);
+ cvReleaseImage(&x->backproject);
+ cvReleaseHist(&x->hist);
+}
+
+t_class *pdp_opencv_camshift_class;
+
+void *pdp_opencv_camshift_new(t_floatarg f)
+{
+ int hdims = 16;
+ float hranges_arr[] = {0,180};
+ float* hranges = hranges_arr;
+
+ t_pdp_opencv_camshift *x = (t_pdp_opencv_camshift *)pd_new(pdp_opencv_camshift_class);
+
+ x->x_outlet0 = outlet_new(&x->x_obj, &s_anything);
+ x->x_outlet1 = outlet_new(&x->x_obj, &s_anything);
+
+ x->x_packet0 = -1;
+ x->x_packet1 = -1;
+ x->x_queue_id = -1;
+
+ x->x_width = 320;
+ x->x_height = 240;
+ x->x_size = x->x_width * x->x_height;
+
+ x->x_track = 0;
+ x->x_init = 0;
+ x->x_rwidth = 20;
+ x->x_rheight = 20;
+ x->x_backproject = 0;
+ x->x_vmin = 50;
+ x->x_vmax = 256;
+ x->x_smin = 30;
+
+ x->image = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 3);
+ x->hsv = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 3);
+ x->hue = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 1);
+ x->mask = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 1);
+ x->backproject = cvCreateImage(cvSize(x->x_width,x->x_height), IPL_DEPTH_8U, 1);
+ x->hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
+
+ return (void *)x;
+}
+
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+void pdp_opencv_camshift_setup(void)
+{
+
+ post( " pdp_opencv_camshift");
+ pdp_opencv_camshift_class = class_new(gensym("pdp_opencv_camshift"), (t_newmethod)pdp_opencv_camshift_new,
+ (t_method)pdp_opencv_camshift_free, sizeof(t_pdp_opencv_camshift), 0, A_DEFFLOAT, A_NULL);
+
+ class_addmethod(pdp_opencv_camshift_class, (t_method)pdp_opencv_camshift_input_0, gensym("pdp"), A_SYMBOL, A_DEFFLOAT, A_NULL);
+ class_addmethod(pdp_opencv_camshift_class, (t_method)pdp_opencv_camshift_backproject, gensym("backproject"), A_FLOAT, A_NULL );
+ class_addmethod(pdp_opencv_camshift_class, (t_method)pdp_opencv_camshift_vmin, gensym("vmin"), A_FLOAT, A_NULL );
+ class_addmethod(pdp_opencv_camshift_class, (t_method)pdp_opencv_camshift_vmax, gensym("vmax"), A_FLOAT, A_NULL );
+ class_addmethod(pdp_opencv_camshift_class, (t_method)pdp_opencv_camshift_smin, gensym("smin"), A_FLOAT, A_NULL );
+ class_addmethod(pdp_opencv_camshift_class, (t_method)pdp_opencv_camshift_track, gensym("track"), A_FLOAT, A_FLOAT, A_NULL );
+ class_addmethod(pdp_opencv_camshift_class, (t_method)pdp_opencv_camshift_rwidth, gensym("rwidth"), A_FLOAT, A_NULL );
+ class_addmethod(pdp_opencv_camshift_class, (t_method)pdp_opencv_camshift_rheight, gensym("rheight"), A_FLOAT, A_NULL );
+
+}
+
+#ifdef __cplusplus
+}
+#endif