aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlluís gómez i bigordà <lluisbigorda@users.sourceforge.net>2009-09-18 10:25:06 +0000
committerlluís gómez i bigordà <lluisbigorda@users.sourceforge.net>2009-09-18 10:25:06 +0000
commit589b247931bb64edc2e13d19bbf6f4db15e6b9c6 (patch)
treea67c958b198e8f2ea747c6193098be413adc834d
parent00730c3a0246e33eb0b36e4bfef768ca838e02b2 (diff)
added pix_opencv_convexhull object
svn path=/trunk/externals/pix_opencv/; revision=12390
-rw-r--r--pix_opencv_contours_convexhull-help.pd133
-rw-r--r--pix_opencv_contours_convexhull.cc562
-rw-r--r--pix_opencv_contours_convexhull.h87
3 files changed, 782 insertions, 0 deletions
diff --git a/pix_opencv_contours_convexhull-help.pd b/pix_opencv_contours_convexhull-help.pd
new file mode 100644
index 0000000..a11072d
--- /dev/null
+++ b/pix_opencv_contours_convexhull-help.pd
@@ -0,0 +1,133 @@
+#N canvas 547 63 1018 740 10;
+#X obj 264 18 gemhead;
+#X obj 195 378 pix_texture;
+#X obj 195 406 square 2;
+#X obj 558 -22 cnv 15 220 70 empty empty empty 20 12 0 14 -195568 -66577
+0;
+#N canvas 0 22 454 304 gemwin 0;
+#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;
+#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 obj 160 260 gemwin;
+#X msg 159 207 create \, 1;
+#X msg 235 208 destroy;
+#X msg 281 180 frame 5;
+#X obj 162 170 t b b b;
+#X connect 1 0 2 0;
+#X connect 2 0 3 0;
+#X connect 2 0 10 0;
+#X connect 2 1 4 0;
+#X connect 2 1 8 0;
+#X connect 3 0 0 0;
+#X connect 4 0 0 0;
+#X connect 7 0 6 0;
+#X connect 8 0 6 0;
+#X connect 9 0 6 0;
+#X connect 10 0 7 0;
+#X connect 10 2 9 0;
+#X restore 563 22 pd gemwin;
+#X msg 564 0 create;
+#X text 560 -20 Create window and render;
+#X obj 31 372 pix_texture;
+#X obj 31 400 square 2;
+#X obj 97 171 translateXYZ -2 0 0;
+#X obj 198 228 separator;
+#X obj 177 285 cnv 15 255 67 empty empty empty 20 12 0 14 -24198 -66577
+0;
+#X obj 342 -62 bng 25 250 50 0 empty empty empty 0 -6 0 8 -262144 -1
+-1;
+#X obj 355 132 unpack 0 0 0;
+#X floatatom 345 155 5 0 0 3 length - -;
+#X floatatom 394 155 5 0 0 3 width - -;
+#X floatatom 444 155 5 0 0 3 height - -;
+#X obj 399 113 bng 15 250 50 0 empty empty end_reached 20 7 0 10 -262144
+-1 -1;
+#X floatatom 381 78 5 0 10000 1 frame# - -;
+#X obj 342 -33 openpanel;
+#X msg 342 -13 open \$1;
+#X obj 330 96 pix_film;
+#X msg 348 30 auto \$1;
+#X obj 348 12 tgl 15 0 empty empty empty 0 -6 0 8 -262144 -1 -1 0 1
+;
+#X obj 195 263 translateXYZ 4 0 0;
+#X obj 209 114 pix_invert;
+#X obj 150 139 pix_threshold;
+#X floatatom 256 146 5 0 0 0 - - -;
+#X msg 272 195 0.33;
+#X obj 310 196 loadbang;
+#X floatatom 438 484 5 0 0 0 - - -;
+#X floatatom 473 484 5 0 0 0 - - -;
+#X floatatom 503 465 5 0 0 0 - - -;
+#X floatatom 537 465 5 0 0 0 - - -;
+#X obj 465 404 unpack 0 0 0 0 0 0;
+#X floatatom 567 446 5 0 0 0 - - -;
+#X floatatom 601 446 5 0 0 0 - - -;
+#X floatatom 291 379 5 0 0 0 - - -;
+#X text 504 230 This object considers a contour to be analyzed any
+group of white pixels in a black background \, for this reason you
+will find useful to use pix_threshold to obtain a correct input for
+pix_opencv_contours_boundingrect.;
+#X msg 390 54 colorspace RGBA;
+#X obj 413 27 loadbang;
+#X msg 497 55 colorspace RGB;
+#X msg 596 55 colorspace Grey;
+#X obj 195 328 pix_opencv_contours_convexhull;
+#X text 287 394 number convexity vertex;
+#X text 640 447 etc ...;
+#X text 575 465 Second vertex point (X -Y);
+#X text 513 484 First vertex point (X -Y);
+#X text 505 194 pix_opencv_contours_convexity :: Finds convexhull of
+the bigest contour of a binary image.;
+#X text 505 283 pix_opencv_contours_convexhull works detecting the
+bigest white area in the input image \, sometimes you will need to
+use pix_opencv_morphology to transform the contours of the shape to
+be analyzed..;
+#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 25 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 43 0;
+#X connect 25 0 26 0;
+#X connect 26 0 9 0;
+#X connect 27 0 26 1;
+#X connect 28 0 27 0;
+#X connect 29 0 28 0;
+#X connect 34 0 30 0;
+#X connect 34 1 31 0;
+#X connect 34 2 32 0;
+#X connect 34 3 33 0;
+#X connect 34 4 35 0;
+#X connect 34 5 36 0;
+#X connect 39 0 21 0;
+#X connect 40 0 39 0;
+#X connect 41 0 21 0;
+#X connect 42 0 21 0;
+#X connect 43 0 1 0;
+#X connect 43 1 37 0;
+#X connect 43 2 34 0;
diff --git a/pix_opencv_contours_convexhull.cc b/pix_opencv_contours_convexhull.cc
new file mode 100644
index 0000000..ebd60a3
--- /dev/null
+++ b/pix_opencv_contours_convexhull.cc
@@ -0,0 +1,562 @@
+////////////////////////////////////////////////////////
+//
+// 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_convexhull.h"
+
+CPPEXTERN_NEW(pix_opencv_contours_convexhull)
+
+/////////////////////////////////////////////////////////
+//
+// pix_opencv_contours_convexhull
+//
+/////////////////////////////////////////////////////////
+// Constructor
+//
+/////////////////////////////////////////////////////////
+pix_opencv_contours_convexhull :: pix_opencv_contours_convexhull()
+{
+ //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_nomdef = outlet_new(this->x_obj, 0);
+ m_dataout = outlet_new(this->x_obj, 0);
+ minarea = 1;
+ maxarea = 320*240;
+ comp_xsize = 0;
+ comp_ysize = 0;
+ orig = NULL;
+ gray = NULL;
+ rgb = NULL;
+
+}
+
+/////////////////////////////////////////////////////////
+// Destructor
+//
+/////////////////////////////////////////////////////////
+pix_opencv_contours_convexhull :: ~pix_opencv_contours_convexhull()
+{
+ //Destroy cv_images to clean memory
+ cvReleaseImage(&orig);
+ cvReleaseImage(&gray);
+ cvReleaseImage(&rgb);
+}
+
+/////////////////////////////////////////////////////////
+// processImage
+//
+/////////////////////////////////////////////////////////
+void pix_opencv_contours_convexhull :: processRGBAImage(imageStruct &image)
+{
+ unsigned char *pixels = image.data;
+
+ 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(&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);
+
+ }
+ // 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);
+ cvCvtColor(orig, rgb, CV_RGBA2RGB);
+
+ //CvSeq* seqhull;
+ CvSeq* defects;
+ CvSeq* contours;
+ int* hull;
+ int hullsize;
+ CvPoint* PointArray;
+ CvConvexityDefect* defectArray;
+ CvMemStorage* stor02;
+ CvMemStorage* stor03;
+ stor02 = cvCreateMemStorage(0);
+ stor03 = cvCreateMemStorage(0);
+
+
+ cvFindContours( gray, stor02, &contours, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
+ if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 );
+
+ int i = 0;
+ int area = 0;
+ int selected = -1;
+
+ //busquem el contorn mes gran
+ CvSeq* first_contour;
+ first_contour = contours;
+ for( ; contours != 0; contours = contours->h_next )
+ {
+ CvRect rect;
+ int count = contours->total;
+ rect = cvContourBoundingRect(contours, 1);
+ if ( (rect.width*rect.height) > area )
+ {
+ selected = i;
+ area = rect.width*rect.height;
+ }
+ i++;
+ }
+
+ contours = first_contour;
+
+ int k = 0;
+ for( ; contours != 0; contours = contours->h_next )
+ {
+ int i; // Indicator of cycles.
+ int count = contours->total; // This is number point in contour
+ CvPoint center;
+ CvSize size;
+ CvRect rect;
+
+ rect = cvContourBoundingRect( contours, 1);
+ if ( (k==selected) ) {
+
+
+ //fprintf(stderr,"malloc\n");
+ // Alloc memory for contour point set.
+ PointArray = (CvPoint*)malloc( count*sizeof(CvPoint) );
+
+ // Alloc memory for indices of convex hull vertices.
+ hull = (int*)malloc(sizeof(int)*count);
+
+ // Get contour point set.
+ //fprintf(stderr,"cvCvtSeqToArray\n");
+ cvCvtSeqToArray(contours, PointArray, CV_WHOLE_SEQ);
+
+
+ // Find convex hull for curent contour.
+ //fprintf(stderr,"cvConvexHull\n");
+ cvConvexHull( PointArray,
+ count,
+ NULL,
+ CV_COUNTER_CLOCKWISE,
+ hull,
+ &hullsize);
+
+ // Find convex hull for current contour.
+ // This required for cvConvexityDefects().
+ //fprintf(stderr,"cvConvexHull2\n");
+ //seqhull = cvConvexHull2( contours,0,
+ // CV_COUNTER_CLOCKWISE,
+ // 0);
+
+
+ // Draw current contour.
+ //cvDrawContours(x->cnt_img,contours,CV_RGB(255,255,255),CV_RGB(255,255,255),0,1, 8);
+ //cvDrawContours( rgb, contours, CV_RGB(255,0,0), CV_RGB(0,255,0), 2, 2, CV_AA, cvPoint(0,0) );
+ outlet_float( m_nomdef, hullsize );
+
+ t_atom rlist[hullsize*2];
+
+ int j=0;
+ // Draw convex hull for current contour.
+ for(i=0; i<hullsize-1; i++)
+ {
+ cvLine(rgb, PointArray[hull[i]],
+ PointArray[hull[i+1]],CV_RGB(0,0,255),1, CV_AA, 0 );
+ SETFLOAT(&rlist[j], PointArray[hull[i]].x);
+ SETFLOAT(&rlist[j+1], PointArray[hull[i]].y);
+ j = j + 2;
+ }
+ cvLine(rgb, PointArray[hull[hullsize-1]],
+ PointArray[hull[0]],CV_RGB(0,0,255),1, CV_AA, 0 );
+
+ SETFLOAT(&rlist[j], PointArray[hull[i]].x);
+ SETFLOAT(&rlist[j+1], PointArray[hull[i]].y);
+ outlet_list( m_dataout, 0, hullsize*2, rlist );
+
+
+ // Free memory.
+ free(PointArray);
+ free(hull);
+ /* replace CV_FILLED with 1 to see the outlines */
+ //cvDrawContours( x->cnt_img, contours, CV_RGB(255,0,0), CV_RGB(0,255,0), x->levels, 3, CV_AA, cvPoint(0,0) );
+ //cvConvexityDefects( contours, cvConvexHull2( contours, 0, CV_CLOCKWISE, 0 ), stor022 );
+ }
+ k++;
+ }
+
+ cvReleaseMemStorage( &stor03 );
+ cvReleaseMemStorage( &stor02 );
+ //if (defects) cvClearSeq(defects);
+ //if (seqhull) cvClearSeq(seqhull);
+
+ cvCvtColor(rgb, orig, CV_RGB2RGBA);
+ //copy back the processed frame to image
+ memcpy( image.data, orig->imageData, image.xsize*image.ysize*4 );
+}
+
+void pix_opencv_contours_convexhull :: processRGBImage(imageStruct &image)
+{
+ unsigned char *pixels = image.data;
+
+ 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(&rgb);
+
+ //create the orig image with new size
+ rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);
+
+ // Create the output images with new sizes
+
+ gray = 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* seqhull;
+ CvSeq* defects;
+ CvSeq* contours;
+ int* hull;
+ int hullsize;
+ CvPoint* PointArray;
+ CvConvexityDefect* defectArray;
+ CvMemStorage* stor02;
+ CvMemStorage* stor03;
+ stor02 = cvCreateMemStorage(0);
+ stor03 = cvCreateMemStorage(0);
+
+
+ cvFindContours( gray, stor02, &contours, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
+ if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 );
+
+ int i = 0;
+ int area = 0;
+ int selected = -1;
+
+ //busquem el contorn mes gran
+ CvSeq* first_contour;
+ first_contour = contours;
+ for( ; contours != 0; contours = contours->h_next )
+ {
+ CvRect rect;
+ int count = contours->total;
+ rect = cvContourBoundingRect(contours, 1);
+ if ( (rect.width*rect.height) > area )
+ {
+ selected = i;
+ area = rect.width*rect.height;
+ }
+ i++;
+ }
+
+ contours = first_contour;
+
+ int k = 0;
+ for( ; contours != 0; contours = contours->h_next )
+ {
+ int i; // Indicator of cycles.
+ int count = contours->total; // This is number point in contour
+ CvPoint center;
+ CvSize size;
+ CvRect rect;
+
+ rect = cvContourBoundingRect( contours, 1);
+ if ( (k==selected) ) {
+
+
+ //fprintf(stderr,"malloc\n");
+ // Alloc memory for contour point set.
+ PointArray = (CvPoint*)malloc( count*sizeof(CvPoint) );
+
+ // Alloc memory for indices of convex hull vertices.
+ hull = (int*)malloc(sizeof(int)*count);
+
+ // Get contour point set.
+ //fprintf(stderr,"cvCvtSeqToArray\n");
+ cvCvtSeqToArray(contours, PointArray, CV_WHOLE_SEQ);
+
+
+ // Find convex hull for curent contour.
+ //fprintf(stderr,"cvConvexHull\n");
+ cvConvexHull( PointArray,
+ count,
+ NULL,
+ CV_COUNTER_CLOCKWISE,
+ hull,
+ &hullsize);
+
+ // Find convex hull for current contour.
+ // This required for cvConvexityDefects().
+ //fprintf(stderr,"cvConvexHull2\n");
+ //seqhull = cvConvexHull2( contours,0,
+ // CV_COUNTER_CLOCKWISE,
+ // 0);
+
+
+ // Draw current contour.
+ //cvDrawContours(x->cnt_img,contours,CV_RGB(255,255,255),CV_RGB(255,255,255),0,1, 8);
+ //cvDrawContours( rgb, contours, CV_RGB(255,0,0), CV_RGB(0,255,0), 2, 2, CV_AA, cvPoint(0,0) );
+ outlet_float( m_nomdef, hullsize );
+
+ t_atom rlist[hullsize*2];
+
+ int j=0;
+ // Draw convex hull for current contour.
+ for(i=0; i<hullsize-1; i++)
+ {
+ cvLine(rgb, PointArray[hull[i]],
+ PointArray[hull[i+1]],CV_RGB(0,0,255),1, CV_AA, 0 );
+ SETFLOAT(&rlist[j], PointArray[hull[i]].x);
+ SETFLOAT(&rlist[j+1], PointArray[hull[i]].y);
+ j = j + 2;
+ }
+ cvLine(rgb, PointArray[hull[hullsize-1]],
+ PointArray[hull[0]],CV_RGB(0,0,255),1, CV_AA, 0 );
+
+ SETFLOAT(&rlist[j], PointArray[hull[i]].x);
+ SETFLOAT(&rlist[j+1], PointArray[hull[i]].y);
+ outlet_list( m_dataout, 0, hullsize*2, rlist );
+
+
+ // Free memory.
+ free(PointArray);
+ free(hull);
+ /* replace CV_FILLED with 1 to see the outlines */
+ //cvDrawContours( x->cnt_img, contours, CV_RGB(255,0,0), CV_RGB(0,255,0), x->levels, 3, CV_AA, cvPoint(0,0) );
+ //cvConvexityDefects( contours, cvConvexHull2( contours, 0, CV_CLOCKWISE, 0 ), stor022 );
+ }
+ k++;
+ }
+
+ cvReleaseMemStorage( &stor03 );
+ cvReleaseMemStorage( &stor02 );
+ //if (defects) cvClearSeq(defects);
+ //if (seqhull) cvClearSeq(seqhull);
+
+ //cvShowImage(wndname, cedge);
+ memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 );
+}
+
+void pix_opencv_contours_convexhull :: processYUVImage(imageStruct &image)
+{
+}
+
+void pix_opencv_contours_convexhull :: processGrayImage(imageStruct &image)
+{
+
+ 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(&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);
+
+ }
+ // 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 );
+ cvCvtColor(gray, rgb, CV_GRAY2RGB);
+
+ //CvSeq* seqhull;
+ CvSeq* defects;
+ CvSeq* contours;
+ int* hull;
+ int hullsize;
+ CvPoint* PointArray;
+ CvConvexityDefect* defectArray;
+ CvMemStorage* stor02;
+ CvMemStorage* stor03;
+ stor02 = cvCreateMemStorage(0);
+ stor03 = cvCreateMemStorage(0);
+
+
+ cvFindContours( gray, stor02, &contours, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
+ if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 );
+
+ int i = 0;
+ int area = 0;
+ int selected = -1;
+
+ //busquem el contorn mes gran
+ CvSeq* first_contour;
+ first_contour = contours;
+ for( ; contours != 0; contours = contours->h_next )
+ {
+ CvRect rect;
+ int count = contours->total;
+ rect = cvContourBoundingRect(contours, 1);
+ if ( (rect.width*rect.height) > area )
+ {
+ selected = i;
+ area = rect.width*rect.height;
+ }
+ i++;
+ }
+
+ contours = first_contour;
+
+ int k = 0;
+ for( ; contours != 0; contours = contours->h_next )
+ {
+ int i; // Indicator of cycles.
+ int count = contours->total; // This is number point in contour
+ CvPoint center;
+ CvSize size;
+ CvRect rect;
+
+ rect = cvContourBoundingRect( contours, 1);
+ if ( (k==selected) ) {
+
+
+ //fprintf(stderr,"malloc\n");
+ // Alloc memory for contour point set.
+ PointArray = (CvPoint*)malloc( count*sizeof(CvPoint) );
+
+ // Alloc memory for indices of convex hull vertices.
+ hull = (int*)malloc(sizeof(int)*count);
+
+ // Get contour point set.
+ //fprintf(stderr,"cvCvtSeqToArray\n");
+ cvCvtSeqToArray(contours, PointArray, CV_WHOLE_SEQ);
+
+
+ // Find convex hull for curent contour.
+ //fprintf(stderr,"cvConvexHull\n");
+ cvConvexHull( PointArray,
+ count,
+ NULL,
+ CV_COUNTER_CLOCKWISE,
+ hull,
+ &hullsize);
+
+ // Find convex hull for current contour.
+ // This required for cvConvexityDefects().
+ //fprintf(stderr,"cvConvexHull2\n");
+ //seqhull = cvConvexHull2( contours,0,
+ // CV_COUNTER_CLOCKWISE,
+ // 0);
+
+
+ // Draw current contour.
+ //cvDrawContours(x->cnt_img,contours,CV_RGB(255,255,255),CV_RGB(255,255,255),0,1, 8);
+ //cvDrawContours( rgb, contours, CV_RGB(255,0,0), CV_RGB(0,255,0), 2, 2, CV_AA, cvPoint(0,0) );
+
+ outlet_float( m_nomdef, hullsize );
+
+ t_atom rlist[hullsize*2];
+
+ int j=0;
+ // Draw convex hull for current contour.
+ for(i=0; i<hullsize-1; i++)
+ {
+ cvLine(rgb, PointArray[hull[i]],
+ PointArray[hull[i+1]],CV_RGB(0,0,255),1, CV_AA, 0 );
+ SETFLOAT(&rlist[j], PointArray[hull[i]].x);
+ SETFLOAT(&rlist[j+1], PointArray[hull[i]].y);
+ j = j + 2;
+ }
+ cvLine(rgb, PointArray[hull[hullsize-1]],
+ PointArray[hull[0]],CV_RGB(0,0,255),1, CV_AA, 0 );
+
+ SETFLOAT(&rlist[j], PointArray[hull[i]].x);
+ SETFLOAT(&rlist[j+1], PointArray[hull[i]].y);
+ outlet_list( m_dataout, 0, hullsize*2, rlist );
+
+
+ // Free memory.
+ free(PointArray);
+ free(hull);
+ /* replace CV_FILLED with 1 to see the outlines */
+ //cvDrawContours( x->cnt_img, contours, CV_RGB(255,0,0), CV_RGB(0,255,0), x->levels, 3, CV_AA, cvPoint(0,0) );
+ //cvConvexityDefects( contours, cvConvexHull2( contours, 0, CV_CLOCKWISE, 0 ), stor022 );
+ }
+ k++;
+ }
+
+ cvReleaseMemStorage( &stor03 );
+ cvReleaseMemStorage( &stor02 );
+ //if (defects) cvClearSeq(defects);
+ //if (seqhull) cvClearSeq(seqhull);
+
+ cvCvtColor(rgb, gray, CV_RGB2GRAY);
+
+ //copy back the processed frame to image
+ memcpy( image.data, gray->imageData, image.xsize*image.ysize );
+}
+
+/////////////////////////////////////////////////////////
+// floatThreshMess
+//
+/////////////////////////////////////////////////////////
+void pix_opencv_contours_convexhull :: floatMinAreaMess (float minarea)
+{
+ if (minarea>0) this->minarea = (int)minarea;
+}
+void pix_opencv_contours_convexhull :: floatMaxAreaMess (float maxarea)
+{
+ if (maxarea>0) this->maxarea = (int)maxarea;
+}
+
+/////////////////////////////////////////////////////////
+// static member function
+//
+/////////////////////////////////////////////////////////
+void pix_opencv_contours_convexhull :: obj_setupCallback(t_class *classPtr)
+{
+ class_addmethod(classPtr, (t_method)&pix_opencv_contours_convexhull::floatMinAreaMessCallback,
+ gensym("minarea"), A_FLOAT, A_NULL);
+ class_addmethod(classPtr, (t_method)&pix_opencv_contours_convexhull::floatMaxAreaMessCallback,
+ gensym("maxarea"), A_FLOAT, A_NULL);
+}
+void pix_opencv_contours_convexhull :: floatMaxAreaMessCallback(void *data, t_floatarg maxarea)
+{
+ GetMyClass(data)->floatMaxAreaMess((float)maxarea);
+}
+void pix_opencv_contours_convexhull :: floatMinAreaMessCallback(void *data, t_floatarg minarea)
+{
+ GetMyClass(data)->floatMinAreaMess((float)minarea);
+}
diff --git a/pix_opencv_contours_convexhull.h b/pix_opencv_contours_convexhull.h
new file mode 100644
index 0000000..494136f
--- /dev/null
+++ b/pix_opencv_contours_convexhull.h
@@ -0,0 +1,87 @@
+/*-----------------------------------------------------------------
+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_CONTOURS_CONVEXITY_H_
+#define INCLUDE_PIX_OPENCV_CONTOURS_CONVEXITY_H_
+
+#include "Base/GemPixObj.h"
+
+#ifndef _EiC
+#include "cv.h"
+#endif
+
+/*-----------------------------------------------------------------
+-------------------------------------------------------------------
+CLASS
+ pix_opencv_contours_convexhull
+
+ Change pix to greyscale
+
+KEYWORDS
+ pix
+
+DESCRIPTION
+
+-----------------------------------------------------------------*/
+class GEM_EXTERN pix_opencv_contours_convexhull : public GemPixObj
+{
+ CPPEXTERN_HEADER(pix_opencv_contours_convexhull, GemPixObj)
+
+ public:
+
+ //////////
+ // Constructor
+ pix_opencv_contours_convexhull();
+
+ protected:
+
+ //////////
+ // Destructor
+ virtual ~pix_opencv_contours_convexhull();
+
+ //////////
+ // Do the processing
+ virtual void processRGBAImage(imageStruct &image);
+ virtual void processRGBImage(imageStruct &image);
+ virtual void processYUVImage(imageStruct &image);
+ virtual void processGrayImage(imageStruct &image);
+
+ //////////
+ // Set the new edge threshold
+ void floatMinAreaMess(float minarea);
+ void floatMaxAreaMess(float maxarea);
+ // The new minimal/maximal area
+ int minarea;
+ int maxarea;
+ // to detect changes in the image size
+ int comp_xsize;
+ int comp_ysize;
+
+ private:
+
+ t_outlet *m_nomdef;
+ t_outlet *m_dataout;
+ //////////
+ // Static member functions
+ static void floatMinAreaMessCallback(void *data, t_floatarg minarea);
+ static void floatMaxAreaMessCallback(void *data, t_floatarg maxarea);
+
+ /////////
+ // IplImage needed
+ IplImage *rgb, *orig, *gray;
+
+};
+
+#endif // for header file