From 19ba359f3bac851ce20b952bf14ca23c6db1495f Mon Sep 17 00:00:00 2001 From: "N.N." Date: Sat, 4 Jul 2009 16:30:51 +0000 Subject: added pix_opencv_histo svn path=/trunk/externals/pix_opencv/; revision=11848 --- pix_opencv_histo.cc | 510 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 510 insertions(+) create mode 100755 pix_opencv_histo.cc (limited to 'pix_opencv_histo.cc') diff --git a/pix_opencv_histo.cc b/pix_opencv_histo.cc new file mode 100755 index 0000000..5bd5758 --- /dev/null +++ b/pix_opencv_histo.cc @@ -0,0 +1,510 @@ +//////////////////////////////////////////////////////// +// +// 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_histo.h" + +CPPEXTERN_NEW(pix_opencv_histo) + +///////////////////////////////////////////////////////// +// +// pix_opencv_histo +// +///////////////////////////////////////////////////////// +// Constructor +// +///////////////////////////////////////////////////////// + +pix_opencv_histo :: pix_opencv_histo() +{ + int i; + + comp_xsize=320; + comp_ysize=240; + + inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("save")); + m_dataout = outlet_new(this->x_obj, &s_anything); + + rgba = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 4); + rgb = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3); + grey = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + hsv = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3 ); + + h_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + s_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + v_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + planes[0] = h_plane; + planes[1] = s_plane; + cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); + h_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + s_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + v_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + saved_planes[0] = h_saved_plane; + saved_planes[1] = s_saved_plane; + + int h_bins = (int)(comp_xsize/10), s_bins = (int)(comp_ysize/10); + { + int hist_size[] = { h_bins, s_bins }; + float h_ranges[] = { 0, 180 }; // hue is [0,180] + float s_ranges[] = { 0, 255 }; + float* ranges[] = { h_ranges, s_ranges }; + hist = cvCreateHist( + 2, + hist_size, + CV_HIST_ARRAY, + ranges, + 1 + ); + int n; + for (n=0; ncomp_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(&rgba); + cvReleaseImage(&rgb); + cvReleaseImage(&grey); + cvReleaseImage(&h_plane); + cvReleaseImage(&s_plane); + cvReleaseImage(&v_plane); + cvReleaseImage(&h_saved_plane); + cvReleaseImage(&s_saved_plane); + cvReleaseImage(&v_saved_plane); + + //Create cv_images + rgba = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 4); + rgb = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3); + grey = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + hsv = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3 ); + + h_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + s_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + v_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + planes[0] = h_plane; + planes[1] = s_plane; + cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); + h_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + s_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + v_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + saved_planes[0] = h_saved_plane; + saved_planes[1] = s_saved_plane; + + h_bins = (int)(comp_xsize/10); + s_bins = (int)(comp_ysize/10); + { + int hist_size[] = { h_bins, s_bins }; + float h_ranges[] = { 0, 180 }; // hue is [0,180] + float s_ranges[] = { 0, 255 }; + float* ranges[] = { h_ranges, s_ranges }; + hist = cvCreateHist( + 2, + hist_size, + CV_HIST_ARRAY, + ranges, + 1 + ); + int n; + for (n=0; nimageData, image.data, image.xsize*image.ysize*4 ); + + // Convert to hsv + cvCvtColor( rgba, rgb, CV_BGRA2BGR ); + cvCvtColor( rgb, hsv, CV_BGR2HSV ); + cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); + + // Build the histogram and compute its contents. + if (save_now>=0) { + post("saving histogram %d\n",save_now); + cvCvtPixToPlane( hsv, h_saved_plane, s_saved_plane, v_saved_plane, 0 ); + cvCalcHist( saved_planes, saved_hist[save_now], 0, 0 ); //Compute histogram + cvNormalizeHist( saved_hist[save_now], 1.0 ); //Normalize it + save_now=-1; + nbsaved++; + } + cvCalcHist( planes, hist, 0, 0 ); //Compute histogram + cvNormalizeHist( hist, 1.0 ); //Normalize it + + double tato[MAX_HISTOGRAMS_TO_COMPARE]; + int nearest = -1; + double max = 0; + + int n; + if ( nbsaved > 0 ) + for (n=0; nmax) { + max = tato[n]; + nearest = n; + } + } + + outlet_float(m_dataout, (float)nearest); + + // Create an image to use to visualize our histogram. + int scale = 10; + // populate our visualization with little gray squares. + float max_value = 0; + cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 ); + + int h = 0; + int s = 0; + + for( h = 0; h < h_bins; h++ ) { + for( s = 0; s < s_bins; s++ ) { + float bin_val = cvQueryHistValue_2D( hist, h, s ); + int intensity = cvRound( bin_val * 255 / max_value ); + cvRectangle( + rgba, + cvPoint( h*scale, s*scale ), + cvPoint( (h+1)*scale - 1, (s+1)*scale - 1), + CV_RGB(intensity,intensity,intensity), CV_FILLED, 8 , 0 ); + } + } + + memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 ); + +} + +void pix_opencv_histo :: processRGBImage(imageStruct &image) +{ + int i; + int h_bins = (int)(comp_xsize/10), s_bins = (int)(comp_ysize/10); + + 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(&rgba); + cvReleaseImage(&rgb); + cvReleaseImage(&grey); + cvReleaseImage(&h_plane); + cvReleaseImage(&s_plane); + cvReleaseImage(&v_plane); + cvReleaseImage(&h_saved_plane); + cvReleaseImage(&s_saved_plane); + cvReleaseImage(&v_saved_plane); + + //Create cv_images + rgba = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 4); + rgb = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3); + grey = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + hsv = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3 ); + + h_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + s_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + v_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + planes[0] = h_plane; + planes[1] = s_plane; + cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); + h_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + s_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + v_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + saved_planes[0] = h_saved_plane; + saved_planes[1] = s_saved_plane; + + h_bins = (int)(comp_xsize/10); + s_bins = (int)(comp_ysize/10); + { + int hist_size[] = { h_bins, s_bins }; + float h_ranges[] = { 0, 180 }; // hue is [0,180] + float s_ranges[] = { 0, 255 }; + float* ranges[] = { h_ranges, s_ranges }; + hist = cvCreateHist( + 2, + hist_size, + CV_HIST_ARRAY, + ranges, + 1 + ); + int n; + for (n=0; nimageData, image.data, image.xsize*image.ysize*3 ); + + // Convert to hsv + cvCvtColor( rgb, hsv, CV_BGR2HSV ); + cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); + + // Build the histogram and compute its contents. + if (save_now>=0) { + post("saving histogram %d\n",save_now); + cvCvtPixToPlane( hsv, h_saved_plane, s_saved_plane, v_saved_plane, 0 ); + cvCalcHist( saved_planes, saved_hist[save_now], 0, 0 ); //Compute histogram + cvNormalizeHist( saved_hist[save_now], 1.0 ); //Normalize it + save_now=-1; + nbsaved++; + } + cvCalcHist( planes, hist, 0, 0 ); //Compute histogram + cvNormalizeHist( hist, 1.0 ); //Normalize it + + double tato[MAX_HISTOGRAMS_TO_COMPARE]; + int nearest = -1; + double max = 0; + + int n; + if ( nbsaved > 0 ) + for (n=0; nmax) { + max = tato[n]; + nearest = n; + } + } + + outlet_float(m_dataout, (float)nearest); + + // Create an image to use to visualize our histogram. + int scale = 10; + // populate our visualization with little gray squares. + float max_value = 0; + cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 ); + + int h = 0; + int s = 0; + + for( h = 0; h < h_bins; h++ ) { + for( s = 0; s < s_bins; s++ ) { + float bin_val = cvQueryHistValue_2D( hist, h, s ); + int intensity = cvRound( bin_val * 255 / max_value ); + cvRectangle( + rgb, + cvPoint( h*scale, s*scale ), + cvPoint( (h+1)*scale - 1, (s+1)*scale - 1), + CV_RGB(intensity,intensity,intensity), CV_FILLED, 8 , 0 ); + } + } + + memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 ); + +} + +void pix_opencv_histo :: processYUVImage(imageStruct &image) +{ +} + +void pix_opencv_histo :: processGrayImage(imageStruct &image) +{ + int i; + int h_bins = (int)(comp_xsize/10), s_bins = (int)(comp_ysize/10); + + 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(&rgba); + cvReleaseImage(&rgb); + cvReleaseImage(&grey); + cvReleaseImage(&h_plane); + cvReleaseImage(&s_plane); + cvReleaseImage(&v_plane); + cvReleaseImage(&h_saved_plane); + cvReleaseImage(&s_saved_plane); + cvReleaseImage(&v_saved_plane); + + //Create cv_images + rgba = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 4); + rgb = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3); + grey = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + hsv = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3 ); + + h_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + s_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + v_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + planes[0] = h_plane; + planes[1] = s_plane; + cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); + h_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + s_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + v_saved_plane = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1); + saved_planes[0] = h_saved_plane; + saved_planes[1] = s_saved_plane; + + h_bins = (int)(comp_xsize/10); + s_bins = (int)(comp_ysize/10); + { + int hist_size[] = { h_bins, s_bins }; + float h_ranges[] = { 0, 180 }; // hue is [0,180] + float s_ranges[] = { 0, 255 }; + float* ranges[] = { h_ranges, s_ranges }; + hist = cvCreateHist( + 2, + hist_size, + CV_HIST_ARRAY, + ranges, + 1 + ); + int n; + for (n=0; nimageData, image.data, image.xsize*image.ysize ); + + // Convert to hsv + cvCvtColor( grey, rgb, CV_GRAY2BGR ); + cvCvtColor( rgb, hsv, CV_BGR2HSV ); + cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); + + // Build the histogram and compute its contents. + if (save_now>=0) { + post("saving histogram %d\n",save_now); + cvCvtPixToPlane( hsv, h_saved_plane, s_saved_plane, v_saved_plane, 0 ); + cvCalcHist( saved_planes, saved_hist[save_now], 0, 0 ); //Compute histogram + cvNormalizeHist( saved_hist[save_now], 1.0 ); //Normalize it + save_now=-1; + nbsaved++; + } + cvCalcHist( planes, hist, 0, 0 ); //Compute histogram + cvNormalizeHist( hist, 1.0 ); //Normalize it + + double tato[MAX_HISTOGRAMS_TO_COMPARE]; + int nearest = -1; + double max = 0; + + int n; + if ( nbsaved > 0 ) + for (n=0; nmax) { + max = tato[n]; + nearest = n; + } + } + + outlet_float(m_dataout, (float)nearest); + + // Create an image to use to visualize our histogram. + int scale = 10; + // populate our visualization with little gray squares. + float max_value = 0; + cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 ); + + int h = 0; + int s = 0; + + for( h = 0; h < h_bins; h++ ) { + for( s = 0; s < s_bins; s++ ) { + float bin_val = cvQueryHistValue_2D( hist, h, s ); + int intensity = cvRound( bin_val * 255 / max_value ); + cvRectangle( + grey, + cvPoint( h*scale, s*scale ), + cvPoint( (h+1)*scale - 1, (s+1)*scale - 1), + CV_RGB(intensity,intensity,intensity), CV_FILLED, 8 , 0 ); + } + } + + memcpy( image.data, grey->imageData, image.xsize*image.ysize ); + +} + +///////////////////////////////////////////////////////// +// static member function +// +///////////////////////////////////////////////////////// + +void pix_opencv_histo :: obj_setupCallback(t_class *classPtr) +{ + class_addmethod(classPtr, (t_method)&pix_opencv_histo::saveMessCallback, + gensym("save"), A_FLOAT, A_NULL); +} + +void pix_opencv_histo :: saveMess(float index) +{ + if (((int)index>=0)&&((int)indexsaveMess(index); +} -- cgit v1.2.1