From e78c5f1d5c4ad638c66163a9c57737a8634556e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?llu=C3=ADs=20g=C3=B3mez=20i=20bigord=C3=A0?= Date: Sun, 25 May 2008 23:07:29 +0000 Subject: changed extensions cpp to cc for autobuilds svn path=/trunk/externals/pix_opencv/; revision=9916 --- pix_opencv_bgsubstract.cc | 257 +++++++++++++ pix_opencv_bgsubstract.cpp | 257 ------------- pix_opencv_contours_boundingrect.cc | 305 ++++++++++++++++ pix_opencv_contours_boundingrect.cpp | 305 ---------------- pix_opencv_contours_convexity.cc | 684 +++++++++++++++++++++++++++++++++++ pix_opencv_contours_convexity.cpp | 684 ----------------------------------- pix_opencv_distrans.cc | 428 ++++++++++++++++++++++ pix_opencv_distrans.cpp | 428 ---------------------- pix_opencv_edge.cc | 233 ++++++++++++ pix_opencv_edge.cpp | 233 ------------ pix_opencv_haarcascade.cc | 353 ++++++++++++++++++ pix_opencv_haarcascade.cpp | 353 ------------------ pix_opencv_laplace.cc | 237 ++++++++++++ pix_opencv_laplace.cpp | 237 ------------ pix_opencv_morphology.cc | 297 +++++++++++++++ pix_opencv_morphology.cpp | 297 --------------- pix_opencv_motempl.cc | 631 ++++++++++++++++++++++++++++++++ pix_opencv_motempl.cpp | 631 -------------------------------- pix_opencv_threshold.cpp | 266 -------------- pix_opencv_threshold.h | 87 ----- 20 files changed, 3425 insertions(+), 3778 deletions(-) create mode 100644 pix_opencv_bgsubstract.cc delete mode 100644 pix_opencv_bgsubstract.cpp create mode 100644 pix_opencv_contours_boundingrect.cc delete mode 100644 pix_opencv_contours_boundingrect.cpp create mode 100644 pix_opencv_contours_convexity.cc delete mode 100644 pix_opencv_contours_convexity.cpp create mode 100644 pix_opencv_distrans.cc delete mode 100644 pix_opencv_distrans.cpp create mode 100644 pix_opencv_edge.cc delete mode 100644 pix_opencv_edge.cpp create mode 100644 pix_opencv_haarcascade.cc delete mode 100644 pix_opencv_haarcascade.cpp create mode 100644 pix_opencv_laplace.cc delete mode 100644 pix_opencv_laplace.cpp create mode 100644 pix_opencv_morphology.cc delete mode 100644 pix_opencv_morphology.cpp create mode 100644 pix_opencv_motempl.cc delete mode 100644 pix_opencv_motempl.cpp delete mode 100644 pix_opencv_threshold.cpp delete mode 100644 pix_opencv_threshold.h diff --git a/pix_opencv_bgsubstract.cc b/pix_opencv_bgsubstract.cc new file mode 100644 index 0000000..1daa4ef --- /dev/null +++ b/pix_opencv_bgsubstract.cc @@ -0,0 +1,257 @@ +//////////////////////////////////////////////////////// +// +// 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_bgsubstract.h" + +CPPEXTERN_NEW(pix_opencv_bgsubstract) + +///////////////////////////////////////////////////////// +// +// pix_opencv_bgsubstract +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_bgsubstract :: pix_opencv_bgsubstract() +{ + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); + x_threshold = 13; + x_set = 1; + comp_xsize = 0; + comp_ysize = 0; + orig = NULL; + gray = NULL; + rgb = NULL; + grayLow = NULL; + grayUp = NULL; + prev_gray = NULL; + diff_8U = NULL; +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_bgsubstract :: ~pix_opencv_bgsubstract() +{ + //Destroy cv_images to clean memory + cvReleaseImage(&orig); + cvReleaseImage(&gray); + cvReleaseImage(&rgb); + cvReleaseImage(&prev_gray); + cvReleaseImage(&grayLow); + cvReleaseImage(&grayUp); + cvReleaseImage(&diff_8U); +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_bgsubstract :: processRGBAImage(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); + cvReleaseImage(&prev_gray); + cvReleaseImage(&grayLow); + cvReleaseImage(&grayUp); + cvReleaseImage(&diff_8U); + + //create the orig image with new size + orig = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); + rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); + + // Create the output images with new sizes + gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + grayLow = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + grayUp = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + prev_gray = cvCreateImage(cvSize(orig->width,orig->height), 8, 1); + diff_8U = cvCreateImage(cvSize(orig->width,orig->height), 8, 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_BGRA2GRAY); + + if (x_set) { + memcpy( prev_gray->imageData, gray->imageData, image.xsize*image.ysize ); + x_set=0; + } + + cvSubS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayLow,NULL); + cvAddS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayUp,NULL); + cvInRange (gray, grayLow, grayUp, diff_8U); + + cvNot (diff_8U,diff_8U); + + cvCvtColor(diff_8U, orig, CV_GRAY2BGRA); + + //copy back the processed frame to image + memcpy( image.data, orig->imageData, image.xsize*image.ysize*4 ); +} + +void pix_opencv_bgsubstract :: processRGBImage(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); + cvReleaseImage(&prev_gray); + cvReleaseImage(&grayLow); + cvReleaseImage(&grayUp); + cvReleaseImage(&diff_8U); + + //create the orig image with new size + orig = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); + rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); + + // Create the output images with new sizes + gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + grayLow = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + grayUp = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + prev_gray = cvCreateImage(cvSize(orig->width,orig->height), 8, 1); + diff_8U = cvCreateImage(cvSize(orig->width,orig->height), 8, 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( rgb->imageData, image.data, image.xsize*image.ysize*3 ); + + // Convert to grayscale + cvCvtColor(rgb, gray, CV_BGRA2GRAY); + + if (x_set) { + memcpy( prev_gray->imageData, gray->imageData, image.xsize*image.ysize ); + x_set=0; + } + + cvSubS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayLow,NULL); + cvAddS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayUp,NULL); + cvInRange (gray, grayLow, grayUp, diff_8U); + + cvNot (diff_8U,diff_8U); + + cvCvtColor(diff_8U, rgb, CV_GRAY2BGR); + + //copy back the processed frame to image + memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 ); + +} + +void pix_opencv_bgsubstract :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_bgsubstract :: 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); + cvReleaseImage(&prev_gray); + cvReleaseImage(&grayLow); + cvReleaseImage(&grayUp); + cvReleaseImage(&diff_8U); + + //create the orig image with new size + orig = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); + rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); + + // Create the output images with new sizes + gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + grayLow = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + grayUp = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + prev_gray = cvCreateImage(cvSize(orig->width,orig->height), 8, 1); + diff_8U = cvCreateImage(cvSize(orig->width,orig->height), 8, 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 ); + + + if (x_set) { + memcpy( prev_gray->imageData, gray->imageData, image.xsize*image.ysize ); + x_set=0; + } + + cvSubS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayLow,NULL); + cvAddS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayUp,NULL); + cvInRange (gray, grayLow, grayUp, diff_8U); + + cvNot (diff_8U,diff_8U); + + //copy back the processed frame to image + memcpy( image.data, diff_8U->imageData, image.xsize*image.ysize ); +} + +///////////////////////////////////////////////////////// +// floatThreshMess +// +///////////////////////////////////////////////////////// +void pix_opencv_bgsubstract :: floatThreshMess (float x_threshold) +{ + this->x_threshold = (int)x_threshold; +} +void pix_opencv_bgsubstract :: SetMess () +{ + this->x_set = 1; +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_bgsubstract :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_bgsubstract::floatTreshMessCallback, + gensym("ft1"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_bgsubstract::SetMessCallback, + gensym("set"), A_NULL); +} +void pix_opencv_bgsubstract :: floatTreshMessCallback(void *data, t_floatarg x_threshold) +{ + GetMyClass(data)->floatThreshMess((float)x_threshold); +} +void pix_opencv_bgsubstract :: SetMessCallback(void *data) +{ + GetMyClass(data)->SetMess(); +} diff --git a/pix_opencv_bgsubstract.cpp b/pix_opencv_bgsubstract.cpp deleted file mode 100644 index 1daa4ef..0000000 --- a/pix_opencv_bgsubstract.cpp +++ /dev/null @@ -1,257 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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_bgsubstract.h" - -CPPEXTERN_NEW(pix_opencv_bgsubstract) - -///////////////////////////////////////////////////////// -// -// pix_opencv_bgsubstract -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_bgsubstract :: pix_opencv_bgsubstract() -{ - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); - x_threshold = 13; - x_set = 1; - comp_xsize = 0; - comp_ysize = 0; - orig = NULL; - gray = NULL; - rgb = NULL; - grayLow = NULL; - grayUp = NULL; - prev_gray = NULL; - diff_8U = NULL; -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_bgsubstract :: ~pix_opencv_bgsubstract() -{ - //Destroy cv_images to clean memory - cvReleaseImage(&orig); - cvReleaseImage(&gray); - cvReleaseImage(&rgb); - cvReleaseImage(&prev_gray); - cvReleaseImage(&grayLow); - cvReleaseImage(&grayUp); - cvReleaseImage(&diff_8U); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_bgsubstract :: processRGBAImage(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); - cvReleaseImage(&prev_gray); - cvReleaseImage(&grayLow); - cvReleaseImage(&grayUp); - cvReleaseImage(&diff_8U); - - //create the orig image with new size - orig = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); - rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); - - // Create the output images with new sizes - gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - grayLow = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - grayUp = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - prev_gray = cvCreateImage(cvSize(orig->width,orig->height), 8, 1); - diff_8U = cvCreateImage(cvSize(orig->width,orig->height), 8, 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_BGRA2GRAY); - - if (x_set) { - memcpy( prev_gray->imageData, gray->imageData, image.xsize*image.ysize ); - x_set=0; - } - - cvSubS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayLow,NULL); - cvAddS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayUp,NULL); - cvInRange (gray, grayLow, grayUp, diff_8U); - - cvNot (diff_8U,diff_8U); - - cvCvtColor(diff_8U, orig, CV_GRAY2BGRA); - - //copy back the processed frame to image - memcpy( image.data, orig->imageData, image.xsize*image.ysize*4 ); -} - -void pix_opencv_bgsubstract :: processRGBImage(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); - cvReleaseImage(&prev_gray); - cvReleaseImage(&grayLow); - cvReleaseImage(&grayUp); - cvReleaseImage(&diff_8U); - - //create the orig image with new size - orig = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); - rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); - - // Create the output images with new sizes - gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - grayLow = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - grayUp = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - prev_gray = cvCreateImage(cvSize(orig->width,orig->height), 8, 1); - diff_8U = cvCreateImage(cvSize(orig->width,orig->height), 8, 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( rgb->imageData, image.data, image.xsize*image.ysize*3 ); - - // Convert to grayscale - cvCvtColor(rgb, gray, CV_BGRA2GRAY); - - if (x_set) { - memcpy( prev_gray->imageData, gray->imageData, image.xsize*image.ysize ); - x_set=0; - } - - cvSubS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayLow,NULL); - cvAddS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayUp,NULL); - cvInRange (gray, grayLow, grayUp, diff_8U); - - cvNot (diff_8U,diff_8U); - - cvCvtColor(diff_8U, rgb, CV_GRAY2BGR); - - //copy back the processed frame to image - memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 ); - -} - -void pix_opencv_bgsubstract :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_bgsubstract :: 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); - cvReleaseImage(&prev_gray); - cvReleaseImage(&grayLow); - cvReleaseImage(&grayUp); - cvReleaseImage(&diff_8U); - - //create the orig image with new size - orig = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); - rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); - - // Create the output images with new sizes - gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - grayLow = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - grayUp = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - prev_gray = cvCreateImage(cvSize(orig->width,orig->height), 8, 1); - diff_8U = cvCreateImage(cvSize(orig->width,orig->height), 8, 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 ); - - - if (x_set) { - memcpy( prev_gray->imageData, gray->imageData, image.xsize*image.ysize ); - x_set=0; - } - - cvSubS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayLow,NULL); - cvAddS (prev_gray,cvScalar(x_threshold,x_threshold,x_threshold,x_threshold),grayUp,NULL); - cvInRange (gray, grayLow, grayUp, diff_8U); - - cvNot (diff_8U,diff_8U); - - //copy back the processed frame to image - memcpy( image.data, diff_8U->imageData, image.xsize*image.ysize ); -} - -///////////////////////////////////////////////////////// -// floatThreshMess -// -///////////////////////////////////////////////////////// -void pix_opencv_bgsubstract :: floatThreshMess (float x_threshold) -{ - this->x_threshold = (int)x_threshold; -} -void pix_opencv_bgsubstract :: SetMess () -{ - this->x_set = 1; -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_bgsubstract :: obj_setupCallback(t_class *classPtr) -{ - class_addmethod(classPtr, (t_method)&pix_opencv_bgsubstract::floatTreshMessCallback, - gensym("ft1"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_bgsubstract::SetMessCallback, - gensym("set"), A_NULL); -} -void pix_opencv_bgsubstract :: floatTreshMessCallback(void *data, t_floatarg x_threshold) -{ - GetMyClass(data)->floatThreshMess((float)x_threshold); -} -void pix_opencv_bgsubstract :: SetMessCallback(void *data) -{ - GetMyClass(data)->SetMess(); -} diff --git a/pix_opencv_contours_boundingrect.cc b/pix_opencv_contours_boundingrect.cc new file mode 100644 index 0000000..c6ebd94 --- /dev/null +++ b/pix_opencv_contours_boundingrect.cc @@ -0,0 +1,305 @@ +//////////////////////////////////////////////////////// +// +// 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" + +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); + minarea = 1; + maxarea = 320*240; + comp_xsize = 0; + comp_ysize = 0; + orig = NULL; + gray = NULL; + cnt_img = NULL; + rgb = NULL; + +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_contours_boundingrect :: ~pix_opencv_contours_boundingrect() +{ + //Destroy cv_images to clean memory + cvReleaseImage(&orig); + cvReleaseImage(&gray); + cvReleaseImage(&cnt_img); + cvReleaseImage(&rgb); +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_contours_boundingrect :: 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(&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); + cnt_img = 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); + + CvSeq* contours; + CvMemStorage* stor02; + stor02 = cvCreateMemStorage(0); + + cvFindContours( gray, stor02, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); + if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 ); + + + 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; + + rect = cvContourBoundingRect( contours, 1); + if ( ( (rect.width*rect.height) > minarea ) && ( (rect.width*rect.height) < maxarea ) ) { + cvRectangle( orig, cvPoint(rect.x,rect.y), cvPoint(rect.x+rect.width,rect.y+rect.height), CV_RGB(255,0,0), 2, 8 , 0 ); + + t_atom rlist[4]; + SETFLOAT(&rlist[0], i); + 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++; + } + + } + + 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; + + 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); + + // Create the output images with new sizes + cnt_img = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 3); + + 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* contours; + CvMemStorage* stor02; + stor02 = cvCreateMemStorage(0); + + cvFindContours( gray, stor02, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); + if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 ); + + + 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; + + rect = cvContourBoundingRect( contours, 1); + if ( ( (rect.width*rect.height) > minarea ) && ( (rect.width*rect.height) < maxarea ) ) { + cvRectangle( rgb, cvPoint(rect.x,rect.y), cvPoint(rect.x+rect.width,rect.y+rect.height), CV_RGB(255,0,0), 2, 8 , 0 ); + + t_atom rlist[4]; + SETFLOAT(&rlist[0], i); + 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++; + } + + } + + cvReleaseMemStorage( &stor02 ); + + + //cvShowImage(wndname, cedge); + memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 ); +} + +void pix_opencv_contours_boundingrect :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_contours_boundingrect :: 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(&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); + cnt_img = 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 ); + + + CvSeq* contours; + CvMemStorage* stor02; + stor02 = cvCreateMemStorage(0); + + cvFindContours( gray, stor02, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); + if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 ); + + + 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; + + rect = cvContourBoundingRect( contours, 1); + if ( ( (rect.width*rect.height) > minarea ) && ( (rect.width*rect.height) < maxarea ) ) { + cvRectangle( gray, cvPoint(rect.x,rect.y), cvPoint(rect.x+rect.width,rect.y+rect.height), CV_RGB(255,0,0), 2, 8 , 0 ); + + t_atom rlist[4]; + SETFLOAT(&rlist[0], i); + 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++; + } + + } + + cvReleaseMemStorage( &stor02 ); + + + //copy back the processed frame to image + memcpy( image.data, gray->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; +} + +///////////////////////////////////////////////////////// +// 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); +} +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); +} diff --git a/pix_opencv_contours_boundingrect.cpp b/pix_opencv_contours_boundingrect.cpp deleted file mode 100644 index c6ebd94..0000000 --- a/pix_opencv_contours_boundingrect.cpp +++ /dev/null @@ -1,305 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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" - -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); - minarea = 1; - maxarea = 320*240; - comp_xsize = 0; - comp_ysize = 0; - orig = NULL; - gray = NULL; - cnt_img = NULL; - rgb = NULL; - -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_contours_boundingrect :: ~pix_opencv_contours_boundingrect() -{ - //Destroy cv_images to clean memory - cvReleaseImage(&orig); - cvReleaseImage(&gray); - cvReleaseImage(&cnt_img); - cvReleaseImage(&rgb); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_contours_boundingrect :: 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(&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); - cnt_img = 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); - - CvSeq* contours; - CvMemStorage* stor02; - stor02 = cvCreateMemStorage(0); - - cvFindContours( gray, stor02, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); - if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 ); - - - 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; - - rect = cvContourBoundingRect( contours, 1); - if ( ( (rect.width*rect.height) > minarea ) && ( (rect.width*rect.height) < maxarea ) ) { - cvRectangle( orig, cvPoint(rect.x,rect.y), cvPoint(rect.x+rect.width,rect.y+rect.height), CV_RGB(255,0,0), 2, 8 , 0 ); - - t_atom rlist[4]; - SETFLOAT(&rlist[0], i); - 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++; - } - - } - - 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; - - 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); - - // Create the output images with new sizes - cnt_img = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 3); - - 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* contours; - CvMemStorage* stor02; - stor02 = cvCreateMemStorage(0); - - cvFindContours( gray, stor02, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); - if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 ); - - - 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; - - rect = cvContourBoundingRect( contours, 1); - if ( ( (rect.width*rect.height) > minarea ) && ( (rect.width*rect.height) < maxarea ) ) { - cvRectangle( rgb, cvPoint(rect.x,rect.y), cvPoint(rect.x+rect.width,rect.y+rect.height), CV_RGB(255,0,0), 2, 8 , 0 ); - - t_atom rlist[4]; - SETFLOAT(&rlist[0], i); - 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++; - } - - } - - cvReleaseMemStorage( &stor02 ); - - - //cvShowImage(wndname, cedge); - memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 ); -} - -void pix_opencv_contours_boundingrect :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_contours_boundingrect :: 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(&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); - cnt_img = 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 ); - - - CvSeq* contours; - CvMemStorage* stor02; - stor02 = cvCreateMemStorage(0); - - cvFindContours( gray, stor02, &contours, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) ); - if (contours) contours = cvApproxPoly( contours, sizeof(CvContour), stor02, CV_POLY_APPROX_DP, 3, 1 ); - - - 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; - - rect = cvContourBoundingRect( contours, 1); - if ( ( (rect.width*rect.height) > minarea ) && ( (rect.width*rect.height) < maxarea ) ) { - cvRectangle( gray, cvPoint(rect.x,rect.y), cvPoint(rect.x+rect.width,rect.y+rect.height), CV_RGB(255,0,0), 2, 8 , 0 ); - - t_atom rlist[4]; - SETFLOAT(&rlist[0], i); - 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++; - } - - } - - cvReleaseMemStorage( &stor02 ); - - - //copy back the processed frame to image - memcpy( image.data, gray->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; -} - -///////////////////////////////////////////////////////// -// 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); -} -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); -} diff --git a/pix_opencv_contours_convexity.cc b/pix_opencv_contours_convexity.cc new file mode 100644 index 0000000..982fd3d --- /dev/null +++ b/pix_opencv_contours_convexity.cc @@ -0,0 +1,684 @@ +//////////////////////////////////////////////////////// +// +// 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_convexity.h" + +CPPEXTERN_NEW(pix_opencv_contours_convexity) + +///////////////////////////////////////////////////////// +// +// pix_opencv_contours_convexity +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_contours_convexity :: pix_opencv_contours_convexity() +{ + //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_convexity :: ~pix_opencv_contours_convexity() +{ + //Destroy cv_images to clean memory + cvReleaseImage(&orig); + cvReleaseImage(&gray); + cvReleaseImage(&rgb); +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_contours_convexity :: 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); + + // This required for cvConvexityDefects(). + // Otherwise cvConvexityDefects() falled. + if( hullsize < 4 ) + continue; + + // Find defects of convexity of current contours. + //fprintf(stderr,"cvConvexityDefects\n"); + defects = cvConvexityDefects( contours, + seqhull, + stor03); + int j=0; + // This cycle marks all defects of convexity of current contours. + for(;defects;defects = defects->h_next) + { + int nomdef = defects->total; // defect amount + outlet_float( m_nomdef, nomdef ); + + if(nomdef == 0) + continue; + + // Alloc memory for defect set. + //fprintf(stderr,"malloc\n"); + defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef); + + // Get defect set. + //fprintf(stderr,"cvCvtSeqToArray\n"); + cvCvtSeqToArray(defects,defectArray, CV_WHOLE_SEQ); + + + // Draw marks for all defects. + for(i=0; ix); + SETFLOAT(&rlist[2], defectArray[i].start->y); + SETFLOAT(&rlist[3], defectArray[i].depth_point->x); + SETFLOAT(&rlist[4], defectArray[i].depth_point->y); + SETFLOAT(&rlist[5], defectArray[i].end->x); + SETFLOAT(&rlist[6], defectArray[i].end->y); + outlet_list( m_dataout, 0, 7, rlist ); + } + + j++; + + // Free memory. + free(defectArray); + } + + // 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) ); + + // Draw convex hull for current contour. + for(i=0; icnt_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_convexity :: 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); + + // This required for cvConvexityDefects(). + // Otherwise cvConvexityDefects() falled. + if( hullsize < 4 ) + continue; + + // Find defects of convexity of current contours. + //fprintf(stderr,"cvConvexityDefects\n"); + defects = cvConvexityDefects( contours, + seqhull, + stor03); + int j=0; + // This cycle marks all defects of convexity of current contours. + for(;defects;defects = defects->h_next) + { + int nomdef = defects->total; // defect amount + outlet_float( m_nomdef, nomdef ); + + if(nomdef == 0) + continue; + + // Alloc memory for defect set. + //fprintf(stderr,"malloc\n"); + defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef); + + // Get defect set. + //fprintf(stderr,"cvCvtSeqToArray\n"); + cvCvtSeqToArray(defects,defectArray, CV_WHOLE_SEQ); + + + // Draw marks for all defects. + for(i=0; ix); + SETFLOAT(&rlist[2], defectArray[i].start->y); + SETFLOAT(&rlist[3], defectArray[i].depth_point->x); + SETFLOAT(&rlist[4], defectArray[i].depth_point->y); + SETFLOAT(&rlist[5], defectArray[i].end->x); + SETFLOAT(&rlist[6], defectArray[i].end->y); + outlet_list( m_dataout, 0, 7, rlist ); + } + + j++; + + // Free memory. + free(defectArray); + } + + // 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) ); + + // Draw convex hull for current contour. + for(i=0; icnt_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_convexity :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_contours_convexity :: 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); + + // This required for cvConvexityDefects(). + // Otherwise cvConvexityDefects() falled. + if( hullsize < 4 ) + continue; + + // Find defects of convexity of current contours. + //fprintf(stderr,"cvConvexityDefects\n"); + defects = cvConvexityDefects( contours, + seqhull, + stor03); + int j=0; + // This cycle marks all defects of convexity of current contours. + for(;defects;defects = defects->h_next) + { + int nomdef = defects->total; // defect amount + outlet_float( m_nomdef, nomdef ); + + if(nomdef == 0) + continue; + + // Alloc memory for defect set. + //fprintf(stderr,"malloc\n"); + defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef); + + // Get defect set. + //fprintf(stderr,"cvCvtSeqToArray\n"); + cvCvtSeqToArray(defects,defectArray, CV_WHOLE_SEQ); + + + // Draw marks for all defects. + for(i=0; ix); + SETFLOAT(&rlist[2], defectArray[i].start->y); + SETFLOAT(&rlist[3], defectArray[i].depth_point->x); + SETFLOAT(&rlist[4], defectArray[i].depth_point->y); + SETFLOAT(&rlist[5], defectArray[i].end->x); + SETFLOAT(&rlist[6], defectArray[i].end->y); + outlet_list( m_dataout, 0, 7, rlist ); + } + + j++; + + // Free memory. + free(defectArray); + } + + // 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) ); + + // Draw convex hull for current contour. + for(i=0; icnt_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_convexity :: floatMinAreaMess (float minarea) +{ + if (minarea>0) this->minarea = (int)minarea; +} +void pix_opencv_contours_convexity :: floatMaxAreaMess (float maxarea) +{ + if (maxarea>0) this->maxarea = (int)maxarea; +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_contours_convexity :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_contours_convexity::floatMinAreaMessCallback, + gensym("minarea"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_contours_convexity::floatMaxAreaMessCallback, + gensym("maxarea"), A_FLOAT, A_NULL); +} +void pix_opencv_contours_convexity :: floatMaxAreaMessCallback(void *data, t_floatarg maxarea) +{ + GetMyClass(data)->floatMaxAreaMess((float)maxarea); +} +void pix_opencv_contours_convexity :: floatMinAreaMessCallback(void *data, t_floatarg minarea) +{ + GetMyClass(data)->floatMinAreaMess((float)minarea); +} diff --git a/pix_opencv_contours_convexity.cpp b/pix_opencv_contours_convexity.cpp deleted file mode 100644 index 982fd3d..0000000 --- a/pix_opencv_contours_convexity.cpp +++ /dev/null @@ -1,684 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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_convexity.h" - -CPPEXTERN_NEW(pix_opencv_contours_convexity) - -///////////////////////////////////////////////////////// -// -// pix_opencv_contours_convexity -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_contours_convexity :: pix_opencv_contours_convexity() -{ - //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_convexity :: ~pix_opencv_contours_convexity() -{ - //Destroy cv_images to clean memory - cvReleaseImage(&orig); - cvReleaseImage(&gray); - cvReleaseImage(&rgb); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_contours_convexity :: 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); - - // This required for cvConvexityDefects(). - // Otherwise cvConvexityDefects() falled. - if( hullsize < 4 ) - continue; - - // Find defects of convexity of current contours. - //fprintf(stderr,"cvConvexityDefects\n"); - defects = cvConvexityDefects( contours, - seqhull, - stor03); - int j=0; - // This cycle marks all defects of convexity of current contours. - for(;defects;defects = defects->h_next) - { - int nomdef = defects->total; // defect amount - outlet_float( m_nomdef, nomdef ); - - if(nomdef == 0) - continue; - - // Alloc memory for defect set. - //fprintf(stderr,"malloc\n"); - defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef); - - // Get defect set. - //fprintf(stderr,"cvCvtSeqToArray\n"); - cvCvtSeqToArray(defects,defectArray, CV_WHOLE_SEQ); - - - // Draw marks for all defects. - for(i=0; ix); - SETFLOAT(&rlist[2], defectArray[i].start->y); - SETFLOAT(&rlist[3], defectArray[i].depth_point->x); - SETFLOAT(&rlist[4], defectArray[i].depth_point->y); - SETFLOAT(&rlist[5], defectArray[i].end->x); - SETFLOAT(&rlist[6], defectArray[i].end->y); - outlet_list( m_dataout, 0, 7, rlist ); - } - - j++; - - // Free memory. - free(defectArray); - } - - // 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) ); - - // Draw convex hull for current contour. - for(i=0; icnt_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_convexity :: 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); - - // This required for cvConvexityDefects(). - // Otherwise cvConvexityDefects() falled. - if( hullsize < 4 ) - continue; - - // Find defects of convexity of current contours. - //fprintf(stderr,"cvConvexityDefects\n"); - defects = cvConvexityDefects( contours, - seqhull, - stor03); - int j=0; - // This cycle marks all defects of convexity of current contours. - for(;defects;defects = defects->h_next) - { - int nomdef = defects->total; // defect amount - outlet_float( m_nomdef, nomdef ); - - if(nomdef == 0) - continue; - - // Alloc memory for defect set. - //fprintf(stderr,"malloc\n"); - defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef); - - // Get defect set. - //fprintf(stderr,"cvCvtSeqToArray\n"); - cvCvtSeqToArray(defects,defectArray, CV_WHOLE_SEQ); - - - // Draw marks for all defects. - for(i=0; ix); - SETFLOAT(&rlist[2], defectArray[i].start->y); - SETFLOAT(&rlist[3], defectArray[i].depth_point->x); - SETFLOAT(&rlist[4], defectArray[i].depth_point->y); - SETFLOAT(&rlist[5], defectArray[i].end->x); - SETFLOAT(&rlist[6], defectArray[i].end->y); - outlet_list( m_dataout, 0, 7, rlist ); - } - - j++; - - // Free memory. - free(defectArray); - } - - // 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) ); - - // Draw convex hull for current contour. - for(i=0; icnt_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_convexity :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_contours_convexity :: 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); - - // This required for cvConvexityDefects(). - // Otherwise cvConvexityDefects() falled. - if( hullsize < 4 ) - continue; - - // Find defects of convexity of current contours. - //fprintf(stderr,"cvConvexityDefects\n"); - defects = cvConvexityDefects( contours, - seqhull, - stor03); - int j=0; - // This cycle marks all defects of convexity of current contours. - for(;defects;defects = defects->h_next) - { - int nomdef = defects->total; // defect amount - outlet_float( m_nomdef, nomdef ); - - if(nomdef == 0) - continue; - - // Alloc memory for defect set. - //fprintf(stderr,"malloc\n"); - defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*nomdef); - - // Get defect set. - //fprintf(stderr,"cvCvtSeqToArray\n"); - cvCvtSeqToArray(defects,defectArray, CV_WHOLE_SEQ); - - - // Draw marks for all defects. - for(i=0; ix); - SETFLOAT(&rlist[2], defectArray[i].start->y); - SETFLOAT(&rlist[3], defectArray[i].depth_point->x); - SETFLOAT(&rlist[4], defectArray[i].depth_point->y); - SETFLOAT(&rlist[5], defectArray[i].end->x); - SETFLOAT(&rlist[6], defectArray[i].end->y); - outlet_list( m_dataout, 0, 7, rlist ); - } - - j++; - - // Free memory. - free(defectArray); - } - - // 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) ); - - // Draw convex hull for current contour. - for(i=0; icnt_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_convexity :: floatMinAreaMess (float minarea) -{ - if (minarea>0) this->minarea = (int)minarea; -} -void pix_opencv_contours_convexity :: floatMaxAreaMess (float maxarea) -{ - if (maxarea>0) this->maxarea = (int)maxarea; -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_contours_convexity :: obj_setupCallback(t_class *classPtr) -{ - class_addmethod(classPtr, (t_method)&pix_opencv_contours_convexity::floatMinAreaMessCallback, - gensym("minarea"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_contours_convexity::floatMaxAreaMessCallback, - gensym("maxarea"), A_FLOAT, A_NULL); -} -void pix_opencv_contours_convexity :: floatMaxAreaMessCallback(void *data, t_floatarg maxarea) -{ - GetMyClass(data)->floatMaxAreaMess((float)maxarea); -} -void pix_opencv_contours_convexity :: floatMinAreaMessCallback(void *data, t_floatarg minarea) -{ - GetMyClass(data)->floatMinAreaMess((float)minarea); -} diff --git a/pix_opencv_distrans.cc b/pix_opencv_distrans.cc new file mode 100644 index 0000000..c9e4cdf --- /dev/null +++ b/pix_opencv_distrans.cc @@ -0,0 +1,428 @@ +//////////////////////////////////////////////////////// +// +// 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_distrans.h" + +CPPEXTERN_NEW(pix_opencv_distrans) + +///////////////////////////////////////////////////////// +// +// pix_opencv_distrans +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_distrans :: pix_opencv_distrans() +{ + int i; + + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); + + edge_thresh = 25; + build_voronoi = 0; + comp_xsize = 0; + comp_ysize = 0; + + dist = NULL; + dist8u1 = NULL; + dist8u2 = NULL; + dist8u = NULL; + dist32s = NULL; + src = NULL; + gray = NULL; + edge = NULL; + labels = NULL; + rgba = NULL; + alpha = NULL; + + mask_size = CV_DIST_MASK_PRECISE; + +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_distrans :: ~pix_opencv_distrans() +{ + //Destroy cv_images to clean memory + cvReleaseImage( &src ); + cvReleaseImage( &gray ); + cvReleaseImage( &edge ); + cvReleaseImage( &dist ); + cvReleaseImage( &dist8u ); + cvReleaseImage( &dist8u1 ); + cvReleaseImage( &dist8u2 ); + cvReleaseImage( &dist32s ); + cvReleaseImage( &labels ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_distrans :: processRGBAImage(imageStruct &image) +{ + unsigned char *pixels = image.data; + int i; + static const uchar colors[][3] = + { + {0,0,0}, + {255,0,0}, + {255,128,0}, + {255,255,0}, + {0,255,0}, + {0,128,255}, + {0,255,255}, + {0,0,255}, + {255,0,255} + }; + int msize = mask_size; + + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage( &src ); + cvReleaseImage( &gray ); + cvReleaseImage( &edge ); + cvReleaseImage( &dist ); + cvReleaseImage( &dist8u ); + cvReleaseImage( &dist8u1 ); + cvReleaseImage( &dist8u2 ); + cvReleaseImage( &dist32s ); + cvReleaseImage( &labels ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); + + //Create cv_images + src = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); + gray = cvCreateImage(cvSize(src->width,src->height), IPL_DEPTH_8U, 1); + dist = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32F, 1 ); + dist8u1 = cvCloneImage( gray ); + dist8u2 = cvCloneImage( gray ); + dist8u = cvCreateImage( cvGetSize(gray), IPL_DEPTH_8U, 3 ); + dist32s = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); + edge = cvCloneImage( gray ); + labels = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); + rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); + alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); + + CvArr* in[] = { rgba }; + CvArr* out[] = { src, alpha }; + int from_to[] = { 0, 0, 1, 1, 2, 2, 3, 3 }; + //cvSet( rgba, cvScalar(1,2,3,4) ); + cvMixChannels( (const CvArr**)in, 1, out, 2, from_to, 4 ); + + cvCvtColor(src, gray, CV_BGR2GRAY); + + cvThreshold( gray, edge, (float)edge_thresh, (float)edge_thresh, CV_THRESH_BINARY ); + + if( build_voronoi ) + msize = CV_DIST_MASK_5; + + cvDistTransform( edge, dist, CV_DIST_L2, msize, NULL, build_voronoi ? labels : NULL ); + + if( !build_voronoi ) + { + // begin "painting" the distance transform result + cvConvertScale( dist, dist, 5000.0, 0 ); + cvPow( dist, dist, 0.5 ); + + cvConvertScale( dist, dist32s, 1.0, 0.5 ); + cvAndS( dist32s, cvScalarAll(255), dist32s, 0 ); + cvConvertScale( dist32s, dist8u1, 1, 0 ); + cvConvertScale( dist32s, dist32s, -1, 0 ); + cvAddS( dist32s, cvScalarAll(255), dist32s, 0 ); + cvConvertScale( dist32s, dist8u2, 1, 0 ); + cvMerge( dist8u1, dist8u2, dist8u2, 0, dist8u ); + // end "painting" the distance transform result + } + else + { + int i, j; + for( i = 0; i < labels->height; i++ ) + { + int* ll = (int*)(labels->imageData + i*labels->widthStep); + float* dd = (float*)(dist->imageData + i*dist->widthStep); + uchar* d = (uchar*)(dist8u->imageData + i*dist8u->widthStep); + for( j = 0; j < labels->width; j++ ) + { + int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1; + int b = cvRound(colors[idx][0]); + int g = cvRound(colors[idx][1]); + int r = cvRound(colors[idx][2]); + d[j*3] = (uchar)b; + d[j*3+1] = (uchar)g; + d[j*3+2] = (uchar)r; + } + } + } + + + CvArr* src[] = { dist8u, alpha }; + CvArr* dst[] = { rgba }; + cvMixChannels( (const CvArr**)src, 2, (CvArr**)dst, 1, from_to, 4 ); + //cvShowImage(wndname, cedge); + memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); +} + +void pix_opencv_distrans :: processRGBImage(imageStruct &image) +{ + unsigned char *pixels = image.data; + int i; + static const uchar colors[][3] = + { + {0,0,0}, + {255,0,0}, + {255,128,0}, + {255,255,0}, + {0,255,0}, + {0,128,255}, + {0,255,255}, + {0,0,255}, + {255,0,255} + }; + int msize = mask_size; + + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage( &src ); + cvReleaseImage( &gray ); + cvReleaseImage( &edge ); + cvReleaseImage( &dist ); + cvReleaseImage( &dist8u ); + cvReleaseImage( &dist8u1 ); + cvReleaseImage( &dist8u2 ); + cvReleaseImage( &dist32s ); + cvReleaseImage( &labels ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); + + //Create cv_images + src = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); + gray = cvCreateImage(cvSize(src->width,src->height), IPL_DEPTH_8U, 1); + dist = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32F, 1 ); + dist8u1 = cvCloneImage( gray ); + dist8u2 = cvCloneImage( gray ); + dist8u = cvCreateImage( cvGetSize(gray), IPL_DEPTH_8U, 3 ); + dist32s = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); + edge = cvCloneImage( gray ); + labels = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); + rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); + alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( src->imageData, image.data, image.xsize*image.ysize*3 ); + + cvCvtColor(src, gray, CV_BGR2GRAY); + + cvThreshold( gray, edge, (float)edge_thresh, (float)edge_thresh, CV_THRESH_BINARY ); + + if( build_voronoi ) + msize = CV_DIST_MASK_5; + + cvDistTransform( edge, dist, CV_DIST_L2, msize, NULL, build_voronoi ? labels : NULL ); + + if( !build_voronoi ) + { + // begin "painting" the distance transform result + cvConvertScale( dist, dist, 5000.0, 0 ); + cvPow( dist, dist, 0.5 ); + + cvConvertScale( dist, dist32s, 1.0, 0.5 ); + cvAndS( dist32s, cvScalarAll(255), dist32s, 0 ); + cvConvertScale( dist32s, dist8u1, 1, 0 ); + cvConvertScale( dist32s, dist32s, -1, 0 ); + cvAddS( dist32s, cvScalarAll(255), dist32s, 0 ); + cvConvertScale( dist32s, dist8u2, 1, 0 ); + cvMerge( dist8u1, dist8u2, dist8u2, 0, dist8u ); + // end "painting" the distance transform result + } + else + { + int i, j; + for( i = 0; i < labels->height; i++ ) + { + int* ll = (int*)(labels->imageData + i*labels->widthStep); + float* dd = (float*)(dist->imageData + i*dist->widthStep); + uchar* d = (uchar*)(dist8u->imageData + i*dist8u->widthStep); + for( j = 0; j < labels->width; j++ ) + { + int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1; + int b = cvRound(colors[idx][0]); + int g = cvRound(colors[idx][1]); + int r = cvRound(colors[idx][2]); + d[j*3] = (uchar)b; + d[j*3+1] = (uchar)g; + d[j*3+2] = (uchar)r; + } + } + } + + //cvShowImage(wndname, cedge); + memcpy( image.data, dist8u->imageData, image.xsize*image.ysize*3 ); +} + +void pix_opencv_distrans :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_distrans :: processGrayImage(imageStruct &image) +{ + unsigned char *pixels = image.data; + int i; + static const uchar colors[][3] = + { + {0,0,0}, + {255,0,0}, + {255,128,0}, + {255,255,0}, + {0,255,0}, + {0,128,255}, + {0,255,255}, + {0,0,255}, + {255,0,255} + }; + int msize = mask_size; + + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage( &src ); + cvReleaseImage( &gray ); + cvReleaseImage( &edge ); + cvReleaseImage( &dist ); + cvReleaseImage( &dist8u ); + cvReleaseImage( &dist8u1 ); + cvReleaseImage( &dist8u2 ); + cvReleaseImage( &dist32s ); + cvReleaseImage( &labels ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); + + //Create cv_images + src = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); + gray = cvCreateImage(cvSize(src->width,src->height), IPL_DEPTH_8U, 1); + dist = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32F, 1 ); + dist8u1 = cvCloneImage( gray ); + dist8u2 = cvCloneImage( gray ); + dist8u = cvCreateImage( cvGetSize(gray), IPL_DEPTH_8U, 3 ); + dist32s = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); + edge = cvCloneImage( gray ); + labels = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); + rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); + alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( gray->imageData, image.data, image.xsize*image.ysize ); + + + cvThreshold( gray, edge, (float)edge_thresh, (float)edge_thresh, CV_THRESH_BINARY ); + + if( build_voronoi ) + msize = CV_DIST_MASK_5; + + cvDistTransform( edge, dist, CV_DIST_L2, msize, NULL, build_voronoi ? labels : NULL ); + + if( !build_voronoi ) + { + // begin "painting" the distance transform result + cvConvertScale( dist, dist, 5000.0, 0 ); + cvPow( dist, dist, 0.5 ); + + cvConvertScale( dist, dist32s, 1.0, 0.5 ); + cvAndS( dist32s, cvScalarAll(255), dist32s, 0 ); + cvConvertScale( dist32s, dist8u1, 1, 0 ); + cvConvertScale( dist32s, dist32s, -1, 0 ); + cvAddS( dist32s, cvScalarAll(255), dist32s, 0 ); + cvConvertScale( dist32s, dist8u2, 1, 0 ); + cvMerge( dist8u1, dist8u2, dist8u2, 0, dist8u ); + // end "painting" the distance transform result + } + else + { + int i, j; + for( i = 0; i < labels->height; i++ ) + { + int* ll = (int*)(labels->imageData + i*labels->widthStep); + float* dd = (float*)(dist->imageData + i*dist->widthStep); + uchar* d = (uchar*)(dist8u->imageData + i*dist8u->widthStep); + for( j = 0; j < labels->width; j++ ) + { + int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1; + int b = cvRound(colors[idx][0]); + int g = cvRound(colors[idx][1]); + int r = cvRound(colors[idx][2]); + d[j*3] = (uchar)b; + d[j*3+1] = (uchar)g; + d[j*3+2] = (uchar)r; + } + } + } + + + cvCvtColor(dist8u, gray, CV_RGB2GRAY); + //cvShowImage(wndname, cedge); + memcpy( image.data, gray->imageData, image.xsize*image.ysize ); +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_distrans :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_distrans::thresholdMessCallback, + gensym("ft1"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_distrans::voronoiMessCallback, + gensym("voronoi"), A_DEFFLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_distrans::maskMessCallback, + gensym("mask"), A_DEFFLOAT, A_NULL); +} +void pix_opencv_distrans :: thresholdMessCallback(void *data, t_floatarg pos) +{ + if (pos>=0) GetMyClass(data)->edge_thresh = (int)pos; +} +void pix_opencv_distrans :: voronoiMessCallback(void *data, t_floatarg voronoi) +{ + GetMyClass(data)->build_voronoi=!(!(int)voronoi); +} +void pix_opencv_distrans :: maskMessCallback(void *data, t_floatarg f) +{ + if( (int)f == 3 ) + GetMyClass(data)->mask_size = CV_DIST_MASK_3; + else if( (int)f == 5 ) + GetMyClass(data)->mask_size = CV_DIST_MASK_5; + else if( (int)f == 0 ) + GetMyClass(data)->mask_size = CV_DIST_MASK_PRECISE; +} diff --git a/pix_opencv_distrans.cpp b/pix_opencv_distrans.cpp deleted file mode 100644 index c9e4cdf..0000000 --- a/pix_opencv_distrans.cpp +++ /dev/null @@ -1,428 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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_distrans.h" - -CPPEXTERN_NEW(pix_opencv_distrans) - -///////////////////////////////////////////////////////// -// -// pix_opencv_distrans -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_distrans :: pix_opencv_distrans() -{ - int i; - - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); - - edge_thresh = 25; - build_voronoi = 0; - comp_xsize = 0; - comp_ysize = 0; - - dist = NULL; - dist8u1 = NULL; - dist8u2 = NULL; - dist8u = NULL; - dist32s = NULL; - src = NULL; - gray = NULL; - edge = NULL; - labels = NULL; - rgba = NULL; - alpha = NULL; - - mask_size = CV_DIST_MASK_PRECISE; - -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_distrans :: ~pix_opencv_distrans() -{ - //Destroy cv_images to clean memory - cvReleaseImage( &src ); - cvReleaseImage( &gray ); - cvReleaseImage( &edge ); - cvReleaseImage( &dist ); - cvReleaseImage( &dist8u ); - cvReleaseImage( &dist8u1 ); - cvReleaseImage( &dist8u2 ); - cvReleaseImage( &dist32s ); - cvReleaseImage( &labels ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_distrans :: processRGBAImage(imageStruct &image) -{ - unsigned char *pixels = image.data; - int i; - static const uchar colors[][3] = - { - {0,0,0}, - {255,0,0}, - {255,128,0}, - {255,255,0}, - {0,255,0}, - {0,128,255}, - {0,255,255}, - {0,0,255}, - {255,0,255} - }; - int msize = mask_size; - - if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage( &src ); - cvReleaseImage( &gray ); - cvReleaseImage( &edge ); - cvReleaseImage( &dist ); - cvReleaseImage( &dist8u ); - cvReleaseImage( &dist8u1 ); - cvReleaseImage( &dist8u2 ); - cvReleaseImage( &dist32s ); - cvReleaseImage( &labels ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); - - //Create cv_images - src = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); - gray = cvCreateImage(cvSize(src->width,src->height), IPL_DEPTH_8U, 1); - dist = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32F, 1 ); - dist8u1 = cvCloneImage( gray ); - dist8u2 = cvCloneImage( gray ); - dist8u = cvCreateImage( cvGetSize(gray), IPL_DEPTH_8U, 3 ); - dist32s = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); - edge = cvCloneImage( gray ); - labels = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); - rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); - alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); - - CvArr* in[] = { rgba }; - CvArr* out[] = { src, alpha }; - int from_to[] = { 0, 0, 1, 1, 2, 2, 3, 3 }; - //cvSet( rgba, cvScalar(1,2,3,4) ); - cvMixChannels( (const CvArr**)in, 1, out, 2, from_to, 4 ); - - cvCvtColor(src, gray, CV_BGR2GRAY); - - cvThreshold( gray, edge, (float)edge_thresh, (float)edge_thresh, CV_THRESH_BINARY ); - - if( build_voronoi ) - msize = CV_DIST_MASK_5; - - cvDistTransform( edge, dist, CV_DIST_L2, msize, NULL, build_voronoi ? labels : NULL ); - - if( !build_voronoi ) - { - // begin "painting" the distance transform result - cvConvertScale( dist, dist, 5000.0, 0 ); - cvPow( dist, dist, 0.5 ); - - cvConvertScale( dist, dist32s, 1.0, 0.5 ); - cvAndS( dist32s, cvScalarAll(255), dist32s, 0 ); - cvConvertScale( dist32s, dist8u1, 1, 0 ); - cvConvertScale( dist32s, dist32s, -1, 0 ); - cvAddS( dist32s, cvScalarAll(255), dist32s, 0 ); - cvConvertScale( dist32s, dist8u2, 1, 0 ); - cvMerge( dist8u1, dist8u2, dist8u2, 0, dist8u ); - // end "painting" the distance transform result - } - else - { - int i, j; - for( i = 0; i < labels->height; i++ ) - { - int* ll = (int*)(labels->imageData + i*labels->widthStep); - float* dd = (float*)(dist->imageData + i*dist->widthStep); - uchar* d = (uchar*)(dist8u->imageData + i*dist8u->widthStep); - for( j = 0; j < labels->width; j++ ) - { - int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1; - int b = cvRound(colors[idx][0]); - int g = cvRound(colors[idx][1]); - int r = cvRound(colors[idx][2]); - d[j*3] = (uchar)b; - d[j*3+1] = (uchar)g; - d[j*3+2] = (uchar)r; - } - } - } - - - CvArr* src[] = { dist8u, alpha }; - CvArr* dst[] = { rgba }; - cvMixChannels( (const CvArr**)src, 2, (CvArr**)dst, 1, from_to, 4 ); - //cvShowImage(wndname, cedge); - memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); -} - -void pix_opencv_distrans :: processRGBImage(imageStruct &image) -{ - unsigned char *pixels = image.data; - int i; - static const uchar colors[][3] = - { - {0,0,0}, - {255,0,0}, - {255,128,0}, - {255,255,0}, - {0,255,0}, - {0,128,255}, - {0,255,255}, - {0,0,255}, - {255,0,255} - }; - int msize = mask_size; - - if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage( &src ); - cvReleaseImage( &gray ); - cvReleaseImage( &edge ); - cvReleaseImage( &dist ); - cvReleaseImage( &dist8u ); - cvReleaseImage( &dist8u1 ); - cvReleaseImage( &dist8u2 ); - cvReleaseImage( &dist32s ); - cvReleaseImage( &labels ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); - - //Create cv_images - src = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); - gray = cvCreateImage(cvSize(src->width,src->height), IPL_DEPTH_8U, 1); - dist = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32F, 1 ); - dist8u1 = cvCloneImage( gray ); - dist8u2 = cvCloneImage( gray ); - dist8u = cvCreateImage( cvGetSize(gray), IPL_DEPTH_8U, 3 ); - dist32s = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); - edge = cvCloneImage( gray ); - labels = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); - rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); - alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( src->imageData, image.data, image.xsize*image.ysize*3 ); - - cvCvtColor(src, gray, CV_BGR2GRAY); - - cvThreshold( gray, edge, (float)edge_thresh, (float)edge_thresh, CV_THRESH_BINARY ); - - if( build_voronoi ) - msize = CV_DIST_MASK_5; - - cvDistTransform( edge, dist, CV_DIST_L2, msize, NULL, build_voronoi ? labels : NULL ); - - if( !build_voronoi ) - { - // begin "painting" the distance transform result - cvConvertScale( dist, dist, 5000.0, 0 ); - cvPow( dist, dist, 0.5 ); - - cvConvertScale( dist, dist32s, 1.0, 0.5 ); - cvAndS( dist32s, cvScalarAll(255), dist32s, 0 ); - cvConvertScale( dist32s, dist8u1, 1, 0 ); - cvConvertScale( dist32s, dist32s, -1, 0 ); - cvAddS( dist32s, cvScalarAll(255), dist32s, 0 ); - cvConvertScale( dist32s, dist8u2, 1, 0 ); - cvMerge( dist8u1, dist8u2, dist8u2, 0, dist8u ); - // end "painting" the distance transform result - } - else - { - int i, j; - for( i = 0; i < labels->height; i++ ) - { - int* ll = (int*)(labels->imageData + i*labels->widthStep); - float* dd = (float*)(dist->imageData + i*dist->widthStep); - uchar* d = (uchar*)(dist8u->imageData + i*dist8u->widthStep); - for( j = 0; j < labels->width; j++ ) - { - int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1; - int b = cvRound(colors[idx][0]); - int g = cvRound(colors[idx][1]); - int r = cvRound(colors[idx][2]); - d[j*3] = (uchar)b; - d[j*3+1] = (uchar)g; - d[j*3+2] = (uchar)r; - } - } - } - - //cvShowImage(wndname, cedge); - memcpy( image.data, dist8u->imageData, image.xsize*image.ysize*3 ); -} - -void pix_opencv_distrans :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_distrans :: processGrayImage(imageStruct &image) -{ - unsigned char *pixels = image.data; - int i; - static const uchar colors[][3] = - { - {0,0,0}, - {255,0,0}, - {255,128,0}, - {255,255,0}, - {0,255,0}, - {0,128,255}, - {0,255,255}, - {0,0,255}, - {255,0,255} - }; - int msize = mask_size; - - if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage( &src ); - cvReleaseImage( &gray ); - cvReleaseImage( &edge ); - cvReleaseImage( &dist ); - cvReleaseImage( &dist8u ); - cvReleaseImage( &dist8u1 ); - cvReleaseImage( &dist8u2 ); - cvReleaseImage( &dist32s ); - cvReleaseImage( &labels ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); - - //Create cv_images - src = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3); - gray = cvCreateImage(cvSize(src->width,src->height), IPL_DEPTH_8U, 1); - dist = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32F, 1 ); - dist8u1 = cvCloneImage( gray ); - dist8u2 = cvCloneImage( gray ); - dist8u = cvCreateImage( cvGetSize(gray), IPL_DEPTH_8U, 3 ); - dist32s = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); - edge = cvCloneImage( gray ); - labels = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 ); - rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); - alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( gray->imageData, image.data, image.xsize*image.ysize ); - - - cvThreshold( gray, edge, (float)edge_thresh, (float)edge_thresh, CV_THRESH_BINARY ); - - if( build_voronoi ) - msize = CV_DIST_MASK_5; - - cvDistTransform( edge, dist, CV_DIST_L2, msize, NULL, build_voronoi ? labels : NULL ); - - if( !build_voronoi ) - { - // begin "painting" the distance transform result - cvConvertScale( dist, dist, 5000.0, 0 ); - cvPow( dist, dist, 0.5 ); - - cvConvertScale( dist, dist32s, 1.0, 0.5 ); - cvAndS( dist32s, cvScalarAll(255), dist32s, 0 ); - cvConvertScale( dist32s, dist8u1, 1, 0 ); - cvConvertScale( dist32s, dist32s, -1, 0 ); - cvAddS( dist32s, cvScalarAll(255), dist32s, 0 ); - cvConvertScale( dist32s, dist8u2, 1, 0 ); - cvMerge( dist8u1, dist8u2, dist8u2, 0, dist8u ); - // end "painting" the distance transform result - } - else - { - int i, j; - for( i = 0; i < labels->height; i++ ) - { - int* ll = (int*)(labels->imageData + i*labels->widthStep); - float* dd = (float*)(dist->imageData + i*dist->widthStep); - uchar* d = (uchar*)(dist8u->imageData + i*dist8u->widthStep); - for( j = 0; j < labels->width; j++ ) - { - int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1; - int b = cvRound(colors[idx][0]); - int g = cvRound(colors[idx][1]); - int r = cvRound(colors[idx][2]); - d[j*3] = (uchar)b; - d[j*3+1] = (uchar)g; - d[j*3+2] = (uchar)r; - } - } - } - - - cvCvtColor(dist8u, gray, CV_RGB2GRAY); - //cvShowImage(wndname, cedge); - memcpy( image.data, gray->imageData, image.xsize*image.ysize ); -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_distrans :: obj_setupCallback(t_class *classPtr) -{ - class_addmethod(classPtr, (t_method)&pix_opencv_distrans::thresholdMessCallback, - gensym("ft1"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_distrans::voronoiMessCallback, - gensym("voronoi"), A_DEFFLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_distrans::maskMessCallback, - gensym("mask"), A_DEFFLOAT, A_NULL); -} -void pix_opencv_distrans :: thresholdMessCallback(void *data, t_floatarg pos) -{ - if (pos>=0) GetMyClass(data)->edge_thresh = (int)pos; -} -void pix_opencv_distrans :: voronoiMessCallback(void *data, t_floatarg voronoi) -{ - GetMyClass(data)->build_voronoi=!(!(int)voronoi); -} -void pix_opencv_distrans :: maskMessCallback(void *data, t_floatarg f) -{ - if( (int)f == 3 ) - GetMyClass(data)->mask_size = CV_DIST_MASK_3; - else if( (int)f == 5 ) - GetMyClass(data)->mask_size = CV_DIST_MASK_5; - else if( (int)f == 0 ) - GetMyClass(data)->mask_size = CV_DIST_MASK_PRECISE; -} diff --git a/pix_opencv_edge.cc b/pix_opencv_edge.cc new file mode 100644 index 0000000..198d8ee --- /dev/null +++ b/pix_opencv_edge.cc @@ -0,0 +1,233 @@ +//////////////////////////////////////////////////////// +// +// 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_edge.h" + +CPPEXTERN_NEW(pix_opencv_edge) + +///////////////////////////////////////////////////////// +// +// pix_opencv_edge +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_edge :: pix_opencv_edge() +{ + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); + edge_thresh = 50; + comp_xsize = 0; + comp_ysize = 0; + orig = NULL; + gray = NULL; + edge = NULL; + cedge = NULL; + cedgergb = NULL; + rgb = NULL; + +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_edge :: ~pix_opencv_edge() +{ + //Destroy cv_images to clean memory + cvReleaseImage(&orig); + cvReleaseImage(&gray); + cvReleaseImage(&edge); + cvReleaseImage(&cedge); + cvReleaseImage(&cedgergb); + cvReleaseImage(&rgb); +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_edge :: 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(&edge); + cvReleaseImage(&cedge); + cvReleaseImage(&cedgergb); + 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 + cedge = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 4); + + gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); + edge = 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_BGRA2GRAY); + + cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 ); + cvNot( gray, edge ); + + // Run the edge detector on grayscale + cvCanny(gray, edge, (float)this->edge_thresh, (float)this->edge_thresh*3, 3); + + cvZero( cedge ); + // copy edge points + cvCopy( orig, cedge, edge ); + + //copy back the processed frame to image + memcpy( image.data, cedge->imageData, image.xsize*image.ysize*4 ); +} + +void pix_opencv_edge :: 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(&edge); + cvReleaseImage(&cedge); + cvReleaseImage(&cedgergb); + 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 + cedgergb = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 3); + + gray = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 1); + edge = 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); + + cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 ); + cvNot( gray, edge ); + + // Run the edge detector on grayscale + cvCanny(gray, edge, (float)this->edge_thresh, (float)this->edge_thresh*3, 3); + + cvZero( cedgergb ); + // copy edge points + cvCopy( rgb, cedgergb, edge ); + + //cvShowImage(wndname, cedge); + memcpy( image.data, cedgergb->imageData, image.xsize*image.ysize*3 ); +} + +void pix_opencv_edge :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_edge :: processGrayImage(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(&edge); + cvReleaseImage(&cedge); + cvReleaseImage(&cedgergb); + 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 + cedgergb = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 3); + + gray = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 1); + edge = 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( gray->imageData, image.data, image.xsize*image.ysize ); + + // Convert to RGB + cvCvtColor( gray, rgb, CV_GRAY2RGB); + + cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 ); + cvNot( gray, edge ); + + // Run the edge detector on grayscale + cvCanny(gray, edge, (float)this->edge_thresh, (float)this->edge_thresh*3, 3); + + cvZero( cedgergb ); + // copy edge points + cvCopy( rgb, cedgergb, edge ); + + cvCvtColor( cedgergb, gray, CV_RGB2GRAY); + //cvShowImage(wndname, cedge); + memcpy( image.data, gray->imageData, image.xsize*image.ysize ); +} + +///////////////////////////////////////////////////////// +// floatThreshMess +// +///////////////////////////////////////////////////////// +void pix_opencv_edge :: floatThreshMess (float edge_thresh) +{ + this->edge_thresh = (int)edge_thresh; +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_edge :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_edge::floatTreshMessCallback, + gensym("ft1"), A_FLOAT, A_NULL); +} +void pix_opencv_edge :: floatTreshMessCallback(void *data, t_floatarg edge_thresh) +{ + GetMyClass(data)->floatThreshMess((float)edge_thresh); +} diff --git a/pix_opencv_edge.cpp b/pix_opencv_edge.cpp deleted file mode 100644 index 198d8ee..0000000 --- a/pix_opencv_edge.cpp +++ /dev/null @@ -1,233 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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_edge.h" - -CPPEXTERN_NEW(pix_opencv_edge) - -///////////////////////////////////////////////////////// -// -// pix_opencv_edge -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_edge :: pix_opencv_edge() -{ - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); - edge_thresh = 50; - comp_xsize = 0; - comp_ysize = 0; - orig = NULL; - gray = NULL; - edge = NULL; - cedge = NULL; - cedgergb = NULL; - rgb = NULL; - -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_edge :: ~pix_opencv_edge() -{ - //Destroy cv_images to clean memory - cvReleaseImage(&orig); - cvReleaseImage(&gray); - cvReleaseImage(&edge); - cvReleaseImage(&cedge); - cvReleaseImage(&cedgergb); - cvReleaseImage(&rgb); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_edge :: 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(&edge); - cvReleaseImage(&cedge); - cvReleaseImage(&cedgergb); - 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 - cedge = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 4); - - gray = cvCreateImage(cvSize(orig->width,orig->height), IPL_DEPTH_8U, 1); - edge = 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_BGRA2GRAY); - - cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 ); - cvNot( gray, edge ); - - // Run the edge detector on grayscale - cvCanny(gray, edge, (float)this->edge_thresh, (float)this->edge_thresh*3, 3); - - cvZero( cedge ); - // copy edge points - cvCopy( orig, cedge, edge ); - - //copy back the processed frame to image - memcpy( image.data, cedge->imageData, image.xsize*image.ysize*4 ); -} - -void pix_opencv_edge :: 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(&edge); - cvReleaseImage(&cedge); - cvReleaseImage(&cedgergb); - 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 - cedgergb = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 3); - - gray = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 1); - edge = 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); - - cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 ); - cvNot( gray, edge ); - - // Run the edge detector on grayscale - cvCanny(gray, edge, (float)this->edge_thresh, (float)this->edge_thresh*3, 3); - - cvZero( cedgergb ); - // copy edge points - cvCopy( rgb, cedgergb, edge ); - - //cvShowImage(wndname, cedge); - memcpy( image.data, cedgergb->imageData, image.xsize*image.ysize*3 ); -} - -void pix_opencv_edge :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_edge :: processGrayImage(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(&edge); - cvReleaseImage(&cedge); - cvReleaseImage(&cedgergb); - 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 - cedgergb = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 3); - - gray = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 1); - edge = 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( gray->imageData, image.data, image.xsize*image.ysize ); - - // Convert to RGB - cvCvtColor( gray, rgb, CV_GRAY2RGB); - - cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 ); - cvNot( gray, edge ); - - // Run the edge detector on grayscale - cvCanny(gray, edge, (float)this->edge_thresh, (float)this->edge_thresh*3, 3); - - cvZero( cedgergb ); - // copy edge points - cvCopy( rgb, cedgergb, edge ); - - cvCvtColor( cedgergb, gray, CV_RGB2GRAY); - //cvShowImage(wndname, cedge); - memcpy( image.data, gray->imageData, image.xsize*image.ysize ); -} - -///////////////////////////////////////////////////////// -// floatThreshMess -// -///////////////////////////////////////////////////////// -void pix_opencv_edge :: floatThreshMess (float edge_thresh) -{ - this->edge_thresh = (int)edge_thresh; -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_edge :: obj_setupCallback(t_class *classPtr) -{ - class_addmethod(classPtr, (t_method)&pix_opencv_edge::floatTreshMessCallback, - gensym("ft1"), A_FLOAT, A_NULL); -} -void pix_opencv_edge :: floatTreshMessCallback(void *data, t_floatarg edge_thresh) -{ - GetMyClass(data)->floatThreshMess((float)edge_thresh); -} diff --git a/pix_opencv_haarcascade.cc b/pix_opencv_haarcascade.cc new file mode 100644 index 0000000..f446f5b --- /dev/null +++ b/pix_opencv_haarcascade.cc @@ -0,0 +1,353 @@ +//////////////////////////////////////////////////////// +// +// 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_haarcascade.h" + +CPPEXTERN_NEW(pix_opencv_haarcascade) + +///////////////////////////////////////////////////////// +// +// pix_opencv_haarcascade +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_haarcascade :: pix_opencv_haarcascade() +{ + m_dataout = outlet_new(this->x_obj, 0); + + scale_factor = 1.1; + min_neighbors = 2; + mode = 0; + min_size = 30; + + comp_xsize = 0; + comp_ysize = 0; + rgba = NULL; + grey = NULL; + frame = NULL; + + cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 ); + if( !cascade ) + { + post( "ERROR: Could not load classifier cascade\n" ); + } + else post( "Loaded classifier cascade from %s", cascade_name ); + +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_haarcascade :: ~pix_opencv_haarcascade() +{ + //Destroy cv_images to clean memory + cvReleaseImage(&rgba); + cvReleaseImage(&grey); + cvReleaseImage(&frame); +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_haarcascade :: processRGBAImage(imageStruct &image) +{ + double scale = 1; + + if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgba)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage(&rgba); + cvReleaseImage(&grey); + cvReleaseImage(&frame); + + //create the orig image with new size + rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); + frame = cvCreateImage(cvSize(rgba->width,rgba->height), IPL_DEPTH_8U, 3); + grey = cvCreateImage( cvSize(rgba->width,rgba->height), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); + CvMemStorage* storage = cvCreateMemStorage(0); + + static CvScalar colors[] = + { + {{0,0,255}}, + {{0,128,255}}, + {{0,255,255}}, + {{0,255,0}}, + {{255,128,0}}, + {{255,255,0}}, + {{255,0,0}}, + {{255,0,255}} + }; + + int i; + + if( cascade ) + { + CvSeq* faces = cvHaarDetectObjects( rgba, cascade, storage, + scale_factor, min_neighbors, mode, cvSize(min_size, min_size) ); + for( i = 0; i < (faces ? faces->total : 0); i++ ) + { + CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); + CvPoint center; + int radius; + center.x = cvRound((r->x + r->width*0.5)*scale); + center.y = cvRound((r->y + r->height*0.5)*scale); + radius = cvRound((r->width + r->height)*0.25*scale); + cvCircle( rgba, center, radius, colors[i%8], 3, 8, 0 ); + + t_atom rlist[4]; + SETFLOAT(&rlist[0], i); + SETFLOAT(&rlist[1], center.x); + SETFLOAT(&rlist[2], center.y); + SETFLOAT(&rlist[3], radius); + outlet_list( m_dataout, 0, 4, rlist ); + } + } + + + cvReleaseMemStorage( &storage ); + //cvShowImage(wndname, cedge); + memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); +} + +void pix_opencv_haarcascade :: processRGBImage(imageStruct &image) +{ + double scale = 1; + + if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!frame)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage(&rgba); + cvReleaseImage(&grey); + cvReleaseImage(&frame); + + //create the orig image with new size + rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); + frame = cvCreateImage(cvSize(rgba->width,rgba->height), IPL_DEPTH_8U, 3); + grey = cvCreateImage( cvSize(rgba->width,rgba->height), 8, 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( frame->imageData, image.data, image.xsize*image.ysize*3 ); + CvMemStorage* storage = cvCreateMemStorage(0); + + static CvScalar colors[] = + { + {{0,0,255}}, + {{0,128,255}}, + {{0,255,255}}, + {{0,255,0}}, + {{255,128,0}}, + {{255,255,0}}, + {{255,0,0}}, + {{255,0,255}} + }; + + int i; + + if( cascade ) + { + CvSeq* faces = cvHaarDetectObjects( frame, cascade, storage, + 1.1, 2, 0, cvSize(30, 30) ); + for( i = 0; i < (faces ? faces->total : 0); i++ ) + { + CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); + CvPoint center; + int radius; + center.x = cvRound((r->x + r->width*0.5)*scale); + center.y = cvRound((r->y + r->height*0.5)*scale); + radius = cvRound((r->width + r->height)*0.25*scale); + cvCircle( frame, center, radius, colors[i%8], 3, 8, 0 ); + + t_atom rlist[4]; + SETFLOAT(&rlist[0], i); + SETFLOAT(&rlist[1], center.x); + SETFLOAT(&rlist[2], center.y); + SETFLOAT(&rlist[3], radius); + outlet_list( m_dataout, 0, 4, rlist ); + } + } + + + cvReleaseMemStorage( &storage ); + //cvShowImage(wndname, cedge); + memcpy( image.data, frame->imageData, image.xsize*image.ysize*3 ); +} + +void pix_opencv_haarcascade :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_haarcascade :: processGrayImage(imageStruct &image) +{ + double scale = 1; + + if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!grey)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage(&rgba); + cvReleaseImage(&grey); + cvReleaseImage(&frame); + + //create the orig image with new size + rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); + frame = cvCreateImage(cvSize(rgba->width,rgba->height), IPL_DEPTH_8U, 3); + grey = cvCreateImage( cvSize(rgba->width,rgba->height), 8, 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( grey->imageData, image.data, image.xsize*image.ysize ); + CvMemStorage* storage = cvCreateMemStorage(0); + + static CvScalar colors[] = + { + {{0,0,255}}, + {{0,128,255}}, + {{0,255,255}}, + {{0,255,0}}, + {{255,128,0}}, + {{255,255,0}}, + {{255,0,0}}, + {{255,0,255}} + }; + + int i; + + if( cascade ) + { + CvSeq* faces = cvHaarDetectObjects( grey, cascade, storage, + 1.1, 2, 0, cvSize(30, 30) ); + for( i = 0; i < (faces ? faces->total : 0); i++ ) + { + CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); + CvPoint center; + int radius; + center.x = cvRound((r->x + r->width*0.5)*scale); + center.y = cvRound((r->y + r->height*0.5)*scale); + radius = cvRound((r->width + r->height)*0.25*scale); + cvCircle( grey, center, radius, colors[i%8], 3, 8, 0 ); + + t_atom rlist[4]; + SETFLOAT(&rlist[0], i); + SETFLOAT(&rlist[1], center.x); + SETFLOAT(&rlist[2], center.y); + SETFLOAT(&rlist[3], radius); + outlet_list( m_dataout, 0, 4, rlist ); + } + } + + + cvReleaseMemStorage( &storage ); + //cvShowImage(wndname, cedge); + memcpy( image.data, grey->imageData, image.xsize*image.ysize ); +} + +///////////////////////////////////////////////////////// +// scaleFactorMess +// +///////////////////////////////////////////////////////// +void pix_opencv_haarcascade :: scaleFactorMess (float scale_factor) +{ + this->scale_factor = scale_factor; +} + +///////////////////////////////////////////////////////// +// minNeighborsMess +// +///////////////////////////////////////////////////////// +void pix_opencv_haarcascade :: minNeighborsMess (float min_neighbors) +{ + this->min_neighbors = (int)min_neighbors; +} + +///////////////////////////////////////////////////////// +// modeMess +// +///////////////////////////////////////////////////////// +void pix_opencv_haarcascade :: modeMess (float mode) +{ + this->mode = !(!(int)mode); +} + +///////////////////////////////////////////////////////// +// minSizeMess +// +///////////////////////////////////////////////////////// +void pix_opencv_haarcascade :: minSizeMess (float min_size) +{ + this->min_size = (int)min_size; +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_haarcascade :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::scaleFactorMessCallback, + gensym("scale_factor"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::minNeighborsMessCallback, + gensym("min_neighbors"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::modeMessCallback, + gensym("mode"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::minSizeMessCallback, + gensym("min_size"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::loadCascadeMessCallback, + gensym("load"), A_SYMBOL, A_NULL); +} +void pix_opencv_haarcascade :: scaleFactorMessCallback(void *data, t_floatarg scale_factor) +{ + if (scale_factor>1) GetMyClass(data)->scaleFactorMess((float)scale_factor); +} +void pix_opencv_haarcascade :: minNeighborsMessCallback(void *data, t_floatarg min_neighbors) +{ + if (min_neighbors>=1) GetMyClass(data)->minNeighborsMess((float)min_neighbors); +} +void pix_opencv_haarcascade :: modeMessCallback(void *data, t_floatarg mode) +{ + if ((mode==0)||(mode==1)) GetMyClass(data)->modeMess((float)mode); +} +void pix_opencv_haarcascade :: minSizeMessCallback(void *data, t_floatarg min_size) +{ + if (min_size>1) GetMyClass(data)->minSizeMess((float)min_size); +} +void pix_opencv_haarcascade :: loadCascadeMessCallback(void *data, t_symbol* filename) +{ + GetMyClass(data)->loadCascadeMess(filename); +} +void pix_opencv_haarcascade :: loadCascadeMess(t_symbol *filename) +{ + cascade = (CvHaarClassifierCascade*)cvLoad( filename->s_name, 0, 0, 0 ); + if( !cascade ) + { + post( "ERROR: Could not load classifier cascade" ); + } + else post( "Loaded classifier cascade from %s", filename->s_name ); +} diff --git a/pix_opencv_haarcascade.cpp b/pix_opencv_haarcascade.cpp deleted file mode 100644 index f446f5b..0000000 --- a/pix_opencv_haarcascade.cpp +++ /dev/null @@ -1,353 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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_haarcascade.h" - -CPPEXTERN_NEW(pix_opencv_haarcascade) - -///////////////////////////////////////////////////////// -// -// pix_opencv_haarcascade -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_haarcascade :: pix_opencv_haarcascade() -{ - m_dataout = outlet_new(this->x_obj, 0); - - scale_factor = 1.1; - min_neighbors = 2; - mode = 0; - min_size = 30; - - comp_xsize = 0; - comp_ysize = 0; - rgba = NULL; - grey = NULL; - frame = NULL; - - cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 ); - if( !cascade ) - { - post( "ERROR: Could not load classifier cascade\n" ); - } - else post( "Loaded classifier cascade from %s", cascade_name ); - -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_haarcascade :: ~pix_opencv_haarcascade() -{ - //Destroy cv_images to clean memory - cvReleaseImage(&rgba); - cvReleaseImage(&grey); - cvReleaseImage(&frame); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_haarcascade :: processRGBAImage(imageStruct &image) -{ - double scale = 1; - - if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgba)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage(&rgba); - cvReleaseImage(&grey); - cvReleaseImage(&frame); - - //create the orig image with new size - rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); - frame = cvCreateImage(cvSize(rgba->width,rgba->height), IPL_DEPTH_8U, 3); - grey = cvCreateImage( cvSize(rgba->width,rgba->height), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); - CvMemStorage* storage = cvCreateMemStorage(0); - - static CvScalar colors[] = - { - {{0,0,255}}, - {{0,128,255}}, - {{0,255,255}}, - {{0,255,0}}, - {{255,128,0}}, - {{255,255,0}}, - {{255,0,0}}, - {{255,0,255}} - }; - - int i; - - if( cascade ) - { - CvSeq* faces = cvHaarDetectObjects( rgba, cascade, storage, - scale_factor, min_neighbors, mode, cvSize(min_size, min_size) ); - for( i = 0; i < (faces ? faces->total : 0); i++ ) - { - CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); - CvPoint center; - int radius; - center.x = cvRound((r->x + r->width*0.5)*scale); - center.y = cvRound((r->y + r->height*0.5)*scale); - radius = cvRound((r->width + r->height)*0.25*scale); - cvCircle( rgba, center, radius, colors[i%8], 3, 8, 0 ); - - t_atom rlist[4]; - SETFLOAT(&rlist[0], i); - SETFLOAT(&rlist[1], center.x); - SETFLOAT(&rlist[2], center.y); - SETFLOAT(&rlist[3], radius); - outlet_list( m_dataout, 0, 4, rlist ); - } - } - - - cvReleaseMemStorage( &storage ); - //cvShowImage(wndname, cedge); - memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); -} - -void pix_opencv_haarcascade :: processRGBImage(imageStruct &image) -{ - double scale = 1; - - if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!frame)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage(&rgba); - cvReleaseImage(&grey); - cvReleaseImage(&frame); - - //create the orig image with new size - rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); - frame = cvCreateImage(cvSize(rgba->width,rgba->height), IPL_DEPTH_8U, 3); - grey = cvCreateImage( cvSize(rgba->width,rgba->height), 8, 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( frame->imageData, image.data, image.xsize*image.ysize*3 ); - CvMemStorage* storage = cvCreateMemStorage(0); - - static CvScalar colors[] = - { - {{0,0,255}}, - {{0,128,255}}, - {{0,255,255}}, - {{0,255,0}}, - {{255,128,0}}, - {{255,255,0}}, - {{255,0,0}}, - {{255,0,255}} - }; - - int i; - - if( cascade ) - { - CvSeq* faces = cvHaarDetectObjects( frame, cascade, storage, - 1.1, 2, 0, cvSize(30, 30) ); - for( i = 0; i < (faces ? faces->total : 0); i++ ) - { - CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); - CvPoint center; - int radius; - center.x = cvRound((r->x + r->width*0.5)*scale); - center.y = cvRound((r->y + r->height*0.5)*scale); - radius = cvRound((r->width + r->height)*0.25*scale); - cvCircle( frame, center, radius, colors[i%8], 3, 8, 0 ); - - t_atom rlist[4]; - SETFLOAT(&rlist[0], i); - SETFLOAT(&rlist[1], center.x); - SETFLOAT(&rlist[2], center.y); - SETFLOAT(&rlist[3], radius); - outlet_list( m_dataout, 0, 4, rlist ); - } - } - - - cvReleaseMemStorage( &storage ); - //cvShowImage(wndname, cedge); - memcpy( image.data, frame->imageData, image.xsize*image.ysize*3 ); -} - -void pix_opencv_haarcascade :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_haarcascade :: processGrayImage(imageStruct &image) -{ - double scale = 1; - - if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!grey)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage(&rgba); - cvReleaseImage(&grey); - cvReleaseImage(&frame); - - //create the orig image with new size - rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4); - frame = cvCreateImage(cvSize(rgba->width,rgba->height), IPL_DEPTH_8U, 3); - grey = cvCreateImage( cvSize(rgba->width,rgba->height), 8, 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( grey->imageData, image.data, image.xsize*image.ysize ); - CvMemStorage* storage = cvCreateMemStorage(0); - - static CvScalar colors[] = - { - {{0,0,255}}, - {{0,128,255}}, - {{0,255,255}}, - {{0,255,0}}, - {{255,128,0}}, - {{255,255,0}}, - {{255,0,0}}, - {{255,0,255}} - }; - - int i; - - if( cascade ) - { - CvSeq* faces = cvHaarDetectObjects( grey, cascade, storage, - 1.1, 2, 0, cvSize(30, 30) ); - for( i = 0; i < (faces ? faces->total : 0); i++ ) - { - CvRect* r = (CvRect*)cvGetSeqElem( faces, i ); - CvPoint center; - int radius; - center.x = cvRound((r->x + r->width*0.5)*scale); - center.y = cvRound((r->y + r->height*0.5)*scale); - radius = cvRound((r->width + r->height)*0.25*scale); - cvCircle( grey, center, radius, colors[i%8], 3, 8, 0 ); - - t_atom rlist[4]; - SETFLOAT(&rlist[0], i); - SETFLOAT(&rlist[1], center.x); - SETFLOAT(&rlist[2], center.y); - SETFLOAT(&rlist[3], radius); - outlet_list( m_dataout, 0, 4, rlist ); - } - } - - - cvReleaseMemStorage( &storage ); - //cvShowImage(wndname, cedge); - memcpy( image.data, grey->imageData, image.xsize*image.ysize ); -} - -///////////////////////////////////////////////////////// -// scaleFactorMess -// -///////////////////////////////////////////////////////// -void pix_opencv_haarcascade :: scaleFactorMess (float scale_factor) -{ - this->scale_factor = scale_factor; -} - -///////////////////////////////////////////////////////// -// minNeighborsMess -// -///////////////////////////////////////////////////////// -void pix_opencv_haarcascade :: minNeighborsMess (float min_neighbors) -{ - this->min_neighbors = (int)min_neighbors; -} - -///////////////////////////////////////////////////////// -// modeMess -// -///////////////////////////////////////////////////////// -void pix_opencv_haarcascade :: modeMess (float mode) -{ - this->mode = !(!(int)mode); -} - -///////////////////////////////////////////////////////// -// minSizeMess -// -///////////////////////////////////////////////////////// -void pix_opencv_haarcascade :: minSizeMess (float min_size) -{ - this->min_size = (int)min_size; -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_haarcascade :: obj_setupCallback(t_class *classPtr) -{ - class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::scaleFactorMessCallback, - gensym("scale_factor"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::minNeighborsMessCallback, - gensym("min_neighbors"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::modeMessCallback, - gensym("mode"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::minSizeMessCallback, - gensym("min_size"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_haarcascade::loadCascadeMessCallback, - gensym("load"), A_SYMBOL, A_NULL); -} -void pix_opencv_haarcascade :: scaleFactorMessCallback(void *data, t_floatarg scale_factor) -{ - if (scale_factor>1) GetMyClass(data)->scaleFactorMess((float)scale_factor); -} -void pix_opencv_haarcascade :: minNeighborsMessCallback(void *data, t_floatarg min_neighbors) -{ - if (min_neighbors>=1) GetMyClass(data)->minNeighborsMess((float)min_neighbors); -} -void pix_opencv_haarcascade :: modeMessCallback(void *data, t_floatarg mode) -{ - if ((mode==0)||(mode==1)) GetMyClass(data)->modeMess((float)mode); -} -void pix_opencv_haarcascade :: minSizeMessCallback(void *data, t_floatarg min_size) -{ - if (min_size>1) GetMyClass(data)->minSizeMess((float)min_size); -} -void pix_opencv_haarcascade :: loadCascadeMessCallback(void *data, t_symbol* filename) -{ - GetMyClass(data)->loadCascadeMess(filename); -} -void pix_opencv_haarcascade :: loadCascadeMess(t_symbol *filename) -{ - cascade = (CvHaarClassifierCascade*)cvLoad( filename->s_name, 0, 0, 0 ); - if( !cascade ) - { - post( "ERROR: Could not load classifier cascade" ); - } - else post( "Loaded classifier cascade from %s", filename->s_name ); -} diff --git a/pix_opencv_laplace.cc b/pix_opencv_laplace.cc new file mode 100644 index 0000000..5fbecda --- /dev/null +++ b/pix_opencv_laplace.cc @@ -0,0 +1,237 @@ +//////////////////////////////////////////////////////// +// +// 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_laplace.h" + +CPPEXTERN_NEW(pix_opencv_laplace) + +///////////////////////////////////////////////////////// +// +// pix_opencv_laplace +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_laplace :: pix_opencv_laplace() +{ + int i; + + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); + + aperture_size = 3; + comp_xsize = 0; + comp_ysize = 0; + + frame = NULL; + rgba = NULL; + alpha = NULL; + laplace = NULL; + colorlaplace = NULL; + for (i=0; i<3; i++) planes[i] = NULL; + +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_laplace :: ~pix_opencv_laplace() +{ + int i; + //Destroy cv_images to clean memory + for( i = 0; i < 3; i++ ) + cvReleaseImage( &planes[i] ); + cvReleaseImage( &frame ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); + cvReleaseImage( &laplace ); + cvReleaseImage( &colorlaplace ); +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_laplace :: processRGBAImage(imageStruct &image) +{ + unsigned char *pixels = image.data; + int i; + + if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgba)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + for( i = 0; i < 3; i++ ) + cvReleaseImage( &planes[i] ); + cvReleaseImage( &frame ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); + cvReleaseImage( &laplace ); + cvReleaseImage( &colorlaplace ); + + //Create cv_images + for( i = 0; i < 3; i++ ) + planes[i] = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 1 ); + laplace = cvCreateImage( cvSize(image.xsize, image.ysize), IPL_DEPTH_16S, 1 ); + colorlaplace = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); + frame = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); + rgba = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 4 ); + alpha = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); + + CvArr* in[] = { rgba }; + CvArr* out[] = { frame, alpha }; + int from_to[] = { 0, 0, 1, 1, 2, 2, 3, 3 }; + //cvSet( rgba, cvScalar(1,2,3,4) ); + cvMixChannels( (const CvArr**)in, 1, out, 2, from_to, 4 ); + + cvCvtPixToPlane( frame, planes[0], planes[1], planes[2], 0 ); + for( i = 0; i < 3; i++ ) + { + cvLaplace( planes[i], laplace, aperture_size ); + cvConvertScaleAbs( laplace, planes[i], 1, 0 ); + } + cvCvtPlaneToPix( planes[0], planes[1], planes[2], 0, colorlaplace ); + colorlaplace->origin = frame->origin; + + + CvArr* src[] = { colorlaplace, alpha }; + CvArr* dst[] = { rgba }; + cvMixChannels( (const CvArr**)src, 2, (CvArr**)dst, 1, from_to, 4 ); + //cvShowImage(wndname, cedge); + memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); +} + +void pix_opencv_laplace :: processRGBImage(imageStruct &image) +{ + unsigned char *pixels = image.data; + int i; + + if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!frame)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + for( i = 0; i < 3; i++ ) + cvReleaseImage( &planes[i] ); + cvReleaseImage( &frame ); + cvReleaseImage( &laplace ); + cvReleaseImage( &colorlaplace ); + + //Create cv_images + for( i = 0; i < 3; i++ ) + planes[i] = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 1 ); + laplace = cvCreateImage( cvSize(image.xsize, image.ysize), IPL_DEPTH_16S, 1 ); + colorlaplace = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); + frame = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); + } + // 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( frame->imageData, image.data, image.xsize*image.ysize*3 ); + + cvCvtPixToPlane( frame, planes[0], planes[1], planes[2], 0 ); + for( i = 0; i < 3; i++ ) + { + cvLaplace( planes[i], laplace, 3 ); + cvConvertScaleAbs( laplace, planes[i], 1, 0 ); + } + cvCvtPlaneToPix( planes[0], planes[1], planes[2], 0, colorlaplace ); + colorlaplace->origin = frame->origin; + + + //cvShowImage(wndname, cedge); + memcpy( image.data, colorlaplace->imageData, image.xsize*image.ysize*3 ); +} + +void pix_opencv_laplace :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_laplace :: processGrayImage(imageStruct &image) +{ + unsigned char *pixels = image.data; + int i; + + if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!alpha)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + for( i = 0; i < 3; i++ ) + cvReleaseImage( &planes[i] ); + cvReleaseImage( &frame ); + cvReleaseImage( &laplace ); + cvReleaseImage( &colorlaplace ); + cvReleaseImage( &alpha ); + + //Create cv_images + for( i = 0; i < 3; i++ ) + planes[i] = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 1 ); + laplace = cvCreateImage( cvSize(image.xsize, image.ysize), IPL_DEPTH_16S, 1 ); + colorlaplace = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); + frame = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); + alpha = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 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( alpha->imageData, image.data, image.xsize*image.ysize ); + + cvCvtColor( alpha, frame, CV_GRAY2RGB); + + cvCvtPixToPlane( frame, planes[0], planes[1], planes[2], 0 ); + for( i = 0; i < 3; i++ ) + { + cvLaplace( planes[i], laplace, 3 ); + cvConvertScaleAbs( laplace, planes[i], 1, 0 ); + } + cvCvtPlaneToPix( planes[0], planes[1], planes[2], 0, colorlaplace ); + colorlaplace->origin = frame->origin; + + + cvCvtColor( colorlaplace, alpha, CV_RGB2GRAY); + //cvShowImage(wndname, cedge); + memcpy( image.data, alpha->imageData, image.xsize*image.ysize ); +} + +///////////////////////////////////////////////////////// +// floatApertureMess +// +///////////////////////////////////////////////////////// +void pix_opencv_laplace :: floatApertureMess (float aperture_size) +{ + if ((aperture_size==1)||(aperture_size==3)||(aperture_size==5)||(aperture_size==7)) this->aperture_size = (int)aperture_size; + else post("aperture size out of range ... must be 1,3,5 or 7"); +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_laplace :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_laplace::floatApertureMessCallback, + gensym("ft1"), A_FLOAT, A_NULL); +} +void pix_opencv_laplace :: floatApertureMessCallback(void *data, t_floatarg aperture_size) +{ + GetMyClass(data)->floatApertureMess((float)aperture_size); +} diff --git a/pix_opencv_laplace.cpp b/pix_opencv_laplace.cpp deleted file mode 100644 index 5fbecda..0000000 --- a/pix_opencv_laplace.cpp +++ /dev/null @@ -1,237 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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_laplace.h" - -CPPEXTERN_NEW(pix_opencv_laplace) - -///////////////////////////////////////////////////////// -// -// pix_opencv_laplace -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_laplace :: pix_opencv_laplace() -{ - int i; - - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); - - aperture_size = 3; - comp_xsize = 0; - comp_ysize = 0; - - frame = NULL; - rgba = NULL; - alpha = NULL; - laplace = NULL; - colorlaplace = NULL; - for (i=0; i<3; i++) planes[i] = NULL; - -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_laplace :: ~pix_opencv_laplace() -{ - int i; - //Destroy cv_images to clean memory - for( i = 0; i < 3; i++ ) - cvReleaseImage( &planes[i] ); - cvReleaseImage( &frame ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); - cvReleaseImage( &laplace ); - cvReleaseImage( &colorlaplace ); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_laplace :: processRGBAImage(imageStruct &image) -{ - unsigned char *pixels = image.data; - int i; - - if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgba)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - for( i = 0; i < 3; i++ ) - cvReleaseImage( &planes[i] ); - cvReleaseImage( &frame ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); - cvReleaseImage( &laplace ); - cvReleaseImage( &colorlaplace ); - - //Create cv_images - for( i = 0; i < 3; i++ ) - planes[i] = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 1 ); - laplace = cvCreateImage( cvSize(image.xsize, image.ysize), IPL_DEPTH_16S, 1 ); - colorlaplace = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); - frame = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); - rgba = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 4 ); - alpha = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); - - CvArr* in[] = { rgba }; - CvArr* out[] = { frame, alpha }; - int from_to[] = { 0, 0, 1, 1, 2, 2, 3, 3 }; - //cvSet( rgba, cvScalar(1,2,3,4) ); - cvMixChannels( (const CvArr**)in, 1, out, 2, from_to, 4 ); - - cvCvtPixToPlane( frame, planes[0], planes[1], planes[2], 0 ); - for( i = 0; i < 3; i++ ) - { - cvLaplace( planes[i], laplace, aperture_size ); - cvConvertScaleAbs( laplace, planes[i], 1, 0 ); - } - cvCvtPlaneToPix( planes[0], planes[1], planes[2], 0, colorlaplace ); - colorlaplace->origin = frame->origin; - - - CvArr* src[] = { colorlaplace, alpha }; - CvArr* dst[] = { rgba }; - cvMixChannels( (const CvArr**)src, 2, (CvArr**)dst, 1, from_to, 4 ); - //cvShowImage(wndname, cedge); - memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); -} - -void pix_opencv_laplace :: processRGBImage(imageStruct &image) -{ - unsigned char *pixels = image.data; - int i; - - if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!frame)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - for( i = 0; i < 3; i++ ) - cvReleaseImage( &planes[i] ); - cvReleaseImage( &frame ); - cvReleaseImage( &laplace ); - cvReleaseImage( &colorlaplace ); - - //Create cv_images - for( i = 0; i < 3; i++ ) - planes[i] = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 1 ); - laplace = cvCreateImage( cvSize(image.xsize, image.ysize), IPL_DEPTH_16S, 1 ); - colorlaplace = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); - frame = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); - } - // 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( frame->imageData, image.data, image.xsize*image.ysize*3 ); - - cvCvtPixToPlane( frame, planes[0], planes[1], planes[2], 0 ); - for( i = 0; i < 3; i++ ) - { - cvLaplace( planes[i], laplace, 3 ); - cvConvertScaleAbs( laplace, planes[i], 1, 0 ); - } - cvCvtPlaneToPix( planes[0], planes[1], planes[2], 0, colorlaplace ); - colorlaplace->origin = frame->origin; - - - //cvShowImage(wndname, cedge); - memcpy( image.data, colorlaplace->imageData, image.xsize*image.ysize*3 ); -} - -void pix_opencv_laplace :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_laplace :: processGrayImage(imageStruct &image) -{ - unsigned char *pixels = image.data; - int i; - - if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!alpha)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - for( i = 0; i < 3; i++ ) - cvReleaseImage( &planes[i] ); - cvReleaseImage( &frame ); - cvReleaseImage( &laplace ); - cvReleaseImage( &colorlaplace ); - cvReleaseImage( &alpha ); - - //Create cv_images - for( i = 0; i < 3; i++ ) - planes[i] = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 1 ); - laplace = cvCreateImage( cvSize(image.xsize, image.ysize), IPL_DEPTH_16S, 1 ); - colorlaplace = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); - frame = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 3 ); - alpha = cvCreateImage( cvSize(image.xsize,image.ysize), 8, 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( alpha->imageData, image.data, image.xsize*image.ysize ); - - cvCvtColor( alpha, frame, CV_GRAY2RGB); - - cvCvtPixToPlane( frame, planes[0], planes[1], planes[2], 0 ); - for( i = 0; i < 3; i++ ) - { - cvLaplace( planes[i], laplace, 3 ); - cvConvertScaleAbs( laplace, planes[i], 1, 0 ); - } - cvCvtPlaneToPix( planes[0], planes[1], planes[2], 0, colorlaplace ); - colorlaplace->origin = frame->origin; - - - cvCvtColor( colorlaplace, alpha, CV_RGB2GRAY); - //cvShowImage(wndname, cedge); - memcpy( image.data, alpha->imageData, image.xsize*image.ysize ); -} - -///////////////////////////////////////////////////////// -// floatApertureMess -// -///////////////////////////////////////////////////////// -void pix_opencv_laplace :: floatApertureMess (float aperture_size) -{ - if ((aperture_size==1)||(aperture_size==3)||(aperture_size==5)||(aperture_size==7)) this->aperture_size = (int)aperture_size; - else post("aperture size out of range ... must be 1,3,5 or 7"); -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_laplace :: obj_setupCallback(t_class *classPtr) -{ - class_addmethod(classPtr, (t_method)&pix_opencv_laplace::floatApertureMessCallback, - gensym("ft1"), A_FLOAT, A_NULL); -} -void pix_opencv_laplace :: floatApertureMessCallback(void *data, t_floatarg aperture_size) -{ - GetMyClass(data)->floatApertureMess((float)aperture_size); -} diff --git a/pix_opencv_morphology.cc b/pix_opencv_morphology.cc new file mode 100644 index 0000000..5d95266 --- /dev/null +++ b/pix_opencv_morphology.cc @@ -0,0 +1,297 @@ +//////////////////////////////////////////////////////// +// +// 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_morphology.h" + +CPPEXTERN_NEW(pix_opencv_morphology) + +///////////////////////////////////////////////////////// +// +// pix_opencv_morphology +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_morphology :: pix_opencv_morphology() +{ + int i; + + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); + + pos = 0; + comp_xsize = 0; + comp_ysize = 0; + + rgba = NULL; + alpha = NULL; + src = NULL; + dst = NULL; + + element_shape = CV_SHAPE_RECT; + +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_morphology :: ~pix_opencv_morphology() +{ + //Destroy cv_images to clean memory + cvReleaseImage( &src ); + cvReleaseImage( &dst ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_morphology :: processRGBAImage(imageStruct &image) +{ + unsigned char *pixels = image.data; + int i; + + if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgba)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage( &src ); + cvReleaseImage( &dst ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); + + //Create cv_images + src = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 3 ); + dst = cvCloneImage(src); + rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); + alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); + + CvArr* in[] = { rgba }; + CvArr* out[] = { src, alpha }; + int from_to[] = { 0, 0, 1, 1, 2, 2, 3, 3 }; + //cvSet( rgba, cvScalar(1,2,3,4) ); + cvMixChannels( (const CvArr**)in, 1, out, 2, from_to, 4 ); + + if (this->mode == 1) { //open/close + int n = pos; + int an = n > 0 ? n : -n; + element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); + if( n < 0 ) + { + cvErode(src,dst,element,1); + cvDilate(dst,dst,element,1); + } + else + { + cvDilate(src,dst,element,1); + cvErode(dst,dst,element,1); + } + cvReleaseStructuringElement(&element); + + } else { + int n = pos; + int an = n > 0 ? n : -n; + element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); + if( n < 0 ) + { + cvErode(src,dst,element,1); + } + else + { + cvDilate(src,dst,element,1); + } + cvReleaseStructuringElement(&element); + + } + + CvArr* src[] = { dst, alpha }; + CvArr* dst[] = { rgba }; + cvMixChannels( (const CvArr**)src, 2, (CvArr**)dst, 1, from_to, 4 ); + //cvShowImage(wndname, cedge); + memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); +} + +void pix_opencv_morphology :: processRGBImage(imageStruct &image) +{ + unsigned char *pixels = image.data; + int i; + + if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!src)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage( &src ); + cvReleaseImage( &dst ); + + //Create cv_images + src = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); + dst = cvCloneImage(src); + } + // 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( src->imageData, image.data, image.xsize*image.ysize*3 ); + + if (this->mode == 1) { //open/close + int n = pos; + int an = n > 0 ? n : -n; + element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); + if( n < 0 ) + { + cvErode(src,dst,element,1); + cvDilate(dst,dst,element,1); + } + else + { + cvDilate(src,dst,element,1); + cvErode(dst,dst,element,1); + } + cvReleaseStructuringElement(&element); + + } else { + int n = pos; + int an = n > 0 ? n : -n; + element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); + if( n < 0 ) + { + cvErode(src,dst,element,1); + } + else + { + cvDilate(src,dst,element,1); + } + cvReleaseStructuringElement(&element); + + } + + //cvShowImage(wndname, cedge); + memcpy( image.data, dst->imageData, image.xsize*image.ysize*3 ); +} + +void pix_opencv_morphology :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_morphology :: processGrayImage(imageStruct &image) +{ + unsigned char *pixels = image.data; + int i; + + if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!alpha)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage( &src ); + cvReleaseImage( &dst ); + cvReleaseImage( &alpha ); + + //Create cv_images + src = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 3 ); + dst = cvCloneImage(src); + alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( alpha->imageData, image.data, image.xsize*image.ysize ); + + cvCvtColor(alpha, src, CV_GRAY2RGB); + + if (this->mode == 1) { //open/close + int n = pos; + int an = n > 0 ? n : -n; + element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); + if( n < 0 ) + { + cvErode(src,dst,element,1); + cvDilate(dst,dst,element,1); + } + else + { + cvDilate(src,dst,element,1); + cvErode(dst,dst,element,1); + } + cvReleaseStructuringElement(&element); + + } else { + int n = pos; + int an = n > 0 ? n : -n; + element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); + if( n < 0 ) + { + cvErode(src,dst,element,1); + } + else + { + cvDilate(src,dst,element,1); + } + cvReleaseStructuringElement(&element); + + } + + cvCvtColor(dst, alpha, CV_RGB2GRAY); + //cvShowImage(wndname, cedge); + memcpy( image.data, alpha->imageData, image.xsize*image.ysize ); +} + +///////////////////////////////////////////////////////// +// floatPosMess +// +///////////////////////////////////////////////////////// +void pix_opencv_morphology :: floatPosMess (float pos) +{ + this->pos = (int)pos; +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_morphology :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_morphology::floatPosMessCallback, + gensym("ft1"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_morphology::modeMessCallback, + gensym("mode"), A_DEFFLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_morphology::shapeMessCallback, + gensym("shape"), A_DEFFLOAT, A_NULL); +} +void pix_opencv_morphology :: floatPosMessCallback(void *data, t_floatarg pos) +{ + GetMyClass(data)->floatPosMess((float)pos); +} +void pix_opencv_morphology :: modeMessCallback(void *data, t_floatarg mode) +{ + GetMyClass(data)->mode=!(!(int)mode); +} +void pix_opencv_morphology :: shapeMessCallback(void *data, t_floatarg f) +{ + if( (int)f == 1 ) + GetMyClass(data)->element_shape = CV_SHAPE_RECT; + else if( (int)f == 2 ) + GetMyClass(data)->element_shape = CV_SHAPE_ELLIPSE; + else if( (int)f == 3 ) + GetMyClass(data)->element_shape = CV_SHAPE_CROSS; +} diff --git a/pix_opencv_morphology.cpp b/pix_opencv_morphology.cpp deleted file mode 100644 index 5d95266..0000000 --- a/pix_opencv_morphology.cpp +++ /dev/null @@ -1,297 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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_morphology.h" - -CPPEXTERN_NEW(pix_opencv_morphology) - -///////////////////////////////////////////////////////// -// -// pix_opencv_morphology -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_morphology :: pix_opencv_morphology() -{ - int i; - - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); - - pos = 0; - comp_xsize = 0; - comp_ysize = 0; - - rgba = NULL; - alpha = NULL; - src = NULL; - dst = NULL; - - element_shape = CV_SHAPE_RECT; - -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_morphology :: ~pix_opencv_morphology() -{ - //Destroy cv_images to clean memory - cvReleaseImage( &src ); - cvReleaseImage( &dst ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_morphology :: processRGBAImage(imageStruct &image) -{ - unsigned char *pixels = image.data; - int i; - - if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgba)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage( &src ); - cvReleaseImage( &dst ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); - - //Create cv_images - src = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 3 ); - dst = cvCloneImage(src); - rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); - alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); - - CvArr* in[] = { rgba }; - CvArr* out[] = { src, alpha }; - int from_to[] = { 0, 0, 1, 1, 2, 2, 3, 3 }; - //cvSet( rgba, cvScalar(1,2,3,4) ); - cvMixChannels( (const CvArr**)in, 1, out, 2, from_to, 4 ); - - if (this->mode == 1) { //open/close - int n = pos; - int an = n > 0 ? n : -n; - element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); - if( n < 0 ) - { - cvErode(src,dst,element,1); - cvDilate(dst,dst,element,1); - } - else - { - cvDilate(src,dst,element,1); - cvErode(dst,dst,element,1); - } - cvReleaseStructuringElement(&element); - - } else { - int n = pos; - int an = n > 0 ? n : -n; - element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); - if( n < 0 ) - { - cvErode(src,dst,element,1); - } - else - { - cvDilate(src,dst,element,1); - } - cvReleaseStructuringElement(&element); - - } - - CvArr* src[] = { dst, alpha }; - CvArr* dst[] = { rgba }; - cvMixChannels( (const CvArr**)src, 2, (CvArr**)dst, 1, from_to, 4 ); - //cvShowImage(wndname, cedge); - memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); -} - -void pix_opencv_morphology :: processRGBImage(imageStruct &image) -{ - unsigned char *pixels = image.data; - int i; - - if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!src)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage( &src ); - cvReleaseImage( &dst ); - - //Create cv_images - src = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); - dst = cvCloneImage(src); - } - // 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( src->imageData, image.data, image.xsize*image.ysize*3 ); - - if (this->mode == 1) { //open/close - int n = pos; - int an = n > 0 ? n : -n; - element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); - if( n < 0 ) - { - cvErode(src,dst,element,1); - cvDilate(dst,dst,element,1); - } - else - { - cvDilate(src,dst,element,1); - cvErode(dst,dst,element,1); - } - cvReleaseStructuringElement(&element); - - } else { - int n = pos; - int an = n > 0 ? n : -n; - element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); - if( n < 0 ) - { - cvErode(src,dst,element,1); - } - else - { - cvDilate(src,dst,element,1); - } - cvReleaseStructuringElement(&element); - - } - - //cvShowImage(wndname, cedge); - memcpy( image.data, dst->imageData, image.xsize*image.ysize*3 ); -} - -void pix_opencv_morphology :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_morphology :: processGrayImage(imageStruct &image) -{ - unsigned char *pixels = image.data; - int i; - - if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!alpha)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage( &src ); - cvReleaseImage( &dst ); - cvReleaseImage( &alpha ); - - //Create cv_images - src = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 3 ); - dst = cvCloneImage(src); - alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( alpha->imageData, image.data, image.xsize*image.ysize ); - - cvCvtColor(alpha, src, CV_GRAY2RGB); - - if (this->mode == 1) { //open/close - int n = pos; - int an = n > 0 ? n : -n; - element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); - if( n < 0 ) - { - cvErode(src,dst,element,1); - cvDilate(dst,dst,element,1); - } - else - { - cvDilate(src,dst,element,1); - cvErode(dst,dst,element,1); - } - cvReleaseStructuringElement(&element); - - } else { - int n = pos; - int an = n > 0 ? n : -n; - element = cvCreateStructuringElementEx( an*2+1, an*2+1, an, an, element_shape, 0 ); - if( n < 0 ) - { - cvErode(src,dst,element,1); - } - else - { - cvDilate(src,dst,element,1); - } - cvReleaseStructuringElement(&element); - - } - - cvCvtColor(dst, alpha, CV_RGB2GRAY); - //cvShowImage(wndname, cedge); - memcpy( image.data, alpha->imageData, image.xsize*image.ysize ); -} - -///////////////////////////////////////////////////////// -// floatPosMess -// -///////////////////////////////////////////////////////// -void pix_opencv_morphology :: floatPosMess (float pos) -{ - this->pos = (int)pos; -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_morphology :: obj_setupCallback(t_class *classPtr) -{ - class_addmethod(classPtr, (t_method)&pix_opencv_morphology::floatPosMessCallback, - gensym("ft1"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_morphology::modeMessCallback, - gensym("mode"), A_DEFFLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_morphology::shapeMessCallback, - gensym("shape"), A_DEFFLOAT, A_NULL); -} -void pix_opencv_morphology :: floatPosMessCallback(void *data, t_floatarg pos) -{ - GetMyClass(data)->floatPosMess((float)pos); -} -void pix_opencv_morphology :: modeMessCallback(void *data, t_floatarg mode) -{ - GetMyClass(data)->mode=!(!(int)mode); -} -void pix_opencv_morphology :: shapeMessCallback(void *data, t_floatarg f) -{ - if( (int)f == 1 ) - GetMyClass(data)->element_shape = CV_SHAPE_RECT; - else if( (int)f == 2 ) - GetMyClass(data)->element_shape = CV_SHAPE_ELLIPSE; - else if( (int)f == 3 ) - GetMyClass(data)->element_shape = CV_SHAPE_CROSS; -} diff --git a/pix_opencv_motempl.cc b/pix_opencv_motempl.cc new file mode 100644 index 0000000..48f6be6 --- /dev/null +++ b/pix_opencv_motempl.cc @@ -0,0 +1,631 @@ +//////////////////////////////////////////////////////// +// +// 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_motempl.h" + +CPPEXTERN_NEW(pix_opencv_motempl) + +///////////////////////////////////////////////////////// +// +// pix_opencv_motempl +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_motempl :: pix_opencv_motempl() +{ + int i; + + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("min_size")); + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("max_size")); + m_dataout = outlet_new(this->x_obj, 0); + + mhi_duration = 1; + diff_threshold = 30; + last = 0; + comp_xsize = 0; + comp_ysize = 0; + + // various tracking parameters (in seconds) + max_time_delta = 0.5; + min_time_delta = 0.05; + // number of cyclic frame buffer used for motion detection + // (should, probably, depend on FPS) + frame_buffer_num = 4; + + min_size=50; + max_size=500; + + img = NULL; + motion = NULL; + rgba = NULL; + alpha = NULL; + mhi = NULL; + orient = NULL; + mask = NULL; + segmask = NULL; + storage = NULL; + + mask_size = CV_DIST_MASK_PRECISE; + +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_motempl :: ~pix_opencv_motempl() +{ + //Destroy cv_images to clean memory + cvReleaseImage( &img ); + cvReleaseImage( &motion ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_motempl :: processRGBAImage(imageStruct &image) +{ + + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage( &img ); + cvReleaseImage( &motion ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); + + //Create cv_images + img = cvCreateImage(cvSize(image.xsize, image.ysize), IPL_DEPTH_8U, 3); + motion = cvCreateImage( cvSize(img->width,img->height), 8, 3 ); + cvZero( motion ); + motion->origin = img->origin; + rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); + alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); + + CvArr* in[] = { rgba }; + CvArr* out[] = { img, alpha }; + int from_to[] = { 0, 0, 1, 1, 2, 2, 3, 3 }; + //cvSet( rgba, cvScalar(1,2,3,4) ); + cvMixChannels( (const CvArr**)in, 1, out, 2, from_to, 4 ); + + double timestamp = (double)clock()/CLOCKS_PER_SEC; // get current time in seconds + CvSize size = cvSize(img->width,img->height); // get current frame size + int i, idx1 = last, idx2; + IplImage* silh; + CvSeq* seq; + CvRect comp_rect; + double count; + double angle; + CvPoint center; + double magnitude; + CvScalar color; + + // allocate images at the beginning or + // reallocate them if the frame size is changed + if( (!mhi) || (mhi->width != size.width) || (mhi->height != size.height) || (!buf)) { + if( buf == 0 ) { + buf = (IplImage**)malloc(frame_buffer_num*sizeof(buf[0])); + //memset( buf, 0, N*sizeof(buf[0])); + } + + for( i = 0; i < frame_buffer_num; i++ ) { + //cvReleaseImage( &(buf[i]) ); + buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 ); + cvZero( buf[i] ); + } + cvReleaseImage( &mhi ); + cvReleaseImage( &orient ); + cvReleaseImage( &segmask ); + cvReleaseImage( &mask ); + + mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 ); + cvZero( mhi ); // clear MHI at the beginning + orient = cvCreateImage( size, IPL_DEPTH_32F, 1 ); + segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 ); + mask = cvCreateImage( size, IPL_DEPTH_8U, 1 ); + } + + cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale + + idx2 = (last + 1) % frame_buffer_num; // index of (last - (N-1))th frame + last = idx2; + + silh = buf[idx2]; + cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames + + cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); // and threshold it + cvUpdateMotionHistory( silh, mhi, timestamp, mhi_duration ); // update MHI + + // convert MHI to red 8u image + cvCvtScale( mhi, mask, 255./mhi_duration, + (mhi_duration - timestamp)*255./mhi_duration ); + cvZero( motion ); + cvCvtPlaneToPix( mask, 0, 0, 0, motion ); + + // calculate motion gradient orientation and valid orientation mask + cvCalcMotionGradient( mhi, mask, orient, max_time_delta, min_time_delta, 3 ); + + if( !storage ) + storage = cvCreateMemStorage(0); + else + cvClearMemStorage(storage); + + // segment motion: get sequence of motion components + // segmask is marked motion components map. It is not used further + seq = cvSegmentMotion( mhi, segmask, storage, timestamp, max_time_delta ); + + // iterate through the motion components, + // One more iteration (i == -1) corresponds to the whole image (global motion) + for( i = -1; i < seq->total; i++ ) { + + if( i < 0 ) { // case of the whole image + comp_rect = cvRect( 0, 0, size.width, size.height ); + color = CV_RGB(255,255,255); + magnitude = 100; + } + else { // i-th motion component + comp_rect = ((CvConnectedComp*)cvGetSeqElem( seq, i ))->rect; + if(( comp_rect.width + comp_rect.height < min_size )||( comp_rect.width + comp_rect.height > max_size )) // reject very small/big components + continue; + color = CV_RGB(255,0,0); + magnitude = (comp_rect.width + comp_rect.height) /4; + } + + // select component ROI + cvSetImageROI( silh, comp_rect ); + cvSetImageROI( mhi, comp_rect ); + cvSetImageROI( orient, comp_rect ); + cvSetImageROI( mask, comp_rect ); + + // calculate orientation + angle = cvCalcGlobalOrientation( orient, mask, mhi, timestamp, mhi_duration); + angle = 360.0 - angle; // adjust for images with top-left origin + + count = cvNorm( silh, 0, CV_L1, 0 ); // calculate number of points within silhouette ROI + + cvResetImageROI( mhi ); + cvResetImageROI( orient ); + cvResetImageROI( mask ); + cvResetImageROI( silh ); + + // check for the case of little motion + if( count < comp_rect.width*comp_rect.height * 0.05 ) + continue; + + // draw a clock with arrow indicating the direction + center = cvPoint( (comp_rect.x + comp_rect.width/2), + (comp_rect.y + comp_rect.height/2) ); + + cvCircle( motion, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 ); + cvLine( motion, center, cvPoint( cvRound( center.x + magnitude*cos(angle*CV_PI/180)), + cvRound( center.y - magnitude*sin(angle*CV_PI/180))), color, 3, CV_AA, 0 ); + + //aqui treurem la info dels blobs en questio ... + t_atom rlist[6]; + SETFLOAT(&rlist[0], i); + SETFLOAT(&rlist[1], center.x); + SETFLOAT(&rlist[2], center.y); + SETFLOAT(&rlist[3], comp_rect.width); + SETFLOAT(&rlist[4], comp_rect.height); + SETFLOAT(&rlist[5], angle); + outlet_list( m_dataout, 0, 6, rlist ); + } + + + CvArr* src[] = { motion, alpha }; + CvArr* dst[] = { rgba }; + cvMixChannels( (const CvArr**)src, 2, (CvArr**)dst, 1, from_to, 4 ); + //cvShowImage(wndname, cedge); + memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); +} + +void pix_opencv_motempl :: processRGBImage(imageStruct &image) +{ + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage( &img ); + cvReleaseImage( &motion ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); + + //Create cv_images + img = cvCreateImage(cvSize(image.xsize, image.ysize), IPL_DEPTH_8U, 3); + motion = cvCreateImage( cvSize(img->width,img->height), 8, 3 ); + cvZero( motion ); + motion->origin = img->origin; + rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); + alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( img->imageData, image.data, image.xsize*image.ysize*3 ); + + double timestamp = (double)clock()/CLOCKS_PER_SEC; // get current time in seconds + CvSize size = cvSize(img->width,img->height); // get current frame size + int i, idx1 = last, idx2; + IplImage* silh; + CvSeq* seq; + CvRect comp_rect; + double count; + double angle; + CvPoint center; + double magnitude; + CvScalar color; + + // allocate images at the beginning or + // reallocate them if the frame size is changed + if( (!mhi) || (mhi->width != size.width) || (mhi->height != size.height) || (!buf)) { + if( buf == 0 ) { + buf = (IplImage**)malloc(frame_buffer_num*sizeof(buf[0])); + //memset( buf, 0, N*sizeof(buf[0])); + } + + for( i = 0; i < frame_buffer_num; i++ ) { + //cvReleaseImage( &(buf[i]) ); + buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 ); + cvZero( buf[i] ); + } + cvReleaseImage( &mhi ); + cvReleaseImage( &orient ); + cvReleaseImage( &segmask ); + cvReleaseImage( &mask ); + + mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 ); + cvZero( mhi ); // clear MHI at the beginning + orient = cvCreateImage( size, IPL_DEPTH_32F, 1 ); + segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 ); + mask = cvCreateImage( size, IPL_DEPTH_8U, 1 ); + } + + cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale + + idx2 = (last + 1) % frame_buffer_num; // index of (last - (N-1))th frame + last = idx2; + + silh = buf[idx2]; + cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames + + cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); // and threshold it + cvUpdateMotionHistory( silh, mhi, timestamp, mhi_duration ); // update MHI + + // convert MHI to blue 8u image + cvCvtScale( mhi, mask, 255./mhi_duration, + (mhi_duration - timestamp)*255./mhi_duration ); + cvZero( motion ); + cvCvtPlaneToPix( mask, 0, 0, 0, motion ); + + // calculate motion gradient orientation and valid orientation mask + cvCalcMotionGradient( mhi, mask, orient, max_time_delta, min_time_delta, 3 ); + + if( !storage ) + storage = cvCreateMemStorage(0); + else + cvClearMemStorage(storage); + + // segment motion: get sequence of motion components + // segmask is marked motion components map. It is not used further + seq = cvSegmentMotion( mhi, segmask, storage, timestamp, max_time_delta ); + + // iterate through the motion components, + // One more iteration (i == -1) corresponds to the whole image (global motion) + for( i = -1; i < seq->total; i++ ) { + + if( i < 0 ) { // case of the whole image + comp_rect = cvRect( 0, 0, size.width, size.height ); + color = CV_RGB(255,255,255); + magnitude = 100; + } + else { // i-th motion component + comp_rect = ((CvConnectedComp*)cvGetSeqElem( seq, i ))->rect; + if(( comp_rect.width + comp_rect.height < min_size )||( comp_rect.width + comp_rect.height > max_size )) // reject very small/big components + continue; + color = CV_RGB(255,0,0); + magnitude = (comp_rect.width + comp_rect.height) / 4; + } + + // select component ROI + cvSetImageROI( silh, comp_rect ); + cvSetImageROI( mhi, comp_rect ); + cvSetImageROI( orient, comp_rect ); + cvSetImageROI( mask, comp_rect ); + + // calculate orientation + angle = cvCalcGlobalOrientation( orient, mask, mhi, timestamp, mhi_duration); + angle = 360.0 - angle; // adjust for images with top-left origin + + count = cvNorm( silh, 0, CV_L1, 0 ); // calculate number of points within silhouette ROI + + cvResetImageROI( mhi ); + cvResetImageROI( orient ); + cvResetImageROI( mask ); + cvResetImageROI( silh ); + + // check for the case of little motion + if( count < comp_rect.width*comp_rect.height * 0.05 ) + continue; + + // draw a clock with arrow indicating the direction + center = cvPoint( (comp_rect.x + comp_rect.width/2), + (comp_rect.y + comp_rect.height/2) ); + + cvCircle( motion, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 ); + cvLine( motion, center, cvPoint( cvRound( center.x + magnitude*cos(angle*CV_PI/180)), + cvRound( center.y - magnitude*sin(angle*CV_PI/180))), color, 3, CV_AA, 0 ); + + t_atom rlist[6]; + SETFLOAT(&rlist[0], i); + SETFLOAT(&rlist[1], center.x); + SETFLOAT(&rlist[2], center.y); + SETFLOAT(&rlist[3], comp_rect.width); + SETFLOAT(&rlist[4], comp_rect.height); + SETFLOAT(&rlist[5], angle); + outlet_list( m_dataout, 0, 6, rlist ); + } + + + //cvShowImage(wndname, cedge); + memcpy( image.data, motion->imageData, image.xsize*image.ysize*3 ); +} + +void pix_opencv_motempl :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_motempl :: processGrayImage(imageStruct &image) +{ + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { + + this->comp_xsize = image.xsize; + this->comp_ysize = image.ysize; + + //Destroy cv_images to clean memory + cvReleaseImage( &img ); + cvReleaseImage( &motion ); + cvReleaseImage( &rgba ); + cvReleaseImage( &alpha ); + + //Create cv_images + img = cvCreateImage(cvSize(image.xsize, image.ysize), IPL_DEPTH_8U, 3); + motion = cvCreateImage( cvSize(img->width,img->height), 8, 3 ); + cvZero( motion ); + motion->origin = img->origin; + rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); + alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( alpha->imageData, image.data, image.xsize*image.ysize ); + + // Convert to RGB + cvCvtColor( alpha, img, CV_GRAY2RGB); + + double timestamp = (double)clock()/CLOCKS_PER_SEC; // get current time in seconds + CvSize size = cvSize(img->width,img->height); // get current frame size + int i, idx1 = last, idx2; + IplImage* silh; + CvSeq* seq; + CvRect comp_rect; + double count; + double angle; + CvPoint center; + double magnitude; + CvScalar color; + + // allocate images at the beginning or + // reallocate them if the frame size is changed + if( (!mhi) || (mhi->width != size.width) || (mhi->height != size.height) || (!buf)) { + if( buf == 0 ) { + buf = (IplImage**)malloc(frame_buffer_num*sizeof(buf[0])); + //memset( buf, 0, N*sizeof(buf[0])); + } + + for( i = 0; i < frame_buffer_num; i++ ) { + //cvReleaseImage( &(buf[i]) ); + buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 ); + cvZero( buf[i] ); + } + cvReleaseImage( &mhi ); + cvReleaseImage( &orient ); + cvReleaseImage( &segmask ); + cvReleaseImage( &mask ); + + mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 ); + cvZero( mhi ); // clear MHI at the beginning + orient = cvCreateImage( size, IPL_DEPTH_32F, 1 ); + segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 ); + mask = cvCreateImage( size, IPL_DEPTH_8U, 1 ); + } + + cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale + + idx2 = (last + 1) % frame_buffer_num; // index of (last - (N-1))th frame + last = idx2; + + silh = buf[idx2]; + cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames + + cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); // and threshold it + cvUpdateMotionHistory( silh, mhi, timestamp, mhi_duration ); // update MHI + + // convert MHI to blue 8u image + cvCvtScale( mhi, mask, 255./mhi_duration, + (mhi_duration - timestamp)*255./mhi_duration ); + cvZero( motion ); + cvCvtPlaneToPix( mask, 0, 0, 0, motion ); + + // calculate motion gradient orientation and valid orientation mask + cvCalcMotionGradient( mhi, mask, orient, max_time_delta, min_time_delta, 3 ); + + if( !storage ) + storage = cvCreateMemStorage(0); + else + cvClearMemStorage(storage); + + // segment motion: get sequence of motion components + // segmask is marked motion components map. It is not used further + seq = cvSegmentMotion( mhi, segmask, storage, timestamp, max_time_delta ); + + // iterate through the motion components, + // One more iteration (i == -1) corresponds to the whole image (global motion) + for( i = -1; i < seq->total; i++ ) { + + if( i < 0 ) { // case of the whole image + comp_rect = cvRect( 0, 0, size.width, size.height ); + color = CV_RGB(255,255,255); + magnitude = 100; + } + else { // i-th motion component + comp_rect = ((CvConnectedComp*)cvGetSeqElem( seq, i ))->rect; + if(( comp_rect.width + comp_rect.height < min_size )||( comp_rect.width + comp_rect.height > max_size )) // reject very small components + continue; + color = CV_RGB(255,0,0); + magnitude = (comp_rect.width + comp_rect.height) / 4; + } + + // select component ROI + cvSetImageROI( silh, comp_rect ); + cvSetImageROI( mhi, comp_rect ); + cvSetImageROI( orient, comp_rect ); + cvSetImageROI( mask, comp_rect ); + + // calculate orientation + angle = cvCalcGlobalOrientation( orient, mask, mhi, timestamp, mhi_duration); + angle = 360.0 - angle; // adjust for images with top-left origin + + count = cvNorm( silh, 0, CV_L1, 0 ); // calculate number of points within silhouette ROI + + cvResetImageROI( mhi ); + cvResetImageROI( orient ); + cvResetImageROI( mask ); + cvResetImageROI( silh ); + + // check for the case of little motion + if( count < comp_rect.width*comp_rect.height * 0.05 ) + continue; + + // draw a clock with arrow indicating the direction + center = cvPoint( (comp_rect.x + comp_rect.width/2), + (comp_rect.y + comp_rect.height/2) ); + + cvCircle( motion, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 ); + cvLine( motion, center, cvPoint( cvRound( center.x + magnitude*cos(angle*CV_PI/180)), + cvRound( center.y - magnitude*sin(angle*CV_PI/180))), color, 3, CV_AA, 0 ); + + t_atom rlist[6]; + SETFLOAT(&rlist[0], i); + SETFLOAT(&rlist[1], center.x); + SETFLOAT(&rlist[2], center.y); + SETFLOAT(&rlist[3], comp_rect.width); + SETFLOAT(&rlist[4], comp_rect.height); + SETFLOAT(&rlist[5], angle); + outlet_list( m_dataout, 0, 6, rlist ); + } + + + // Convert to grayscale + cvCvtColor( motion, alpha, CV_RGB2GRAY); + //cvShowImage(wndname, cedge); + memcpy( image.data, alpha->imageData, image.xsize*image.ysize ); +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_motempl :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_motempl::thresholdMessCallback, + gensym("ft1"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_motempl::mhi_durationMessCallback, + gensym("mhi_duration"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_motempl::max_time_deltaMessCallback, gensym("max_time_delta"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_motempl::min_time_deltaMessCallback, gensym("min_time_delta"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_motempl::frame_buffer_numMessCallback, gensym("frame_buffer_num"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_motempl::min_sizeMessCallback, gensym("min_size"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_motempl::max_sizeMessCallback, gensym("max_size"), A_FLOAT, A_NULL); +} +void pix_opencv_motempl :: thresholdMessCallback(void *data, t_floatarg pos) +{ + GetMyClass(data)->floatThreshMess((float)pos); +} +void pix_opencv_motempl :: mhi_durationMessCallback(void *data, t_floatarg mhi_duration) +{ + GetMyClass(data)->floatMhiDuration((float)mhi_duration); +} +void pix_opencv_motempl :: min_sizeMessCallback(void *data, t_floatarg min_size) +{ + GetMyClass(data)->floatmin_size((float)min_size); +} +void pix_opencv_motempl :: max_sizeMessCallback(void *data, t_floatarg max_size) +{ + GetMyClass(data)->floatmax_size((float)max_size); +} +void pix_opencv_motempl :: max_time_deltaMessCallback(void *data, t_floatarg max_time_delta) +{ + GetMyClass(data)->floatmax_time_delta((float)max_time_delta); +} +void pix_opencv_motempl :: min_time_deltaMessCallback(void *data, t_floatarg min_time_delta) +{ + GetMyClass(data)->floatmin_time_delta((float)min_time_delta); +} +void pix_opencv_motempl :: frame_buffer_numMessCallback(void *data, t_floatarg frame_buffer_num) +{ + GetMyClass(data)->floatframe_buffer_num((float)frame_buffer_num); +} +void pix_opencv_motempl :: floatThreshMess(float thresh) +{ + if (thresh>=0) diff_threshold = (int)thresh; +} +void pix_opencv_motempl :: floatMhiDuration(float duration) +{ + if (duration>=1) mhi_duration = (int)duration; +} +void pix_opencv_motempl :: floatmax_size(float max_size) +{ + if (max_size>=0) this->max_size = (int)max_size; +} +void pix_opencv_motempl :: floatmin_size(float min_size) +{ + if (min_size>=0) this->min_size = (int)min_size; +} +void pix_opencv_motempl :: floatframe_buffer_num(float frame_buffer_num) +{ + if (frame_buffer_num>=1) this->frame_buffer_num = (int)frame_buffer_num; + this->buf = NULL; +} +void pix_opencv_motempl :: floatmax_time_delta(float max_time_delta) +{ + if (max_time_delta>=0) this->max_time_delta = max_time_delta; +} +void pix_opencv_motempl :: floatmin_time_delta(float min_time_delta) +{ + if (min_time_delta>=0) this->min_time_delta = min_time_delta; +} diff --git a/pix_opencv_motempl.cpp b/pix_opencv_motempl.cpp deleted file mode 100644 index 48f6be6..0000000 --- a/pix_opencv_motempl.cpp +++ /dev/null @@ -1,631 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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_motempl.h" - -CPPEXTERN_NEW(pix_opencv_motempl) - -///////////////////////////////////////////////////////// -// -// pix_opencv_motempl -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_motempl :: pix_opencv_motempl() -{ - int i; - - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("min_size")); - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("max_size")); - m_dataout = outlet_new(this->x_obj, 0); - - mhi_duration = 1; - diff_threshold = 30; - last = 0; - comp_xsize = 0; - comp_ysize = 0; - - // various tracking parameters (in seconds) - max_time_delta = 0.5; - min_time_delta = 0.05; - // number of cyclic frame buffer used for motion detection - // (should, probably, depend on FPS) - frame_buffer_num = 4; - - min_size=50; - max_size=500; - - img = NULL; - motion = NULL; - rgba = NULL; - alpha = NULL; - mhi = NULL; - orient = NULL; - mask = NULL; - segmask = NULL; - storage = NULL; - - mask_size = CV_DIST_MASK_PRECISE; - -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_motempl :: ~pix_opencv_motempl() -{ - //Destroy cv_images to clean memory - cvReleaseImage( &img ); - cvReleaseImage( &motion ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_motempl :: processRGBAImage(imageStruct &image) -{ - - if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage( &img ); - cvReleaseImage( &motion ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); - - //Create cv_images - img = cvCreateImage(cvSize(image.xsize, image.ysize), IPL_DEPTH_8U, 3); - motion = cvCreateImage( cvSize(img->width,img->height), 8, 3 ); - cvZero( motion ); - motion->origin = img->origin; - rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); - alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( rgba->imageData, image.data, image.xsize*image.ysize*4 ); - - CvArr* in[] = { rgba }; - CvArr* out[] = { img, alpha }; - int from_to[] = { 0, 0, 1, 1, 2, 2, 3, 3 }; - //cvSet( rgba, cvScalar(1,2,3,4) ); - cvMixChannels( (const CvArr**)in, 1, out, 2, from_to, 4 ); - - double timestamp = (double)clock()/CLOCKS_PER_SEC; // get current time in seconds - CvSize size = cvSize(img->width,img->height); // get current frame size - int i, idx1 = last, idx2; - IplImage* silh; - CvSeq* seq; - CvRect comp_rect; - double count; - double angle; - CvPoint center; - double magnitude; - CvScalar color; - - // allocate images at the beginning or - // reallocate them if the frame size is changed - if( (!mhi) || (mhi->width != size.width) || (mhi->height != size.height) || (!buf)) { - if( buf == 0 ) { - buf = (IplImage**)malloc(frame_buffer_num*sizeof(buf[0])); - //memset( buf, 0, N*sizeof(buf[0])); - } - - for( i = 0; i < frame_buffer_num; i++ ) { - //cvReleaseImage( &(buf[i]) ); - buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 ); - cvZero( buf[i] ); - } - cvReleaseImage( &mhi ); - cvReleaseImage( &orient ); - cvReleaseImage( &segmask ); - cvReleaseImage( &mask ); - - mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 ); - cvZero( mhi ); // clear MHI at the beginning - orient = cvCreateImage( size, IPL_DEPTH_32F, 1 ); - segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 ); - mask = cvCreateImage( size, IPL_DEPTH_8U, 1 ); - } - - cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale - - idx2 = (last + 1) % frame_buffer_num; // index of (last - (N-1))th frame - last = idx2; - - silh = buf[idx2]; - cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames - - cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); // and threshold it - cvUpdateMotionHistory( silh, mhi, timestamp, mhi_duration ); // update MHI - - // convert MHI to red 8u image - cvCvtScale( mhi, mask, 255./mhi_duration, - (mhi_duration - timestamp)*255./mhi_duration ); - cvZero( motion ); - cvCvtPlaneToPix( mask, 0, 0, 0, motion ); - - // calculate motion gradient orientation and valid orientation mask - cvCalcMotionGradient( mhi, mask, orient, max_time_delta, min_time_delta, 3 ); - - if( !storage ) - storage = cvCreateMemStorage(0); - else - cvClearMemStorage(storage); - - // segment motion: get sequence of motion components - // segmask is marked motion components map. It is not used further - seq = cvSegmentMotion( mhi, segmask, storage, timestamp, max_time_delta ); - - // iterate through the motion components, - // One more iteration (i == -1) corresponds to the whole image (global motion) - for( i = -1; i < seq->total; i++ ) { - - if( i < 0 ) { // case of the whole image - comp_rect = cvRect( 0, 0, size.width, size.height ); - color = CV_RGB(255,255,255); - magnitude = 100; - } - else { // i-th motion component - comp_rect = ((CvConnectedComp*)cvGetSeqElem( seq, i ))->rect; - if(( comp_rect.width + comp_rect.height < min_size )||( comp_rect.width + comp_rect.height > max_size )) // reject very small/big components - continue; - color = CV_RGB(255,0,0); - magnitude = (comp_rect.width + comp_rect.height) /4; - } - - // select component ROI - cvSetImageROI( silh, comp_rect ); - cvSetImageROI( mhi, comp_rect ); - cvSetImageROI( orient, comp_rect ); - cvSetImageROI( mask, comp_rect ); - - // calculate orientation - angle = cvCalcGlobalOrientation( orient, mask, mhi, timestamp, mhi_duration); - angle = 360.0 - angle; // adjust for images with top-left origin - - count = cvNorm( silh, 0, CV_L1, 0 ); // calculate number of points within silhouette ROI - - cvResetImageROI( mhi ); - cvResetImageROI( orient ); - cvResetImageROI( mask ); - cvResetImageROI( silh ); - - // check for the case of little motion - if( count < comp_rect.width*comp_rect.height * 0.05 ) - continue; - - // draw a clock with arrow indicating the direction - center = cvPoint( (comp_rect.x + comp_rect.width/2), - (comp_rect.y + comp_rect.height/2) ); - - cvCircle( motion, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 ); - cvLine( motion, center, cvPoint( cvRound( center.x + magnitude*cos(angle*CV_PI/180)), - cvRound( center.y - magnitude*sin(angle*CV_PI/180))), color, 3, CV_AA, 0 ); - - //aqui treurem la info dels blobs en questio ... - t_atom rlist[6]; - SETFLOAT(&rlist[0], i); - SETFLOAT(&rlist[1], center.x); - SETFLOAT(&rlist[2], center.y); - SETFLOAT(&rlist[3], comp_rect.width); - SETFLOAT(&rlist[4], comp_rect.height); - SETFLOAT(&rlist[5], angle); - outlet_list( m_dataout, 0, 6, rlist ); - } - - - CvArr* src[] = { motion, alpha }; - CvArr* dst[] = { rgba }; - cvMixChannels( (const CvArr**)src, 2, (CvArr**)dst, 1, from_to, 4 ); - //cvShowImage(wndname, cedge); - memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); -} - -void pix_opencv_motempl :: processRGBImage(imageStruct &image) -{ - if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage( &img ); - cvReleaseImage( &motion ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); - - //Create cv_images - img = cvCreateImage(cvSize(image.xsize, image.ysize), IPL_DEPTH_8U, 3); - motion = cvCreateImage( cvSize(img->width,img->height), 8, 3 ); - cvZero( motion ); - motion->origin = img->origin; - rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); - alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( img->imageData, image.data, image.xsize*image.ysize*3 ); - - double timestamp = (double)clock()/CLOCKS_PER_SEC; // get current time in seconds - CvSize size = cvSize(img->width,img->height); // get current frame size - int i, idx1 = last, idx2; - IplImage* silh; - CvSeq* seq; - CvRect comp_rect; - double count; - double angle; - CvPoint center; - double magnitude; - CvScalar color; - - // allocate images at the beginning or - // reallocate them if the frame size is changed - if( (!mhi) || (mhi->width != size.width) || (mhi->height != size.height) || (!buf)) { - if( buf == 0 ) { - buf = (IplImage**)malloc(frame_buffer_num*sizeof(buf[0])); - //memset( buf, 0, N*sizeof(buf[0])); - } - - for( i = 0; i < frame_buffer_num; i++ ) { - //cvReleaseImage( &(buf[i]) ); - buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 ); - cvZero( buf[i] ); - } - cvReleaseImage( &mhi ); - cvReleaseImage( &orient ); - cvReleaseImage( &segmask ); - cvReleaseImage( &mask ); - - mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 ); - cvZero( mhi ); // clear MHI at the beginning - orient = cvCreateImage( size, IPL_DEPTH_32F, 1 ); - segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 ); - mask = cvCreateImage( size, IPL_DEPTH_8U, 1 ); - } - - cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale - - idx2 = (last + 1) % frame_buffer_num; // index of (last - (N-1))th frame - last = idx2; - - silh = buf[idx2]; - cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames - - cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); // and threshold it - cvUpdateMotionHistory( silh, mhi, timestamp, mhi_duration ); // update MHI - - // convert MHI to blue 8u image - cvCvtScale( mhi, mask, 255./mhi_duration, - (mhi_duration - timestamp)*255./mhi_duration ); - cvZero( motion ); - cvCvtPlaneToPix( mask, 0, 0, 0, motion ); - - // calculate motion gradient orientation and valid orientation mask - cvCalcMotionGradient( mhi, mask, orient, max_time_delta, min_time_delta, 3 ); - - if( !storage ) - storage = cvCreateMemStorage(0); - else - cvClearMemStorage(storage); - - // segment motion: get sequence of motion components - // segmask is marked motion components map. It is not used further - seq = cvSegmentMotion( mhi, segmask, storage, timestamp, max_time_delta ); - - // iterate through the motion components, - // One more iteration (i == -1) corresponds to the whole image (global motion) - for( i = -1; i < seq->total; i++ ) { - - if( i < 0 ) { // case of the whole image - comp_rect = cvRect( 0, 0, size.width, size.height ); - color = CV_RGB(255,255,255); - magnitude = 100; - } - else { // i-th motion component - comp_rect = ((CvConnectedComp*)cvGetSeqElem( seq, i ))->rect; - if(( comp_rect.width + comp_rect.height < min_size )||( comp_rect.width + comp_rect.height > max_size )) // reject very small/big components - continue; - color = CV_RGB(255,0,0); - magnitude = (comp_rect.width + comp_rect.height) / 4; - } - - // select component ROI - cvSetImageROI( silh, comp_rect ); - cvSetImageROI( mhi, comp_rect ); - cvSetImageROI( orient, comp_rect ); - cvSetImageROI( mask, comp_rect ); - - // calculate orientation - angle = cvCalcGlobalOrientation( orient, mask, mhi, timestamp, mhi_duration); - angle = 360.0 - angle; // adjust for images with top-left origin - - count = cvNorm( silh, 0, CV_L1, 0 ); // calculate number of points within silhouette ROI - - cvResetImageROI( mhi ); - cvResetImageROI( orient ); - cvResetImageROI( mask ); - cvResetImageROI( silh ); - - // check for the case of little motion - if( count < comp_rect.width*comp_rect.height * 0.05 ) - continue; - - // draw a clock with arrow indicating the direction - center = cvPoint( (comp_rect.x + comp_rect.width/2), - (comp_rect.y + comp_rect.height/2) ); - - cvCircle( motion, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 ); - cvLine( motion, center, cvPoint( cvRound( center.x + magnitude*cos(angle*CV_PI/180)), - cvRound( center.y - magnitude*sin(angle*CV_PI/180))), color, 3, CV_AA, 0 ); - - t_atom rlist[6]; - SETFLOAT(&rlist[0], i); - SETFLOAT(&rlist[1], center.x); - SETFLOAT(&rlist[2], center.y); - SETFLOAT(&rlist[3], comp_rect.width); - SETFLOAT(&rlist[4], comp_rect.height); - SETFLOAT(&rlist[5], angle); - outlet_list( m_dataout, 0, 6, rlist ); - } - - - //cvShowImage(wndname, cedge); - memcpy( image.data, motion->imageData, image.xsize*image.ysize*3 ); -} - -void pix_opencv_motempl :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_motempl :: processGrayImage(imageStruct &image) -{ - if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) { - - this->comp_xsize = image.xsize; - this->comp_ysize = image.ysize; - - //Destroy cv_images to clean memory - cvReleaseImage( &img ); - cvReleaseImage( &motion ); - cvReleaseImage( &rgba ); - cvReleaseImage( &alpha ); - - //Create cv_images - img = cvCreateImage(cvSize(image.xsize, image.ysize), IPL_DEPTH_8U, 3); - motion = cvCreateImage( cvSize(img->width,img->height), 8, 3 ); - cvZero( motion ); - motion->origin = img->origin; - rgba = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 4 ); - alpha = cvCreateImage( cvSize(image.xsize, image.ysize), 8, 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( alpha->imageData, image.data, image.xsize*image.ysize ); - - // Convert to RGB - cvCvtColor( alpha, img, CV_GRAY2RGB); - - double timestamp = (double)clock()/CLOCKS_PER_SEC; // get current time in seconds - CvSize size = cvSize(img->width,img->height); // get current frame size - int i, idx1 = last, idx2; - IplImage* silh; - CvSeq* seq; - CvRect comp_rect; - double count; - double angle; - CvPoint center; - double magnitude; - CvScalar color; - - // allocate images at the beginning or - // reallocate them if the frame size is changed - if( (!mhi) || (mhi->width != size.width) || (mhi->height != size.height) || (!buf)) { - if( buf == 0 ) { - buf = (IplImage**)malloc(frame_buffer_num*sizeof(buf[0])); - //memset( buf, 0, N*sizeof(buf[0])); - } - - for( i = 0; i < frame_buffer_num; i++ ) { - //cvReleaseImage( &(buf[i]) ); - buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 ); - cvZero( buf[i] ); - } - cvReleaseImage( &mhi ); - cvReleaseImage( &orient ); - cvReleaseImage( &segmask ); - cvReleaseImage( &mask ); - - mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 ); - cvZero( mhi ); // clear MHI at the beginning - orient = cvCreateImage( size, IPL_DEPTH_32F, 1 ); - segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 ); - mask = cvCreateImage( size, IPL_DEPTH_8U, 1 ); - } - - cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale - - idx2 = (last + 1) % frame_buffer_num; // index of (last - (N-1))th frame - last = idx2; - - silh = buf[idx2]; - cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames - - cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); // and threshold it - cvUpdateMotionHistory( silh, mhi, timestamp, mhi_duration ); // update MHI - - // convert MHI to blue 8u image - cvCvtScale( mhi, mask, 255./mhi_duration, - (mhi_duration - timestamp)*255./mhi_duration ); - cvZero( motion ); - cvCvtPlaneToPix( mask, 0, 0, 0, motion ); - - // calculate motion gradient orientation and valid orientation mask - cvCalcMotionGradient( mhi, mask, orient, max_time_delta, min_time_delta, 3 ); - - if( !storage ) - storage = cvCreateMemStorage(0); - else - cvClearMemStorage(storage); - - // segment motion: get sequence of motion components - // segmask is marked motion components map. It is not used further - seq = cvSegmentMotion( mhi, segmask, storage, timestamp, max_time_delta ); - - // iterate through the motion components, - // One more iteration (i == -1) corresponds to the whole image (global motion) - for( i = -1; i < seq->total; i++ ) { - - if( i < 0 ) { // case of the whole image - comp_rect = cvRect( 0, 0, size.width, size.height ); - color = CV_RGB(255,255,255); - magnitude = 100; - } - else { // i-th motion component - comp_rect = ((CvConnectedComp*)cvGetSeqElem( seq, i ))->rect; - if(( comp_rect.width + comp_rect.height < min_size )||( comp_rect.width + comp_rect.height > max_size )) // reject very small components - continue; - color = CV_RGB(255,0,0); - magnitude = (comp_rect.width + comp_rect.height) / 4; - } - - // select component ROI - cvSetImageROI( silh, comp_rect ); - cvSetImageROI( mhi, comp_rect ); - cvSetImageROI( orient, comp_rect ); - cvSetImageROI( mask, comp_rect ); - - // calculate orientation - angle = cvCalcGlobalOrientation( orient, mask, mhi, timestamp, mhi_duration); - angle = 360.0 - angle; // adjust for images with top-left origin - - count = cvNorm( silh, 0, CV_L1, 0 ); // calculate number of points within silhouette ROI - - cvResetImageROI( mhi ); - cvResetImageROI( orient ); - cvResetImageROI( mask ); - cvResetImageROI( silh ); - - // check for the case of little motion - if( count < comp_rect.width*comp_rect.height * 0.05 ) - continue; - - // draw a clock with arrow indicating the direction - center = cvPoint( (comp_rect.x + comp_rect.width/2), - (comp_rect.y + comp_rect.height/2) ); - - cvCircle( motion, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 ); - cvLine( motion, center, cvPoint( cvRound( center.x + magnitude*cos(angle*CV_PI/180)), - cvRound( center.y - magnitude*sin(angle*CV_PI/180))), color, 3, CV_AA, 0 ); - - t_atom rlist[6]; - SETFLOAT(&rlist[0], i); - SETFLOAT(&rlist[1], center.x); - SETFLOAT(&rlist[2], center.y); - SETFLOAT(&rlist[3], comp_rect.width); - SETFLOAT(&rlist[4], comp_rect.height); - SETFLOAT(&rlist[5], angle); - outlet_list( m_dataout, 0, 6, rlist ); - } - - - // Convert to grayscale - cvCvtColor( motion, alpha, CV_RGB2GRAY); - //cvShowImage(wndname, cedge); - memcpy( image.data, alpha->imageData, image.xsize*image.ysize ); -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_motempl :: obj_setupCallback(t_class *classPtr) -{ - class_addmethod(classPtr, (t_method)&pix_opencv_motempl::thresholdMessCallback, - gensym("ft1"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_motempl::mhi_durationMessCallback, - gensym("mhi_duration"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_motempl::max_time_deltaMessCallback, gensym("max_time_delta"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_motempl::min_time_deltaMessCallback, gensym("min_time_delta"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_motempl::frame_buffer_numMessCallback, gensym("frame_buffer_num"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_motempl::min_sizeMessCallback, gensym("min_size"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_motempl::max_sizeMessCallback, gensym("max_size"), A_FLOAT, A_NULL); -} -void pix_opencv_motempl :: thresholdMessCallback(void *data, t_floatarg pos) -{ - GetMyClass(data)->floatThreshMess((float)pos); -} -void pix_opencv_motempl :: mhi_durationMessCallback(void *data, t_floatarg mhi_duration) -{ - GetMyClass(data)->floatMhiDuration((float)mhi_duration); -} -void pix_opencv_motempl :: min_sizeMessCallback(void *data, t_floatarg min_size) -{ - GetMyClass(data)->floatmin_size((float)min_size); -} -void pix_opencv_motempl :: max_sizeMessCallback(void *data, t_floatarg max_size) -{ - GetMyClass(data)->floatmax_size((float)max_size); -} -void pix_opencv_motempl :: max_time_deltaMessCallback(void *data, t_floatarg max_time_delta) -{ - GetMyClass(data)->floatmax_time_delta((float)max_time_delta); -} -void pix_opencv_motempl :: min_time_deltaMessCallback(void *data, t_floatarg min_time_delta) -{ - GetMyClass(data)->floatmin_time_delta((float)min_time_delta); -} -void pix_opencv_motempl :: frame_buffer_numMessCallback(void *data, t_floatarg frame_buffer_num) -{ - GetMyClass(data)->floatframe_buffer_num((float)frame_buffer_num); -} -void pix_opencv_motempl :: floatThreshMess(float thresh) -{ - if (thresh>=0) diff_threshold = (int)thresh; -} -void pix_opencv_motempl :: floatMhiDuration(float duration) -{ - if (duration>=1) mhi_duration = (int)duration; -} -void pix_opencv_motempl :: floatmax_size(float max_size) -{ - if (max_size>=0) this->max_size = (int)max_size; -} -void pix_opencv_motempl :: floatmin_size(float min_size) -{ - if (min_size>=0) this->min_size = (int)min_size; -} -void pix_opencv_motempl :: floatframe_buffer_num(float frame_buffer_num) -{ - if (frame_buffer_num>=1) this->frame_buffer_num = (int)frame_buffer_num; - this->buf = NULL; -} -void pix_opencv_motempl :: floatmax_time_delta(float max_time_delta) -{ - if (max_time_delta>=0) this->max_time_delta = max_time_delta; -} -void pix_opencv_motempl :: floatmin_time_delta(float min_time_delta) -{ - if (min_time_delta>=0) this->min_time_delta = min_time_delta; -} diff --git a/pix_opencv_threshold.cpp b/pix_opencv_threshold.cpp deleted file mode 100644 index b102c0d..0000000 --- a/pix_opencv_threshold.cpp +++ /dev/null @@ -1,266 +0,0 @@ -//////////////////////////////////////////////////////// -// -// 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_threshold.h" - -CPPEXTERN_NEW(pix_opencv_threshold) - -///////////////////////////////////////////////////////// -// -// pix_opencv_threshold -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_threshold :: pix_opencv_threshold() -{ - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1")); - inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft2")); - threshold_value = 1; - max_value = 255; - threshold_mode = 0; - comp_xsize = 0; - comp_ysize = 0; - orig = NULL; - gray = NULL; -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_threshold :: ~pix_opencv_threshold() -{ - //Destroy cv_images to clean memory - cvReleaseImage(&orig); - cvReleaseImage(&gray); - cvReleaseImage(&edge); - cvReleaseImage(&cedge); - cvReleaseImage(&cedgergb); - cvReleaseImage(&rgb); -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_threshold :: 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(&edge); - cvReleaseImage(&cedge); - cvReleaseImage(&cedgergb); - 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 - 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_BGRA2GRAY); - - // Applies fixed-level thresholding to single-channel array. - switch(this->threshold_mode) { - case 0: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_BINARY); - break; - case 1: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_BINARY_INV); - break; - case 2: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_TRUNC); - break; - case 3: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_TOZERO); - break; - case 4: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_TOZERO_INV); - break; - } - - cvCvtColor(gray, orig, CV_GRAY2BGRA); - - //copy back the processed frame to image - memcpy( image.data, orig->imageData, image.xsize*image.ysize*4 ); -} - -void pix_opencv_threshold :: 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(&edge); - cvReleaseImage(&cedge); - cvReleaseImage(&cedgergb); - 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); - - // Applies fixed-level thresholding to single-channel array. - switch(this->threshold_mode) { - case 0: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_BINARY); - break; - case 1: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_BINARY_INV); - break; - case 2: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_TRUNC); - break; - case 3: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_TOZERO); - break; - case 4: - cvThreshold(gray, gray, (float)this->threshold_value, (float)this->max_value, CV_THRESH_TOZERO_INV); - break; - } - - cvCvtColor(gray, rgb, CV_GRAY2BGR); - - //cvShowImage(wndname, cedge); - memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 ); -} - -void pix_opencv_threshold :: processYUVImage(imageStruct &image) -{ -} - -void pix_opencv_threshold :: processGrayImage(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(&edge); - cvReleaseImage(&cedge); - cvReleaseImage(&cedgergb); - 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 - cedgergb = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 3); - - gray = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 1); - edge = 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( gray->imageData, image.data, image.xsize*image.ysize ); - - // Convert to RGB - cvCvtColor( gray, rgb, CV_GRAY2RGB); - - cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 ); - cvNot( gray, edge ); - - // Run the edge detector on grayscale - cvCanny(gray, edge, (float)this->edge_thresh, (float)this->edge_thresh*3, 3); - - cvZero( cedgergb ); - // copy edge points - cvCopy( rgb, cedgergb, edge ); - - cvCvtColor( cedgergb, gray, CV_RGB2GRAY); - //cvShowImage(wndname, cedge); - memcpy( image.data, gray->imageData, image.xsize*image.ysize ); -} - -///////////////////////////////////////////////////////// -// floatThreshMess -// -///////////////////////////////////////////////////////// -void pix_opencv_threshold :: floatThreshMess (float edge_thresh) -{ - this->edge_thresh = (int)edge_thresh; -} -void pix_opencv_threshold :: floatMaxMess (float thresh_max) -{ - if ((thresh_max>=0)&&(thresh_max<256)) this->max_value = (int)thresh_max; -} -void pix_opencv_threshold :: floatModeMess (float mode) -{ - f ((mode>=0)&&(mode<7)) this->threshold_mode = (int)edge_thresh; -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_threshold :: obj_setupCallback(t_class *classPtr) -{ - class_addmethod(classPtr, (t_method)&pix_opencv_threshold::floatModeMessCallback, - gensym("mode"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_threshold::floatMaxMessCallback, - gensym("ft1"), A_FLOAT, A_NULL); - class_addmethod(classPtr, (t_method)&pix_opencv_threshold::floatTreshMessCallback, - gensym("ft2"), A_FLOAT, A_NULL); -} -void pix_opencv_threshold :: floatTreshMessCallback(void *data, t_floatarg edge_thresh) -{ - GetMyClass(data)->floatThreshMess((float)edge_thresh); -} -void pix_opencv_threshold :: floatMaxMessCallback(void *data, t_floatarg edge_thresh) -{ - GetMyClass(data)->floatMaxMess((float)edge_thresh); -} -void pix_opencv_threshold :: floatModeMessCallback(void *data, t_floatarg edge_thresh) -{ - GetMyClass(data)->floatModeMess((float)edge_thresh); -} diff --git a/pix_opencv_threshold.h b/pix_opencv_threshold.h deleted file mode 100644 index a3de7df..0000000 --- a/pix_opencv_threshold.h +++ /dev/null @@ -1,87 +0,0 @@ -/*----------------------------------------------------------------- -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_EDGE_H_ -#define INCLUDE_PIX_OPENCV_EDGE_H_ - -#include "Base/GemPixObj.h" - -#ifndef _EiC -#include "cv.h" -#endif - -/*----------------------------------------------------------------- -------------------------------------------------------------------- -CLASS - pix_opencv_threshold - - Change pix to greyscale - -KEYWORDS - pix - -DESCRIPTION - ------------------------------------------------------------------*/ -class GEM_EXTERN pix_opencv_threshold : public GemPixObj -{ - CPPEXTERN_HEADER(pix_opencv_threshold, GemPixObj) - - public: - - ////////// - // Constructor - pix_opencv_threshold(); - - protected: - - ////////// - // Destructor - virtual ~pix_opencv_threshold(); - - ////////// - // 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 floatThreshMess(float edge_thresh); - // The new edge threshold - int threshold_value; - int max_value; - int threshold_mode; - - // to detect changes in the image size - int comp_xsize; - int comp_ysize; - - private: - - ////////// - // Static member functions - static void floatTreshMessCallback(void *data, t_floatarg thresh_value); - static void floatModeMessCallback(void *data, t_floatarg thresh_mode_value); - static void floatMaxMessCallback(void *data, t_floatarg max_value); - - ///////// - // IplImage needed - IplImage *orig, *rgb, *gray; - -}; - -#endif // for header file -- cgit v1.2.1