The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

cvHistWrapper.cpp

Go to the documentation of this file.
00001 #include "cvHistWrapper.h"
00002 #include <fstream>
00003 #include <cmath>
00004 #include "Images/color.h"
00005 #include "Images/imageio.h"
00006 
00007 using namespace std;
00008 
00009 MPColorTools m_colorTools;
00010 MPImageIO m_imageIO;
00011 
00012 /* ================================================================ */
00013 
00014 void cvHistWrapper::update_back_project( const IplImage* cur_frame ) {  
00015         
00016         CvSize size = cvGetSize(cur_frame);
00017         if (m_log_back_project.width != size.width || m_log_back_project.height != size.height)
00018                 m_log_back_project.setSize(size.width, size.height);
00019         
00020         
00021         uchar* data = 0;      
00022         uchar* color_data = 0;
00023         int x, y, color_step = 0, plane_step = 0;
00024         int dims, histsize[CV_MAX_DIM];  
00025         dims = cvGetDims( m_hist->bins, histsize );
00026         
00027         cvReleaseImage( &m_temp );
00028         m_temp = cvCreateImage( size, IPL_DEPTH_8U, 3 );
00029         
00030         cvCvtColor( cur_frame, m_temp, CV_BGR2HSV );
00031         cvGetRawData( m_temp, &color_data, &color_step, &size );
00032         
00033         for(y=0 ; size.height--; y++, color_data += color_step)
00034         {
00035                 for( x = 0; x < size.width; x++ )
00036                 {
00037                         int val0 = color_data[x*3];
00038                         int val1 = color_data[x*3+1];
00039                         int val2 = color_data[x*3+2];
00040                         if( m_min_ch_val[0] <= val0 && val0 <= m_max_ch_val[0] &&
00041                                 m_min_ch_val[1] <= val1 && val1 <= m_max_ch_val[1] &&
00042                                 m_min_ch_val[2] <= val2 && val2 <= m_max_ch_val[2] ) {
00043                                 double prob = cvQueryHistValue_1D( m_hist, m_histlookuptable[val0]);
00044                                 m_log_back_project.setPixel(x, y, prob);
00045                         } else {
00046                                 static float minVal = 0.0;
00047                                 cvGetMinMaxHistValue( m_hist, &minVal, 0 );
00048                                 m_log_back_project.setPixel(x, y, minVal);
00049                         }
00050                 }
00051         }
00052 }
00053 
00054 /* ================================================================ */
00055 
00056 void cvHistWrapper::create_lookup_table(int *tab, CvHistogram* hist) {
00057         int dims, histsize[CV_MAX_DIM];  
00058         dims = cvGetDims( hist->bins, histsize );
00059         
00060         int step = ((CvMatND*)(hist->bins))->dim[0].step/sizeof(float);
00061         
00062         int is_sparse = CV_IS_SPARSE_HIST( hist );
00063         int have_range = CV_HIST_HAS_RANGES(hist);
00064         
00065         double a = have_range ? hist->thresh[0][0] : 0;
00066         double b = have_range ? hist->thresh[0][1] : 256;
00067         
00068         double scale = histsize[0]/(b-a), cur = 0;
00069         for (int i = 0; i < 256; i++) {
00070                 int idx = cvFloor(cur);
00071                 if ((unsigned)idx < histsize[0])
00072                         idx *= step;
00073                 else
00074                         idx = -1;//ICV_HIST_DUMMY_IDX;
00075                 
00076                 tab[i] = idx;
00077                 cur += scale;
00078         }
00079 }
00080 
00081 /* ================================================================ */
00082 
00083 bool cvHistWrapper::update_histogram( const IplImage* cur_frame ) {
00084         int i;
00085         int dims, histsize[CV_MAX_DIM];  
00086         dims = cvGetDims( m_hist->bins, histsize );
00087         bool status = CvCamShiftTracker::update_histogram( cur_frame );
00088         
00089         cvNormalizeHist( m_hist, 1.0 );
00090         
00091         const double uniform = 1.0/histsize[0];
00092         const double alpha = .9999999999;
00093 
00094         /* TODO: should adding uniform to protect against log(0) error be done like this? */
00095         for (i = 0; i < histsize[0]; i++) {
00096                 float *val = cvGetHistValue_1D( m_hist, i );
00097                 *val = alpha*(*val) + (1-alpha)*uniform;
00098         }
00099 
00100         const double expBoostWeight = 1.0; /* bad idea to use anything higher than 1 */
00101 
00102         if (m_expBoost) {
00103                 for (i = 0; i < histsize[0]; i++) {
00104                         float *val = cvGetHistValue_1D( m_hist, i );
00105                         *val = exp(expBoostWeight * *val);
00106                 }
00107                 cvNormalizeHist( m_hist, 1.0 );
00108         }
00109         
00110 
00111         /* TODO: use a separate histogram to store log prob ratio */
00112         for (i = 0; i < histsize[0]; i++) {
00113                 float *val = cvGetHistValue_1D( m_hist, i );
00114                 *val = log(*val) - log(uniform);
00115         }
00116         
00117         create_lookup_table(m_histlookuptable, m_hist);
00118 
00119         FILE *fid = fopen("hist.txt", "w");
00120         for (i = 0; i < histsize[0]; i++) {
00121                 float *val = cvGetHistValue_1D( m_hist, i );
00122                 fprintf(fid, "%g\n", *val);
00123         }
00124         fclose(fid);
00125         
00126         return(status);
00127 }
00128 
00129 /* ================================================================ */
00130 
00131 void cvHistWrapper::process(MPImageWrapper & imageWrapper, const long int & frameNum, MPKeyFrameCollection *collection) {
00132         unsigned char*   pData = imageWrapper.rgbimage;
00133         IplImage image;
00134         CvSize size = cvSize( imageWrapper.width, imageWrapper.height );
00135         int stride = (size.width * 3 + 3) & -4;
00136         cvInitImageHeader( &image, size, IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4 );
00137         cvSetImageData( &image, pData, stride );
00138         
00139         static const float m_params_x = 0.4f; 
00140         static const float m_params_y = 0.3f;
00141         static const float m_params_width = 0.2f;
00142         static const float m_params_height = 0.3f;
00143         static const int m_params_Smin = 20;
00144         static const int m_params_Vmin = 40;
00145         static const int m_params_Vmax = 255;
00146         static const int m_params_bins = 20;
00147         static const int m_params_view = 0;
00148         static const int m_params_threshold = 0;
00149         
00150         long fn = -1;
00151         int x = 0, y = 0, w = 0, h = 0;
00152         for (int s = 0; s < collection->size(); s++) {
00153                 collection->get_keyframe(fn, x, y, w, h, s);
00154                 if (frameNum == fn) {
00155                         m_object.x = x;
00156                         m_object.y = y;
00157                         m_object.width = w;
00158                         m_object.height = h;
00159                         break;
00160                 }
00161         }
00162         
00163         {
00164                 IplImage* _image = &image;
00165                 CvSize size;
00166                 int bins = m_params_bins;
00167                 set_hist_dims( 1, &bins );
00168                 set_hist_bin_range( 0, 0, 180 );
00169                 set_threshold( 0 );
00170                 set_min_ch_val( 1, m_params_Smin );
00171                 set_max_ch_val( 1, 255 );
00172                 set_min_ch_val( 2, m_params_Vmin );
00173                 set_max_ch_val( 2, m_params_Vmax );
00174                 cvGetImageRawData( _image, 0, 0, &size );
00175                 
00176                 if( m_object.x < 0 ) m_object.x = 0;
00177                 if( m_object.x > size.width - m_object.width - 1 )
00178                         m_object.x = MAX(0, size.width - m_object.width - 1);
00179                 if( m_object.y < 0 ) m_object.y = 0;
00180                 if( m_object.y > size.height - m_object.height - 1 )
00181                         m_object.y = MAX(0, size.height - m_object.height - 1);
00182                 if( m_object.width > size.width - m_object.x )
00183                         m_object.width = MIN(size.width, size.width - m_object.x);
00184                 if( m_object.height > size.height - m_object.y )
00185                         m_object.height = MIN(size.height, size.height - m_object.y);
00186                 
00187                 set_window(m_object);
00188                 
00189                 if (frameNum == fn) {
00190                         reset_histogram();
00191                         update_histogram( _image );
00192                 }
00193                 update_back_project( _image );
00194         }
00195 }
00196 
00197 /* ================================================================ */
00198         
00199 void cvHistWrapper::set_expBoost(const bool val) {
00200         m_expBoost = val;
00201 }
00202 
00203 /* ================================================================ */

Generated on Mon Nov 8 17:07:32 2004 for MPT by  doxygen 1.3.9.1