From 33bbe0fa5c8a6b6e6e1f29072756d8713e2f1cbe Mon Sep 17 00:00:00 2001 From: "N.N." Date: Mon, 19 Oct 2009 17:31:38 +0000 Subject: added optical flow objects svn path=/trunk/externals/pix_opencv/; revision=12617 --- pix_opencv_of_bm.cc | 564 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 564 insertions(+) create mode 100755 pix_opencv_of_bm.cc (limited to 'pix_opencv_of_bm.cc') diff --git a/pix_opencv_of_bm.cc b/pix_opencv_of_bm.cc new file mode 100755 index 0000000..00eb8fb --- /dev/null +++ b/pix_opencv_of_bm.cc @@ -0,0 +1,564 @@ + +// +// 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_of_bm.h" +#include + +CPPEXTERN_NEW(pix_opencv_of_bm) + +///////////////////////////////////////////////////////// +// +// pix_opencv_of_bm +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// + +pix_opencv_of_bm :: pix_opencv_of_bm() +{ + comp_xsize=320; + comp_ysize=240; + + m_meanout = outlet_new(this->x_obj, &s_anything); + m_maxout = outlet_new(this->x_obj, &s_anything); + + x_nightmode=0; + x_threshold=10; + x_blocksize.width = 20; + x_blocksize.height = 20; + x_shiftsize.width = 20; + x_shiftsize.height = 20; + x_maxrange.width = 10; + x_maxrange.height = 10; + x_useprevious = 0; + x_minblocks = 10; + x_velsize.width = (comp_xsize-x_blocksize.width)/x_shiftsize.width; + x_velsize.height = (comp_ysize-x_blocksize.height)/x_shiftsize.height; + + // initialize font + cvInitFont( &font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0, 0, 1, 8 ); + + rgba = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 4 ); + rgb = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 3 ); + grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + prev_grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + + x_velx = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + x_vely = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_of_bm :: ~pix_opencv_of_bm() +{ + // Destroy cv_images + cvReleaseImage( &rgba ); + cvReleaseImage( &rgb ); + cvReleaseImage( &grey ); + cvReleaseImage( &prev_grey ); + cvReleaseImage( &x_velx ); + cvReleaseImage( &x_vely ); + +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_of_bm :: processRGBAImage(imageStruct &image) +{ + int px,py; + double meanangle=0.0, meanx=0.0, meany=0.0, maxamp=0.0, maxangle=0.0; + int nbblocks=0; + CvPoint orig, dest; + double angle=0.0; + double hypotenuse=0.0; + + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) + { + + this->comp_xsize=image.xsize; + this->comp_ysize=image.ysize; + + x_velsize.width = (comp_xsize-x_blocksize.width)/x_shiftsize.width; + x_velsize.height = (comp_ysize-x_blocksize.height)/x_shiftsize.height; + + cvReleaseImage( &rgba ); + cvReleaseImage( &rgb ); + cvReleaseImage( &grey ); + cvReleaseImage( &prev_grey ); + cvReleaseImage( &x_velx ); + cvReleaseImage( &x_vely ); + + rgba = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 4 ); + rgb = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 3 ); + grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + prev_grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + + x_velx = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + x_vely = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + } + + memcpy( rgba->imageData, image.data, image.xsize*image.ysize*4 ); + + // Convert to hsv + cvCvtColor(rgba, rgb, CV_BGRA2BGR); + cvCvtColor(rgb, grey, CV_BGR2GRAY); + + if( x_nightmode ) + cvZero( rgb ); + + cvCalcOpticalFlowBM( prev_grey, grey, + x_blocksize, x_shiftsize, + x_maxrange, x_useprevious, + x_velx, x_vely ); + + nbblocks = 0; + for( py=0; py= x_threshold) + { + cvLine( rgb, orig, dest, CV_RGB(0,255,0), (int)hypotenuse/10, CV_AA, 0 ); + + orig.x = (int) (dest.x - (x_shiftsize.width/4) * cos(angle + M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/4) * sin(angle + M_PI / 4)); + cvLine( rgb, orig, dest, CV_RGB(0,0,255), (int)hypotenuse/10, CV_AA, 0 ); + orig.x = (int) (dest.x - (x_shiftsize.width/4) * cos(angle - M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/4) * sin(angle - M_PI / 4)); + cvLine( rgb, orig, dest, CV_RGB(0,0,255), (int)hypotenuse/10, CV_AA, 0 ); + meanx = (meanx*nbblocks+cvGet2D(x_velx, py, px).val[0])/(nbblocks+1); + meany = (meanx*nbblocks+cvGet2D(x_vely, py, px).val[0])/(nbblocks+1); + if ( hypotenuse > maxamp ) + { + maxamp = hypotenuse; + maxangle = angle; + } + // post( "pdp_opencv_of_bm : block %d : amp : %f : angle : %f", nbblocks, hypotenuse, (angle*180)/M_PI ); + nbblocks++; + } + + } + } + + meanangle=-atan2( meany, meanx ); + // post( "pdp_opencv_of_bm : meanangle : %f", (meanangle*180)/M_PI ); + + if ( nbblocks >= x_minblocks ) + { + orig.x = (int) (comp_xsize/2); + orig.y = (int) (comp_ysize/2); + dest.x = (int) (orig.x+((comp_xsize>comp_ysize)?comp_ysize/2:comp_xsize/2)*cos(meanangle)); + dest.y = (int) (orig.y-((comp_xsize>comp_ysize)?comp_ysize/2:comp_xsize/2)*sin(meanangle)); + cvLine( rgb, orig, dest, CV_RGB(255,255,255), 3, CV_AA, 0 ); + orig.x = (int) (dest.x - (x_shiftsize.width/2) * cos(meanangle + M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/2) * sin(meanangle + M_PI / 4)); + cvLine( rgb, orig, dest, CV_RGB(255,255,255), 3, CV_AA, 0 ); + orig.x = (int) (dest.x - (x_shiftsize.width/2) * cos(meanangle - M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/2) * sin(meanangle - M_PI / 4)); + cvLine( rgb, orig, dest, CV_RGB(255,255,255), 3, CV_AA, 0 ); + + // outputs the average angle of movement + meanangle = (meanangle*180)/M_PI; + SETFLOAT(&x_list[0], meanangle); + outlet_list( m_meanout, 0, 1, x_list ); + + // outputs the amplitude and angle of the maximum movement + maxangle = (maxangle*180)/M_PI; + SETFLOAT(&x_list[0], maxamp); + SETFLOAT(&x_list[1], maxangle); + outlet_list( m_maxout, 0, 2, x_list ); + } + + memcpy( prev_grey->imageData, grey->imageData, image.xsize*image.ysize ); + + cvCvtColor(rgb, rgba, CV_BGR2BGRA); + memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); +} + +void pix_opencv_of_bm :: processRGBImage(imageStruct &image) +{ + int px,py; + double meanangle=0.0, meanx=0.0, meany=0.0, maxamp=0.0, maxangle=0.0; + int nbblocks=0; + CvPoint orig, dest; + double angle=0.0; + double hypotenuse=0.0; + + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) + { + + this->comp_xsize=image.xsize; + this->comp_ysize=image.ysize; + + x_velsize.width = (comp_xsize-x_blocksize.width)/x_shiftsize.width; + x_velsize.height = (comp_ysize-x_blocksize.height)/x_shiftsize.height; + + cvReleaseImage( &rgba ); + cvReleaseImage( &rgb ); + cvReleaseImage( &grey ); + cvReleaseImage( &prev_grey ); + cvReleaseImage( &x_velx ); + cvReleaseImage( &x_vely ); + + rgba = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 4 ); + rgb = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 3 ); + grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + prev_grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + + x_velx = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + x_vely = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + } + + memcpy( rgb->imageData, image.data, image.xsize*image.ysize*3 ); + + // Convert to hsv + cvCvtColor(rgba, rgb, CV_BGRA2BGR); + cvCvtColor(rgb, grey, CV_BGR2GRAY); + + if( x_nightmode ) + cvZero( rgb ); + + cvCalcOpticalFlowBM( prev_grey, grey, + x_blocksize, x_shiftsize, + x_maxrange, x_useprevious, + x_velx, x_vely ); + + nbblocks = 0; + for( py=0; py= x_threshold) + { + cvLine( rgb, orig, dest, CV_RGB(0,255,0), (int)hypotenuse/10, CV_AA, 0 ); + + orig.x = (int) (dest.x - (x_shiftsize.width/4) * cos(angle + M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/4) * sin(angle + M_PI / 4)); + cvLine( rgb, orig, dest, CV_RGB(0,0,255), (int)hypotenuse/10, CV_AA, 0 ); + orig.x = (int) (dest.x - (x_shiftsize.width/4) * cos(angle - M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/4) * sin(angle - M_PI / 4)); + cvLine( rgb, orig, dest, CV_RGB(0,0,255), (int)hypotenuse/10, CV_AA, 0 ); + meanx = (meanx*nbblocks+cvGet2D(x_velx, py, px).val[0])/(nbblocks+1); + meany = (meanx*nbblocks+cvGet2D(x_vely, py, px).val[0])/(nbblocks+1); + if ( hypotenuse > maxamp ) + { + maxamp = hypotenuse; + maxangle = angle; + } + // post( "pdp_opencv_of_bm : block %d : amp : %f : angle : %f", nbblocks, hypotenuse, (angle*180)/M_PI ); + nbblocks++; + } + + } + } + + meanangle=-atan2( meany, meanx ); + // post( "pdp_opencv_of_bm : meanangle : %f", (meanangle*180)/M_PI ); + + if ( nbblocks >= x_minblocks ) + { + orig.x = (int) (comp_xsize/2); + orig.y = (int) (comp_ysize/2); + dest.x = (int) (orig.x+((comp_xsize>comp_ysize)?comp_ysize/2:comp_xsize/2)*cos(meanangle)); + dest.y = (int) (orig.y-((comp_xsize>comp_ysize)?comp_ysize/2:comp_xsize/2)*sin(meanangle)); + cvLine( rgb, orig, dest, CV_RGB(255,255,255), 3, CV_AA, 0 ); + orig.x = (int) (dest.x - (x_shiftsize.width/2) * cos(meanangle + M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/2) * sin(meanangle + M_PI / 4)); + cvLine( rgb, orig, dest, CV_RGB(255,255,255), 3, CV_AA, 0 ); + orig.x = (int) (dest.x - (x_shiftsize.width/2) * cos(meanangle - M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/2) * sin(meanangle - M_PI / 4)); + cvLine( rgb, orig, dest, CV_RGB(255,255,255), 3, CV_AA, 0 ); + + // outputs the average angle of movement + meanangle = (meanangle*180)/M_PI; + SETFLOAT(&x_list[0], meanangle); + outlet_list( m_meanout, 0, 1, x_list ); + + // outputs the amplitude and angle of the maximum movement + maxangle = (maxangle*180)/M_PI; + SETFLOAT(&x_list[0], maxamp); + SETFLOAT(&x_list[1], maxangle); + outlet_list( m_maxout, 0, 2, x_list ); + } + + memcpy( prev_grey->imageData, grey->imageData, image.xsize*image.ysize ); + + memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 ); +} + +void pix_opencv_of_bm :: processYUVImage(imageStruct &image) +{ + post( "pix_opencv_of_bm : yuv format not supported" ); +} + +void pix_opencv_of_bm :: processGrayImage(imageStruct &image) +{ + int px,py; + double meanangle=0.0, meanx=0.0, meany=0.0, maxamp=0.0, maxangle=0.0; + int nbblocks=0; + CvPoint orig, dest; + double angle=0.0; + double hypotenuse=0.0; + + if ((this->comp_xsize!=image.xsize)&&(this->comp_ysize!=image.ysize)) + { + + this->comp_xsize=image.xsize; + this->comp_ysize=image.ysize; + + x_velsize.width = (comp_xsize-x_blocksize.width)/x_shiftsize.width; + x_velsize.height = (comp_ysize-x_blocksize.height)/x_shiftsize.height; + + cvReleaseImage( &rgba ); + cvReleaseImage( &rgb ); + cvReleaseImage( &grey ); + cvReleaseImage( &prev_grey ); + cvReleaseImage( &x_velx ); + cvReleaseImage( &x_vely ); + + rgba = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 4 ); + rgb = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 3 ); + grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + prev_grey = cvCreateImage( cvSize(comp_xsize, comp_ysize), 8, 1 ); + + x_velx = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + x_vely = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + } + + memcpy( grey->imageData, image.data, image.xsize*image.ysize ); + + if( x_nightmode ) + cvZero( grey ); + + cvCalcOpticalFlowBM( prev_grey, grey, + x_blocksize, x_shiftsize, + x_maxrange, x_useprevious, + x_velx, x_vely ); + + nbblocks = 0; + for( py=0; py= x_threshold) + { + cvLine( grey, orig, dest, CV_RGB(0,255,0), (int)hypotenuse/10, CV_AA, 0 ); + + orig.x = (int) (dest.x - (x_shiftsize.width/4) * cos(angle + M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/4) * sin(angle + M_PI / 4)); + cvLine( grey, orig, dest, CV_RGB(0,0,255), (int)hypotenuse/10, CV_AA, 0 ); + orig.x = (int) (dest.x - (x_shiftsize.width/4) * cos(angle - M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/4) * sin(angle - M_PI / 4)); + cvLine( grey, orig, dest, CV_RGB(0,0,255), (int)hypotenuse/10, CV_AA, 0 ); + meanx = (meanx*nbblocks+cvGet2D(x_velx, py, px).val[0])/(nbblocks+1); + meany = (meanx*nbblocks+cvGet2D(x_vely, py, px).val[0])/(nbblocks+1); + if ( hypotenuse > maxamp ) + { + maxamp = hypotenuse; + maxangle = angle; + } + // post( "pdp_opencv_of_bm : block %d : amp : %f : angle : %f", nbblocks, hypotenuse, (angle*180)/M_PI ); + nbblocks++; + } + + } + } + + meanangle=-atan2( meany, meanx ); + // post( "pdp_opencv_of_bm : meanangle : %f", (meanangle*180)/M_PI ); + + if ( nbblocks >= x_minblocks ) + { + orig.x = (int) (comp_xsize/2); + orig.y = (int) (comp_ysize/2); + dest.x = (int) (orig.x+((comp_xsize>comp_ysize)?comp_ysize/2:comp_xsize/2)*cos(meanangle)); + dest.y = (int) (orig.y-((comp_xsize>comp_ysize)?comp_ysize/2:comp_xsize/2)*sin(meanangle)); + cvLine( grey, orig, dest, CV_RGB(255,255,255), 3, CV_AA, 0 ); + orig.x = (int) (dest.x - (x_shiftsize.width/2) * cos(meanangle + M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/2) * sin(meanangle + M_PI / 4)); + cvLine( grey, orig, dest, CV_RGB(255,255,255), 3, CV_AA, 0 ); + orig.x = (int) (dest.x - (x_shiftsize.width/2) * cos(meanangle - M_PI / 4)); + orig.y = (int) (dest.y + (x_shiftsize.height/2) * sin(meanangle - M_PI / 4)); + cvLine( grey, orig, dest, CV_RGB(255,255,255), 3, CV_AA, 0 ); + + // outputs the average angle of movement + meanangle = (meanangle*180)/M_PI; + SETFLOAT(&x_list[0], meanangle); + outlet_list( m_meanout, 0, 1, x_list ); + + // outputs the amplitude and angle of the maximum movement + maxangle = (maxangle*180)/M_PI; + SETFLOAT(&x_list[0], maxamp); + SETFLOAT(&x_list[1], maxangle); + outlet_list( m_maxout, 0, 2, x_list ); + } + + memcpy( prev_grey->imageData, grey->imageData, image.xsize*image.ysize ); + + memcpy( image.data, grey->imageData, image.xsize*image.ysize ); +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// + +void pix_opencv_of_bm :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_of_bm::nightModeMessCallback, + gensym("nightmode"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_of_bm::tresholdMessCallback, + gensym("threshold"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_of_bm::blocksizeMessCallback, + gensym("blocksize"), A_FLOAT, A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_of_bm::shiftsizeMessCallback, + gensym("shiftsize"), A_FLOAT, A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_of_bm::maxrangeMessCallback, + gensym("maxrange"), A_FLOAT, A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_of_bm::usePreviousMessCallback, + gensym("useprevious"), A_FLOAT, A_NULL); + class_addmethod(classPtr, (t_method)&pix_opencv_of_bm::minBlocksMessCallback, + gensym("minblocks"), A_FLOAT, A_NULL); +} + +void pix_opencv_of_bm :: nightModeMessCallback(void *data, t_floatarg nightmode) +{ + GetMyClass(data)->nightModeMess((float)nightmode); +} + +void pix_opencv_of_bm :: tresholdMessCallback(void *data, t_floatarg threshold) +{ + GetMyClass(data)->tresholdMess((float)threshold); +} + +void pix_opencv_of_bm :: blocksizeMessCallback(void *data, t_floatarg fwidth, t_floatarg fheight) +{ + GetMyClass(data)->blocksizeMess((float)fwidth, (float)fheight); +} + +void pix_opencv_of_bm :: shiftsizeMessCallback(void *data, t_floatarg fwidth, t_floatarg fheight) +{ + GetMyClass(data)->shiftsizeMess((float)fwidth, (float)fheight); +} + +void pix_opencv_of_bm :: maxrangeMessCallback(void *data, t_floatarg fwidth, t_floatarg fheight) +{ + GetMyClass(data)->maxrangeMess((float)fwidth, (float)fheight); +} + +void pix_opencv_of_bm :: usePreviousMessCallback(void *data, t_floatarg previous) +{ + GetMyClass(data)->usePreviousMess((float)previous); +} + +void pix_opencv_of_bm :: minBlocksMessCallback(void *data, t_floatarg minblocks) +{ + GetMyClass(data)->minBlocksMess((float)minblocks); +} + +void pix_opencv_of_bm :: nightModeMess(float nightmode) +{ + if ( ( (int)nightmode==0 ) || ( (int)nightmode==1 ) ) x_nightmode = (int)nightmode; +} + +void pix_opencv_of_bm :: tresholdMess(float threshold) +{ + if ( (int)threshold>0 ) x_threshold = (int)threshold; +} + +void pix_opencv_of_bm :: blocksizeMess(float fwidth, float fheight) +{ + if (fwidth>=5.0) x_blocksize.width = (int)fwidth; + if (fheight>=5.0) x_blocksize.height = (int)fheight; + + x_velsize.width = (comp_xsize-x_blocksize.width)/x_shiftsize.width; + x_velsize.height = (comp_ysize-x_blocksize.height)/x_shiftsize.height; + cvReleaseImage( &x_velx ); + cvReleaseImage( &x_vely ); + x_velx = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + x_vely = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + +} + +void pix_opencv_of_bm :: shiftsizeMess(float fwidth, float fheight) +{ + if (fwidth>=5.0) x_shiftsize.width = (int)fwidth; + if (fheight>=5.0) x_shiftsize.height = (int)fheight; + + x_velsize.width = (comp_xsize-x_blocksize.width)/x_shiftsize.width; + x_velsize.height = (comp_ysize-x_blocksize.height)/x_shiftsize.height; + cvReleaseImage( &x_velx ); + cvReleaseImage( &x_vely ); + x_velx = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + x_vely = cvCreateImage( x_velsize, IPL_DEPTH_32F, 1 ); + +} + +void pix_opencv_of_bm :: maxrangeMess(float fwidth, float fheight) +{ + if (fwidth>=5.0) x_maxrange.width = (int)fwidth; + if (fheight>=5.0) x_maxrange.height = (int)fheight; +} + +void pix_opencv_of_bm :: usePreviousMess(float previous) +{ + if ((previous==0.0)||(previous==1.0)) x_useprevious = (int)previous; +} + +void pix_opencv_of_bm :: minBlocksMess(float minblocks) +{ + if (minblocks>=1.0) x_minblocks = (int)minblocks; +} + -- cgit v1.2.1