From 10d9a412a7a5d56e5061f1003d1fc244c7322197 Mon Sep 17 00:00:00 2001 From: Antoine Villeret Date: Fri, 7 Dec 2012 17:57:13 +0000 Subject: - add contours and matchshape to build process - update help file svn path=/trunk/externals/pix_opencv/; revision=16651 --- Makefile | 3 +- pix_opencv_contours-help.pd | 73 +++++------ pix_opencv_contours.cc | 179 +++++++++++++++++++++++++++ pix_opencv_contours.cpp | 178 --------------------------- pix_opencv_contours_convexity-help.pd | 2 +- pix_opencv_matchshape.cc | 221 ++++++++++++++++++++++++++++++++++ pix_opencv_matchshape.cpp | 221 ---------------------------------- 7 files changed, 441 insertions(+), 436 deletions(-) create mode 100644 pix_opencv_contours.cc delete mode 100644 pix_opencv_contours.cpp create mode 100644 pix_opencv_matchshape.cc delete mode 100644 pix_opencv_matchshape.cpp diff --git a/Makefile b/Makefile index fd46c16..44aad22 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ endif .SUFFIXES = $(EXTENSION) -SOURCES = pix_opencv_edge.cc pix_opencv_laplace.cc pix_opencv_morphology.cc pix_opencv_distrans.cc pix_opencv_motempl.cc pix_opencv_haarcascade.cc pix_opencv_contours_boundingrect.cc pix_opencv_bgsubstract.cc pix_opencv_contours_convexity.cc pix_opencv_dft.cc pix_opencv_lk.cc pix_opencv_hist_compare.cc pix_opencv_knear.cc pix_opencv_threshold.cc pix_opencv_floodfill.cc pix_opencv_athreshold.cc pix_opencv_bgstats.cc pix_opencv_camshift.cc pix_opencv_hu_compare.cc pix_opencv_pgh_compare.cc pix_opencv_hough_circles.cc pix_opencv_hough_lines.cc pix_opencv_hu_moments.cc pix_opencv_contours_convexhull.cc pix_opencv_colorfilt.cc pix_opencv_of_bm.cc pix_opencv_of_hs.cc pix_opencv_of_lk.cc pix_opencv_calibration.cc pix_opencv_warpperspective.cc pix_opencv_findchessboardcorners.cc pix_opencv_contours.cpp pix_opencv_matchshape.cpp +SOURCES = pix_opencv_edge.cc pix_opencv_laplace.cc pix_opencv_morphology.cc pix_opencv_distrans.cc pix_opencv_motempl.cc pix_opencv_haarcascade.cc pix_opencv_contours_boundingrect.cc pix_opencv_bgsubstract.cc pix_opencv_contours_convexity.cc pix_opencv_dft.cc pix_opencv_lk.cc pix_opencv_hist_compare.cc pix_opencv_knear.cc pix_opencv_threshold.cc pix_opencv_floodfill.cc pix_opencv_athreshold.cc pix_opencv_bgstats.cc pix_opencv_camshift.cc pix_opencv_hu_compare.cc pix_opencv_pgh_compare.cc pix_opencv_hough_circles.cc pix_opencv_hough_lines.cc pix_opencv_hu_moments.cc pix_opencv_contours_convexhull.cc pix_opencv_colorfilt.cc pix_opencv_of_bm.cc pix_opencv_of_hs.cc pix_opencv_of_lk.cc pix_opencv_calibration.cc pix_opencv_warpperspective.cc pix_opencv_findchessboardcorners.cc pix_opencv_contours.cc pix_opencv_matchshape.cc all: $(SOURCES:.cc=.$(EXTENSION)) $(SOURCES_OPT:.cc=.$(EXTENSION)) blobtrack @@ -39,7 +39,6 @@ all: $(SOURCES:.cc=.$(EXTENSION)) $(SOURCES_OPT:.cc=.$(EXTENSION)) blobtrack gcc $(LDFLAGS) -o $*.$(EXTENSION) $*.o $(LIBS) .cc.o: - echo .cc.o target g++ $(CPPFLAGS) $(INCLUDES) -o $*.o -c $*.cc .c.o: diff --git a/pix_opencv_contours-help.pd b/pix_opencv_contours-help.pd index 895cb9c..dd0a77f 100644 --- a/pix_opencv_contours-help.pd +++ b/pix_opencv_contours-help.pd @@ -1,11 +1,11 @@ -#N canvas 711 354 1125 496 10; +#N canvas 703 374 1125 496 10; #X obj 472 38 cnv 15 220 250 empty empty empty 20 12 0 14 -228992 -66577 0; #X obj 11 56 cnv 15 450 100 empty empty empty 20 12 0 14 -233017 -66577 0; -#X obj 12 207 cnv 15 450 130 empty empty empty 20 12 0 14 -233017 -66577 +#X obj 12 207 cnv 15 450 230 empty empty empty 20 12 0 14 -233017 -66577 0; -#X text 13 212 Inlets:; +#X text 14 210 Inlets:; #X obj 12 167 cnv 15 450 30 empty empty empty 20 12 0 14 -195568 -66577 0; #X text 21 166 Arguments:; @@ -23,7 +23,7 @@ image \, simplifies it according to the epsilon value and outputs contours with area greater than area value on second outlet; #X obj 487 171 pix_gray; #X obj 487 141 pix_buf; -#N canvas 880 297 919 782 drawcontours 0; +#N canvas 872 273 919 782 drawcontours 0; #X obj 249 179 list split 2; #X obj 422 675 GEMglEnd; #X obj 461 611 GEMglVertex2d; @@ -140,7 +140,6 @@ with area greater than area value on second outlet; #X restore 575 281 pd color; #X obj 249 117 t a b; #X obj 500 375 GEMglLineWidth 2; -#X obj 249 64 inlet; #X obj 500 344 scaleXYZ 1 1 1; #X obj 472 564 * 10.6666; #X obj 472 584 - 5.33333; @@ -155,42 +154,43 @@ with area greater than area value on second outlet; #X msg 500 214 0; #X obj 204 208 unpack 0 0; #X floatatom 204 229 5 0 0 0 nb_of_segments - -; -#X connect 0 0 28 0; +#X obj 249 62 inlet CONTOUR; +#X connect 0 0 27 0; #X connect 0 1 8 0; -#X connect 2 0 21 1; +#X connect 2 0 20 1; #X connect 3 0 5 0; #X connect 4 0 3 0; #X connect 4 1 7 0; #X connect 5 0 4 0; #X connect 5 1 3 1; -#X connect 6 0 17 0; +#X connect 6 0 16 0; #X connect 6 1 10 0; -#X connect 7 0 22 0; +#X connect 7 0 21 0; #X connect 7 1 6 0; -#X connect 8 0 21 0; +#X connect 8 0 20 0; #X connect 8 1 5 0; -#X connect 8 2 25 0; -#X connect 9 0 16 0; +#X connect 8 2 24 0; +#X connect 9 0 15 0; #X connect 10 0 11 0; #X connect 11 0 2 2; #X connect 12 0 9 1; #X connect 13 0 0 0; #X connect 13 1 12 0; -#X connect 14 0 23 0; -#X connect 15 0 13 0; -#X connect 16 0 14 0; -#X connect 17 0 18 0; -#X connect 18 0 2 1; -#X connect 19 0 16 2; -#X connect 20 0 24 0; -#X connect 21 0 1 0; -#X connect 22 0 2 0; -#X connect 23 0 22 1; -#X connect 24 0 12 1; -#X connect 25 0 9 0; -#X connect 26 0 27 0; -#X connect 27 0 25 0; -#X connect 28 0 29 0; +#X connect 14 0 22 0; +#X connect 15 0 14 0; +#X connect 16 0 17 0; +#X connect 17 0 2 1; +#X connect 18 0 15 2; +#X connect 19 0 23 0; +#X connect 20 0 1 0; +#X connect 21 0 2 0; +#X connect 22 0 21 1; +#X connect 23 0 12 1; +#X connect 24 0 9 0; +#X connect 25 0 26 0; +#X connect 26 0 24 0; +#X connect 27 0 28 0; +#X connect 29 0 13 0; #X restore 575 315 pd drawcontours; #X obj 487 76 t b a a; #X obj 487 57 gemhead 1; @@ -228,8 +228,7 @@ with area greater than area value on second outlet; #X text 355 214 Create window:; #X text 31 224 Inlet 1: binarized image; #X msg 625 141 2; -#X obj 487 240 pix_opencv_contours_convexhull; -#N canvas 1201 172 474 845 draw_rotrect 0; +#N canvas 1199 177 474 845 draw_rotrect 0; #X obj 71 506 GEMglVertex2d; #X obj 71 586 GEMglVertex2d; #X obj 71 666 GEMglVertex2d; @@ -421,33 +420,39 @@ with area greater than area value on second outlet; #X text 31 353 ; -#X msg 359 235 create; #X text 355 214 Create window:; #X text 31 224 Inlet 1: binarized image; -#X text 25 304 Outlet 2: data output; #X text 21 343 info; #X text 21 393 contour; #X text 31 403 <2> ; #X obj 575 265 route contour; #X obj 673 265 route info; +#X obj 487 240 pix_opencv_contours; +#X text 26 302 Outlet 2: contour output; +#X text 26 292 Outlet 1: gemlist output; +#X text 26 312 Outlet 3: info output; #X connect 14 0 24 0; -#X connect 16 0 36 0; +#X connect 16 0 45 0; #X connect 17 0 16 0; #X connect 19 0 17 0; #X connect 19 1 26 0; #X connect 19 2 18 1; #X connect 20 0 19 0; -#X connect 21 0 36 0; +#X connect 21 0 45 0; #X connect 22 0 21 0; #X connect 23 0 35 0; #X connect 23 0 29 0; #X connect 25 0 17 0; #X connect 25 1 14 0; #X connect 26 0 25 0; -#X connect 27 0 36 0; +#X connect 27 0 45 0; #X connect 28 0 27 0; #X connect 29 0 28 0; #X connect 31 0 32 0; #X connect 32 0 31 0; #X connect 35 0 22 0; +#X connect 43 0 18 0; +#X connect 44 0 36 0; +#X connect 45 1 43 0; +#X connect 45 2 44 0; diff --git a/pix_opencv_contours.cc b/pix_opencv_contours.cc new file mode 100644 index 0000000..842cbf2 --- /dev/null +++ b/pix_opencv_contours.cc @@ -0,0 +1,179 @@ +//////////////////////////////////////////////////////// +// +// 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. +// +///////////////////////////////////////////////////////// +// based on code written by Lluis Gomez i Bigorda ( lluisgomez _at_ hangar _dot_ org ) (pix_opencv) +// pix_opencv_contours extract and simplify contours of incomming image +// by Antoine Villeret - 2012 + +#include "pix_opencv_contours.h" +#include +#include + + +CPPEXTERN_NEW(pix_opencv_contours) + +///////////////////////////////////////////////////////// +// +// pix_opencv_contours +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_contours :: pix_opencv_contours() : m_area_threshold(30) +{ + m_dataout_middle = outlet_new(this->x_obj, 0); + m_dataout_right = outlet_new(this->x_obj, 0); + + //~ post("build on %s at %s", __DATE__, __TIME__); +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_contours :: ~pix_opencv_contours() +{ +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_contours :: processRGBAImage(imageStruct &image) +{ + error( "pix_opencv_contours : rgba format not supported" ); +} + +void pix_opencv_contours :: processRGBImage(imageStruct &image) { + error( "pix_opencv_contours : rgb format not supported"); +} + +void pix_opencv_contours :: processYUVImage(imageStruct &image) { + error( "pix_opencv_contours : yuv format not supported" ); +} + +void pix_opencv_contours :: processGrayImage(imageStruct &image) +{ + if ( image.xsize < 0 || image.ysize < 0 ) return; + + cv::Mat imgMat2( image.ysize, image.xsize, CV_8UC1, image.data, image.csize*image.xsize); // just transform imageStruct to IplImage without copying data + + cv::Mat imgMat = imgMat2.clone(); + + std::vector > contours; + std::vector one_contour; + + contours.clear(); + m_contours.clear(); + + cv::findContours(imgMat, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); + + for( size_t i = 0; i < contours.size(); i++ ) { + if ( cv::contourArea(contours[i], false) > m_area_threshold ){ + one_contour.clear(); + cv::approxPolyDP(contours[i], one_contour, m_epsilon, true); + m_contours.push_back(one_contour); + } + } + //~ cv::drawContours(imgMat2, m_contours, -1, cv::Scalar(128,255,255), 3); + + t_atom*info; + info = new t_atom[(int) m_contours.size()*14+2]; + // info : 14x(contour_nb) matrix + // info for each contour : area, rotrect corner (8 float), rotrect center, rotrect size, rotation angle + int count(0); + SETFLOAT(info+1, 14.); + int info_offset(2); + + for( std::vector >::iterator it = m_contours.begin(); it != m_contours.end(); ++it ) { + if (!it->empty() && it->size() > 2) { + SETFLOAT(info+info_offset, (float) cv::contourArea(*it)); + + cv::RotatedRect rot_rect = cv::minAreaRect(*it); + cv::Point2f corners[4]; + rot_rect.points(corners); + for (int j=0;j<4;j++) { + SETFLOAT(info+info_offset+j*2+1, corners[j].x/image.xsize); + SETFLOAT(info+info_offset+j*2+2, corners[j].y/image.ysize); + } + + SETFLOAT(info+info_offset+9, rot_rect.center.x/image.xsize); + SETFLOAT(info+info_offset+10, rot_rect.center.y/image.ysize); + SETFLOAT(info+info_offset+11, rot_rect.size.width/image.xsize); + SETFLOAT(info+info_offset+12, rot_rect.size.height/image.xsize); + SETFLOAT(info+info_offset+13, rot_rect.angle); + + info_offset+=14; + count++; + } + } + SETFLOAT(info, (float) count); + if (count) outlet_anything(m_dataout_right, gensym("info"), count*14+2, info); + else outlet_list(m_dataout_right, gensym("info"), 1, 0); + + for( std::vector >::iterator it = m_contours.begin(); it != m_contours.end(); ++it ) { + if (!it->empty() && it->size() > 2) { + int size = 2+it->size()*2; + t_atom*ap = new t_atom[size]; + SETFLOAT(ap, static_cast(it->size())); + SETFLOAT(ap+1, 2.0); + + int offset(2); + + for ( std::vector::iterator ite=it->begin(); ite!=it->end(); ++ite){ + SETFLOAT(ap+offset,(float) (*ite).x/image.xsize); + SETFLOAT(ap+offset+1,(float) (*ite).y/image.ysize); + offset+=2; + } + outlet_anything(m_dataout_middle, gensym("contour"), size, ap); + if(ap)delete[]ap;ap=NULL; + + } + } + + if (info) delete info; + info = NULL; +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_contours :: obj_setupCallback(t_class *classPtr) +{ + CPPEXTERN_MSG1(classPtr, "epsilon", epsilonMess, double); + CPPEXTERN_MSG1(classPtr, "area", areaMess, double); +} + +///////////////////////////////////////////////////////// +// messages handling +// +///////////////////////////////////////////////////////// +void pix_opencv_contours :: epsilonMess(double arg) +{ + m_epsilon = arg > 0 ? arg : 3.; + t_atom data_out; + SETFLOAT(&data_out, (float) m_epsilon); + outlet_anything( m_dataout_right, gensym("epsilon"), 1, &data_out); +} +void pix_opencv_contours :: areaMess(double arg) +{ + m_area_threshold = arg > 0 ? arg : 30.; + t_atom data_out; + SETFLOAT(&data_out, (float) m_area_threshold); + outlet_anything( m_dataout_right, gensym("area"), 1, &data_out); +} diff --git a/pix_opencv_contours.cpp b/pix_opencv_contours.cpp deleted file mode 100644 index 0632c57..0000000 --- a/pix_opencv_contours.cpp +++ /dev/null @@ -1,178 +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. -// -///////////////////////////////////////////////////////// -// based on code written by Lluis Gomez i Bigorda ( lluisgomez _at_ hangar _dot_ org ) (pix_opencv) -// Template for pix_opencv class - -#include "pix_opencv_contours.h" -#include -#include - - -CPPEXTERN_NEW(pix_opencv_contours) - -///////////////////////////////////////////////////////// -// -// pix_opencv_contours -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_contours :: pix_opencv_contours() : m_area_threshold(30) -{ - m_dataout_middle = outlet_new(this->x_obj, 0); - m_dataout_right = outlet_new(this->x_obj, 0); - - //~ post("build on %s at %s", __DATE__, __TIME__); -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_contours :: ~pix_opencv_contours() -{ -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_contours :: processRGBAImage(imageStruct &image) -{ - error( "pix_opencv_contours : rgba format not supported" ); -} - -void pix_opencv_contours :: processRGBImage(imageStruct &image) { - error( "pix_opencv_contours : rgb format not supported"); -} - -void pix_opencv_contours :: processYUVImage(imageStruct &image) { - error( "pix_opencv_contours : yuv format not supported" ); -} - -void pix_opencv_contours :: processGrayImage(imageStruct &image) -{ - if ( image.xsize < 0 || image.ysize < 0 ) return; - - cv::Mat imgMat2( image.ysize, image.xsize, CV_8UC1, image.data, image.csize*image.xsize); // just transform imageStruct to IplImage without copying data - - cv::Mat imgMat = imgMat2.clone(); - - std::vector > contours; - std::vector one_contour; - - contours.clear(); - m_contours.clear(); - - cv::findContours(imgMat, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); - - for( size_t i = 0; i < contours.size(); i++ ) { - if ( cv::contourArea(contours[i], false) > m_area_threshold ){ - one_contour.clear(); - cv::approxPolyDP(contours[i], one_contour, m_epsilon, true); - m_contours.push_back(one_contour); - } - } - //~ cv::drawContours(imgMat2, m_contours, -1, cv::Scalar(128,255,255), 3); - - t_atom*info; - info = new t_atom[(int) m_contours.size()*14+2]; - // info : 14x(contour_nb) matrix - // info for each contour : area, rotrect corner (8 float), rotrect center, rotrect size, rotation angle - int count(0); - SETFLOAT(info+1, 14.); - int info_offset(2); - - for( std::vector >::iterator it = m_contours.begin(); it != m_contours.end(); ++it ) { - if (!it->empty() && it->size() > 2) { - SETFLOAT(info+info_offset, (float) cv::contourArea(*it)); - - cv::RotatedRect rot_rect = cv::minAreaRect(*it); - cv::Point2f corners[4]; - rot_rect.points(corners); - for (int j=0;j<4;j++) { - SETFLOAT(info+info_offset+j*2+1, corners[j].x/image.xsize); - SETFLOAT(info+info_offset+j*2+2, corners[j].y/image.ysize); - } - - SETFLOAT(info+info_offset+9, rot_rect.center.x/image.xsize); - SETFLOAT(info+info_offset+10, rot_rect.center.y/image.ysize); - SETFLOAT(info+info_offset+11, rot_rect.size.width/image.xsize); - SETFLOAT(info+info_offset+12, rot_rect.size.height/image.xsize); - SETFLOAT(info+info_offset+13, rot_rect.angle); - - info_offset+=14; - count++; - } - } - SETFLOAT(info, (float) count); - if (count) outlet_anything(m_dataout_right, gensym("info"), count*14+2, info); - else outlet_list(m_dataout_right, gensym("info"), 1, 0); - - for( std::vector >::iterator it = m_contours.begin(); it != m_contours.end(); ++it ) { - if (!it->empty() && it->size() > 2) { - int size = 2+it->size()*2; - t_atom*ap = new t_atom[size]; - SETFLOAT(ap, static_cast(it->size())); - SETFLOAT(ap+1, 2.0); - - int offset(2); - - for ( std::vector::iterator ite=it->begin(); ite!=it->end(); ++ite){ - SETFLOAT(ap+offset,(float) (*ite).x/image.xsize); - SETFLOAT(ap+offset+1,(float) (*ite).y/image.ysize); - offset+=2; - } - outlet_anything(m_dataout_middle, gensym("contour"), size, ap); - if(ap)delete[]ap;ap=NULL; - - } - } - - if (info) delete info; - info = NULL; -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_contours :: obj_setupCallback(t_class *classPtr) -{ - CPPEXTERN_MSG1(classPtr, "epsilon", epsilonMess, double); - CPPEXTERN_MSG1(classPtr, "area", areaMess, double); -} - -///////////////////////////////////////////////////////// -// messages handling -// -///////////////////////////////////////////////////////// -void pix_opencv_contours :: epsilonMess(double arg) -{ - m_epsilon = arg > 0 ? arg : 3.; - t_atom data_out; - SETFLOAT(&data_out, (float) m_epsilon); - outlet_anything( m_dataout_right, gensym("epsilon"), 1, &data_out); -} -void pix_opencv_contours :: areaMess(double arg) -{ - m_area_threshold = arg > 0 ? arg : 30.; - t_atom data_out; - SETFLOAT(&data_out, (float) m_area_threshold); - outlet_anything( m_dataout_right, gensym("area"), 1, &data_out); -} diff --git a/pix_opencv_contours_convexity-help.pd b/pix_opencv_contours_convexity-help.pd index 9204814..e1ddab2 100644 --- a/pix_opencv_contours_convexity-help.pd +++ b/pix_opencv_contours_convexity-help.pd @@ -1,4 +1,4 @@ -#N canvas 231 355 1068 671 10; +#N canvas 229 360 1068 671 10; #X obj 17 217 cnv 15 430 450 empty empty empty 20 12 0 14 -233017 -66577 0; #X obj 17 171 cnv 15 430 40 empty empty empty 20 12 0 14 -195568 -66577 diff --git a/pix_opencv_matchshape.cc b/pix_opencv_matchshape.cc new file mode 100644 index 0000000..75dc910 --- /dev/null +++ b/pix_opencv_matchshape.cc @@ -0,0 +1,221 @@ +//////////////////////////////////////////////////////// +// +// 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. +// +///////////////////////////////////////////////////////// +// based on code written by Lluis Gomez i Bigorda ( lluisgomez _at_ hangar _dot_ org ) (pix_opencv) +// Template for pix_opencv class + +#include "pix_opencv_matchshape.h" +#include +#include + +using namespace std; + + +CPPEXTERN_NEW(pix_opencv_matchshape) + +///////////////////////////////////////////////////////// +// +// pix_opencv_matchshape +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// +pix_opencv_matchshape :: pix_opencv_matchshape() : m_threshold(2), m_method(CV_CONTOURS_MATCH_I1) +{ + m_dataout = outlet_new(this->x_obj, 0); + m_template_vec_vec.clear(); + m_template_vec_mat.clear(); +} + +///////////////////////////////////////////////////////// +// Destructor +// +///////////////////////////////////////////////////////// +pix_opencv_matchshape :: ~pix_opencv_matchshape() +{ +} + +///////////////////////////////////////////////////////// +// processImage +// +///////////////////////////////////////////////////////// +void pix_opencv_matchshape :: processRGBAImage(imageStruct &image) +{ + error( "pix_opencv_matchshape : rgba format not supported" ); +} + +void pix_opencv_matchshape :: processRGBImage(imageStruct &image) { + error( "pix_opencv_matchshape : rgb format not supported"); +} + +void pix_opencv_matchshape :: processYUVImage(imageStruct &image) +{ + error( "pix_opencv_matchshape : yuv format not supported" ); +} + +void pix_opencv_matchshape :: processGrayImage(imageStruct &image) +{ + error( "pix_opencv_matchshape : gray format not supported" ); + error( "pix_opencv_matchshape only works on pd lists" ); + + // TODO add support to compare grayscale images +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// +void pix_opencv_matchshape :: obj_setupCallback(t_class *classPtr) +{ + CPPEXTERN_MSG0(classPtr, "clear", clearMess); + CPPEXTERN_MSG(classPtr, "template", templateMess); + CPPEXTERN_MSG(classPtr, "contour", contourMess); + CPPEXTERN_MSG1(classPtr, "threshold", thresholdMess, float); + CPPEXTERN_MSG1(classPtr, "method", methodMess, int); +} + +void pix_opencv_matchshape :: clearMess(void) +{ + for ( vector >::iterator it=m_template_vec_vec.end(); it!=m_template_vec_vec.begin(); it--) + { + (*it).clear(); + } + m_template_vec_vec.clear(); + m_template_vec_mat.clear(); + + t_atom data; + SETFLOAT(&data, (float)m_template_vec_vec.size()); + outlet_anything(m_dataout, gensym("template_vec"), 1, &data); + + SETFLOAT(&data, (float)m_template_vec_mat.size()); + outlet_anything(m_dataout, gensym("template_mat"), 1, &data); +} + + +void pix_opencv_matchshape :: contourMess(t_symbol*s, int argc, t_atom*argv) +{ + if ( m_template_vec_vec.empty() ){ + error("no template to match, please load a template before matching"); + return; + } + + if ( (argc%2)!=0 ) { + error("template message argument should be multiple of 2"); + return; + } + + for (int i=0;i tmp_contour; + for (int i=2;i >::iterator it=m_template_vec_vec.begin(); it!=m_template_vec_vec.end(); ++it){ + comp = cv::matchShapes(tmp_contour, *it, m_method, 0.); + if ( comp < min ){ + min=comp; + id=i; + } + i++; + } + + if ( min < m_threshold ) + { + t_atom data[2]; + SETFLOAT(data,id); + SETFLOAT(data+1, min); + outlet_anything(m_dataout, gensym("match_vec"), 2, data); + } +} + +void pix_opencv_matchshape :: templateMess(t_symbol*s, int argc, t_atom*argv) +{ + if ( (argc%2)!=0 ) { + error("template message argument should be multiple of 2"); + return; + } + for (int i=0;i tmp_contour; + tmp_contour.clear(); + for (int i=2;i0.?arg:0.); + t_atom data; + SETFLOAT(&data, m_threshold); + outlet_anything(m_dataout, gensym("threshold"), 1, &data); +} + +void pix_opencv_matchshape :: methodMess(int arg) +{ + switch (arg) { + case 1: + m_method=CV_CONTOURS_MATCH_I1; + break; + case 2: + m_method=CV_CONTOURS_MATCH_I2; + break; + case 3: + m_method=CV_CONTOURS_MATCH_I3; + break; + default: + error("method should be 1, 2 or 3"); + m_method=CV_CONTOURS_MATCH_I1; + } + t_atom data; + switch (m_method) { + case CV_CONTOURS_MATCH_I1: + SETFLOAT(&data, 1); + break; + case CV_CONTOURS_MATCH_I2: + SETFLOAT(&data, 2); + break; + case CV_CONTOURS_MATCH_I3: + SETFLOAT(&data, 3); + break; + } + outlet_anything(m_dataout, gensym("method"), 1, &data); +} diff --git a/pix_opencv_matchshape.cpp b/pix_opencv_matchshape.cpp deleted file mode 100644 index 75dc910..0000000 --- a/pix_opencv_matchshape.cpp +++ /dev/null @@ -1,221 +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. -// -///////////////////////////////////////////////////////// -// based on code written by Lluis Gomez i Bigorda ( lluisgomez _at_ hangar _dot_ org ) (pix_opencv) -// Template for pix_opencv class - -#include "pix_opencv_matchshape.h" -#include -#include - -using namespace std; - - -CPPEXTERN_NEW(pix_opencv_matchshape) - -///////////////////////////////////////////////////////// -// -// pix_opencv_matchshape -// -///////////////////////////////////////////////////////// -// Constructor -// -///////////////////////////////////////////////////////// -pix_opencv_matchshape :: pix_opencv_matchshape() : m_threshold(2), m_method(CV_CONTOURS_MATCH_I1) -{ - m_dataout = outlet_new(this->x_obj, 0); - m_template_vec_vec.clear(); - m_template_vec_mat.clear(); -} - -///////////////////////////////////////////////////////// -// Destructor -// -///////////////////////////////////////////////////////// -pix_opencv_matchshape :: ~pix_opencv_matchshape() -{ -} - -///////////////////////////////////////////////////////// -// processImage -// -///////////////////////////////////////////////////////// -void pix_opencv_matchshape :: processRGBAImage(imageStruct &image) -{ - error( "pix_opencv_matchshape : rgba format not supported" ); -} - -void pix_opencv_matchshape :: processRGBImage(imageStruct &image) { - error( "pix_opencv_matchshape : rgb format not supported"); -} - -void pix_opencv_matchshape :: processYUVImage(imageStruct &image) -{ - error( "pix_opencv_matchshape : yuv format not supported" ); -} - -void pix_opencv_matchshape :: processGrayImage(imageStruct &image) -{ - error( "pix_opencv_matchshape : gray format not supported" ); - error( "pix_opencv_matchshape only works on pd lists" ); - - // TODO add support to compare grayscale images -} - -///////////////////////////////////////////////////////// -// static member function -// -///////////////////////////////////////////////////////// -void pix_opencv_matchshape :: obj_setupCallback(t_class *classPtr) -{ - CPPEXTERN_MSG0(classPtr, "clear", clearMess); - CPPEXTERN_MSG(classPtr, "template", templateMess); - CPPEXTERN_MSG(classPtr, "contour", contourMess); - CPPEXTERN_MSG1(classPtr, "threshold", thresholdMess, float); - CPPEXTERN_MSG1(classPtr, "method", methodMess, int); -} - -void pix_opencv_matchshape :: clearMess(void) -{ - for ( vector >::iterator it=m_template_vec_vec.end(); it!=m_template_vec_vec.begin(); it--) - { - (*it).clear(); - } - m_template_vec_vec.clear(); - m_template_vec_mat.clear(); - - t_atom data; - SETFLOAT(&data, (float)m_template_vec_vec.size()); - outlet_anything(m_dataout, gensym("template_vec"), 1, &data); - - SETFLOAT(&data, (float)m_template_vec_mat.size()); - outlet_anything(m_dataout, gensym("template_mat"), 1, &data); -} - - -void pix_opencv_matchshape :: contourMess(t_symbol*s, int argc, t_atom*argv) -{ - if ( m_template_vec_vec.empty() ){ - error("no template to match, please load a template before matching"); - return; - } - - if ( (argc%2)!=0 ) { - error("template message argument should be multiple of 2"); - return; - } - - for (int i=0;i tmp_contour; - for (int i=2;i >::iterator it=m_template_vec_vec.begin(); it!=m_template_vec_vec.end(); ++it){ - comp = cv::matchShapes(tmp_contour, *it, m_method, 0.); - if ( comp < min ){ - min=comp; - id=i; - } - i++; - } - - if ( min < m_threshold ) - { - t_atom data[2]; - SETFLOAT(data,id); - SETFLOAT(data+1, min); - outlet_anything(m_dataout, gensym("match_vec"), 2, data); - } -} - -void pix_opencv_matchshape :: templateMess(t_symbol*s, int argc, t_atom*argv) -{ - if ( (argc%2)!=0 ) { - error("template message argument should be multiple of 2"); - return; - } - for (int i=0;i tmp_contour; - tmp_contour.clear(); - for (int i=2;i0.?arg:0.); - t_atom data; - SETFLOAT(&data, m_threshold); - outlet_anything(m_dataout, gensym("threshold"), 1, &data); -} - -void pix_opencv_matchshape :: methodMess(int arg) -{ - switch (arg) { - case 1: - m_method=CV_CONTOURS_MATCH_I1; - break; - case 2: - m_method=CV_CONTOURS_MATCH_I2; - break; - case 3: - m_method=CV_CONTOURS_MATCH_I3; - break; - default: - error("method should be 1, 2 or 3"); - m_method=CV_CONTOURS_MATCH_I1; - } - t_atom data; - switch (m_method) { - case CV_CONTOURS_MATCH_I1: - SETFLOAT(&data, 1); - break; - case CV_CONTOURS_MATCH_I2: - SETFLOAT(&data, 2); - break; - case CV_CONTOURS_MATCH_I3: - SETFLOAT(&data, 3); - break; - } - outlet_anything(m_dataout, gensym("method"), 1, &data); -} -- cgit v1.2.1