Main Page | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages | Examples

vetMotionIlluminationInvariant.cpp

Go to the documentation of this file.
00001 
00018 #include "vetMotionIlluminationInvariant.h"
00019 #include <math.h>
00020 
00021 
00022  #define RED_COEF .2989961801
00023  #define GREEN_COEF .5869947588
00024  #define BLUE_COEF .1139912943
00025 
00026 
00027 #include "../filters/vetDigitalFilter.h" // lowpass and average
00028 
00029 
00030 vetDFMatrix* gaussian = vetDFMatrix::createKernel_3x3(VETDF_3x3_gaussian);
00031 vetDFMatrix* myLowpass = vetDFMatrix::createKernel_3x3(VETDF_3x3_lowpass1);
00032 
00033 
00034 vetMotionIlluminationInvariant::vetMotionIlluminationInvariant(vetMotionIlluminationInvariantParameters* initParams) : vetVision()
00035  {
00036         INFO("vetMotionIlluminationInvariant::vetMotionIlluminationInvariant(..* initParams)  : vetMotion() [CONTRUCTOR]")
00037 
00038         buffer = NULL;
00039         reflectance = NULL;
00040 
00041         setParameters(initParams);
00042         reset();
00043  }
00044 
00045 vetMotionIlluminationInvariant::~vetMotionIlluminationInvariant()
00046  {
00047         INFO("vetMotionIlluminationInvariant::~vetMotionIlluminationInvariant() [DESTRUCTOR]")
00048 
00049         if (buffer != NULL)
00050                 delete buffer;
00051 
00052         if (reflectance != NULL)
00053                 delete reflectance;
00054 
00055 
00056         delete myParams;//BUG
00057 
00058  }
00059 
00060 VETRESULT vetMotionIlluminationInvariant::reset()
00061  {
00062         INFO("int vetMotionIlluminationInvariant::reset() [SET DEFAULT PARAMETERS]")
00063 
00064         if (buffer != NULL)
00065                 delete buffer;
00066         buffer = NULL;
00067 
00068         if (reflectance != NULL)
00069                 delete reflectance;
00070         reflectance = NULL;
00071 
00072         alertCall = NULL;
00073         alertCallArgument = NULL;
00074 
00075         lastDiffValue = 0;
00076 
00077         setName("Lame Motion Detection");
00078         setDescription("detect difference between 2 frames.");
00079         setVersion(1.0);
00080 
00081         return VETRET_OK;
00082  }
00083 
00084 
00085 VETRESULT vetMotionIlluminationInvariant::setParameters(vetMotionIlluminationInvariantParameters* initParams)
00086  {
00087         if ( initParams == NULL )
00088                 myParams = new vetMotionIlluminationInvariantParameters();
00089         else
00090                 myParams = initParams;
00091 
00092         return VETRET_OK;
00093  }
00094 
00095 
00096 
00097 
00098 void vetMotionIlluminationInvariant::initFrame(vetFrameRGB24& img)
00099  {
00100         if (buffer != NULL)
00101                 delete buffer;
00102 
00103         buffer = new vetFrameT<unsigned char>(img.width, img.height);
00104 
00105         if (reflectance != NULL)
00106                 delete reflectance;
00107 
00108         reflectance = new vetFrameT<unsigned char>(img.width, img.height);
00109         diff = new vetFrameT<unsigned char>(img.width, img.height);
00110 
00111         getReflectance(*reflectance, img);
00112 
00113         lastDiffValue = 0;
00114  }
00115 
00116 
00117 
00118 
00119 
00120 
00121 VETRESULT vetMotionIlluminationInvariant::importFrom(vetFrameRGB24& img)
00122  {
00123         INFO("int vetMotionIlluminationInvariant::importFrom(vetFrameRGB24& img) [reading data]")
00124 
00125         if (buffer == NULL)
00126          {
00127                 initFrame(img);
00128                 return VETRET_OK;
00129          }
00130 
00131         getReflectance(*buffer, img);
00132         getDifference(*diff,*buffer, *reflectance);
00133 
00134 
00135         if ( myParams->doEval && evalDifference(*diff) && myParams->doAlert )
00136                 doAlert();
00137 
00138         vetFrameT<unsigned char>* tmp = reflectance;
00139         reflectance = buffer;//bug
00140         buffer = tmp;
00141 
00142         return VETRET_OK;
00143  }
00144 
00145 
00146 
00147 
00148 VETRESULT vetMotionIlluminationInvariant::importFrom(vetFrameYUV420& img)
00149  {
00150         INFO("int vetMotionIlluminationInvariant::importFrom(vetFrameYUV420& img) [reading data]")
00151 
00152         return VETRET_NOT_IMPLEMENTED;
00153  }
00154 
00155 
00156 
00157 VETRESULT vetMotionIlluminationInvariant::importFrom(vetFrameT<unsigned char>& img)
00158  {
00159         INFO("int vetMotionIlluminationInvariant::importFrom(vetFrameT<unsigned char>& img) [reading data]")
00160 
00161         return VETRET_NOT_IMPLEMENTED;
00162  }
00163 
00164 
00165 
00166 int vetMotionIlluminationInvariant::evalDifference(vetFrameT<unsigned char>& diff_img)
00167  {
00168         INFO("")
00169 
00170         unsigned char* diff_ptr = diff_img.data;
00171         vetFrameT<unsigned char> tmp(diff_img.width, diff_img.height);
00172 
00173         // LP average 5x5
00174         vetDigitalFilter::doProcessing(diff_img, tmp, *myLowpass);
00175 
00176         lastDiffValue=0;
00177         for (unsigned int i=0; i< diff_img.width*diff_img.height; i++)
00178          {
00179                 lastDiffValue += (long)*diff_ptr;
00180          }
00181 
00182 
00183 //      if ( myParams->threshold > lastDiffValue )
00184 //              return 0;
00185 
00186         return lastDiffValue;
00187  }
00188 
00189 int vetMotionIlluminationInvariant::evalDifference(vetFrameRGB24& diff_img)
00190  {
00191         INFO("")
00192 
00193         vetFrameRGB24 tmp(diff_img.width, diff_img.height);
00194 
00195         // LP average 5x5
00196         vetDigitalFilter::doProcessing(diff_img, tmp, *myLowpass);
00197 
00198         lastDiffValue=0;
00199         for (unsigned int i=0; i< diff_img.width*diff_img.height; i++)
00200          {
00201                 lastDiffValue += (long)( (float)diff_img.data[i][0] * RED_COEF + (float)diff_img.data[i][0] * GREEN_COEF + (float)diff_img.data[i][0] * BLUE_COEF);
00202          }
00203 
00204 //      if ( myParams->threshold > lastDiffValue )
00205 //              return 0;
00206 
00207         return lastDiffValue;
00208  }
00209 
00210 
00211 
00212 
00213 
00214 VETRESULT vetMotionIlluminationInvariant::getReflectance(vetFrameT<unsigned char> &refl_img, vetFrameRGB24& source_img)
00215  {
00216         unsigned char* refl_ptr = refl_img.data;
00217         unsigned int i=0;
00218 
00219         vetFrameT<float> buffer(source_img.width, source_img.height);
00220         vetFrameT<float> lowpass(source_img.width, source_img.height);
00221 
00222         float* buffer_ptr = buffer.data;
00223         float* lowpass_ptr = lowpass.data;
00224 
00225 // refl_img <= log(source_img) = log(illumination) + log (reflectance)
00226 
00227         for (i=0; i< source_img.width*source_img.height; i++, buffer_ptr++)
00228          {
00229                 *buffer_ptr = (float)log( (float)source_img.data[i][0] * RED_COEF + (float)source_img.data[i][0] * GREEN_COEF + (float)source_img.data[i][0] * BLUE_COEF );
00230          }
00231 
00232 
00233 // l = LOWPASS(r) , illumination = exp( LOWPASS( log(source_img) ) )
00234 
00235         vetDigitalFilter::doProcessing(buffer, lowpass, *gaussian);
00236 
00237 
00238 // refl_img = refl_img - l , reflectance = exp( log(source_img) - LOWPASS( log(source_img) )
00239         buffer_ptr = buffer.data;
00240         for (i=0; i< source_img.width*source_img.height; i++, lowpass_ptr++, buffer_ptr++)
00241          {
00242                 *buffer_ptr -= *lowpass_ptr;
00243 //              *buffer_ptr = *lowpass_ptr - *buffer_ptr;
00244          }
00245 
00246 
00247 // refl_img <= exp(refl_img)
00248 
00249         buffer_ptr = buffer.data;
00250         for (i=0; i< source_img.width*source_img.height; i++, refl_ptr++, buffer_ptr++)
00251          {
00252                 *refl_ptr = (unsigned char) exp(*buffer_ptr);
00253          }
00254 
00255         return VETRET_OK;
00256  }
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00269 
00270 
00271 
00272 
00273 
00274 
00275 VETRESULT vetMotionIlluminationInvariant::getReflectance(vetFrameRGB24& source_img, vetFrameRGB24& refl_img)
00276  {
00277 
00278         unsigned char* refl_ptr = refl_img.data[0];
00279 
00280         vetFrameT<float> buffer(source_img.width, source_img.height);
00281         vetFrameT<float> lowpass(source_img.width, source_img.height);
00282 
00283         float* buffer_ptr = buffer.data;
00284         float* lowpass_ptr = lowpass.data;
00285 
00286         float tmp = 0;
00287         unsigned int i;
00288 
00289 // refl_img <= log(source_img) = log(illumination) + log (reflectance)
00290 
00291         for (i=0; i< source_img.width*source_img.height; i++, buffer_ptr++)
00292          {
00293 //              *buffer_ptr = log( (float)*source_ptr );
00294                 tmp = (float)( (float)source_img.data[i][0] * RED_COEF + (float)source_img.data[i][0] * GREEN_COEF + (float)source_img.data[i][0] * BLUE_COEF );
00295                 if (tmp)
00296                         *buffer_ptr = (float)log( tmp );
00297 
00298          }
00299 
00300 
00301 // l = LOWPASS(r) , illumination = exp( LOWPASS( log(source_img) ) )
00302 
00303         vetDigitalFilter::doProcessing(buffer, lowpass, *gaussian);
00304 
00305 
00306 // refl_img = refl_img - l , reflectance = exp( log(source_img) - LOWPASS( log(source_img) )
00307 
00308         buffer_ptr = buffer.data;
00309         for (i=0; i< source_img.width*source_img.height; i++, lowpass_ptr++, buffer_ptr++)
00310          {
00311 //              *buffer_ptr -= *lowpass_ptr;
00312                 *buffer_ptr = *lowpass_ptr - *buffer_ptr;
00313 
00314          }
00315 
00316 
00317 // refl_img <= exp(refl_img)
00318         buffer_ptr = buffer.data;
00319         for (i=0; i< source_img.width*source_img.height; i++)
00320          {
00321                 *refl_ptr = (unsigned char) exp(*buffer_ptr);
00322                 *(refl_ptr+1) = (unsigned char) exp(*buffer_ptr);
00323                 *(refl_ptr+2) = (unsigned char) exp(*buffer_ptr);
00324                 refl_ptr += 3;
00325                 buffer_ptr++;
00326          }
00327 
00328         return VETRET_OK;
00329  }
00330 
00331 
00332 VETRESULT vetMotionIlluminationInvariant::getIllumination(vetFrameRGB24& source_img, vetFrameRGB24& ill_img)
00333  {
00334         vetFrameT<float> buffer(source_img.width, source_img.height);
00335         vetFrameT<float> ill(source_img.width, source_img.height);
00336 
00337         float* buffer_ptr = buffer.data;
00338         float* ill_ptr = ill.data;
00339         unsigned char* ret_ptr = ill_img.data[0];
00340 
00341         float tmp;
00342         unsigned int i;
00343 
00344         for (i=0; i< source_img.width*source_img.height; i++, buffer_ptr++)
00345          {
00346                 tmp = (float)( (float)source_img.data[i][0] * RED_COEF + (float)source_img.data[i][0] * GREEN_COEF + (float)source_img.data[i][0] * BLUE_COEF);
00347                 if (tmp)
00348                         *buffer_ptr = (float)log( tmp );
00349          }
00350 
00351         vetDigitalFilter::doProcessing(buffer, ill, *gaussian);
00352 
00353         for (i=0; i< source_img.width*source_img.height; i++)
00354          {
00355                 *ret_ptr = (unsigned char) exp(*ill_ptr);
00356                 *(ret_ptr+1) = (unsigned char) exp(*ill_ptr);
00357                 *(ret_ptr+2) = (unsigned char) exp(*ill_ptr);
00358                 ret_ptr += 3;
00359                 ill_ptr++;
00360          }
00361 
00362         return VETRET_OK;
00363  }
00364 
00366 
00367 
00368 
00369 
00370 
00371 vetMotionIlluminationInvariantParameters::vetMotionIlluminationInvariantParameters(unsigned int threshold, bool doAlert)
00372  {
00373         reset();
00374 
00375         setDoAlert(doAlert);
00376         setThresholdValue(threshold);
00377  }
00378 
00379 void vetMotionIlluminationInvariantParameters::reset()
00380  {
00381         doAlert = false;
00382         threshold = 1;
00383  }
00384 
00385 
00386 void vetMotionIlluminationInvariantParameters::setThresholdValue(unsigned int value)
00387  {
00388         threshold = value;
00389  }
00390 
00391 
00392 
00393 VETRESULT vetMotionIlluminationInvariantParameters::saveToStreamXML(FILE *fp)
00394  {
00395 
00396         if ( fp == NULL )
00397                 return VETRET_PARAM_ERR;
00398 
00399         if( fprintf(fp, "<vetMotionIlluminationInvariantParameters>\n") == EOF )
00400                 return VETRET_INTERNAL_ERR;
00401 
00402         if ( fprintf(fp, "  <threshold=\"%u\" />\n", threshold) == EOF)
00403                 return VETRET_INTERNAL_ERR;
00404 
00405         if ( fprintf(fp, "  <doEval=\"%u\" />\n", (int)doEval) == EOF)
00406                 return VETRET_INTERNAL_ERR;
00407 
00408         if ( fprintf(fp, "  <doAlert=\"%u\" />\n", (int)doAlert) == EOF)
00409                 return VETRET_INTERNAL_ERR;
00410 
00411         if( fprintf(fp, "</vetMotionIlluminationInvariantParameters>\n") == EOF )
00412                 return VETRET_INTERNAL_ERR;
00413 
00414         return VETRET_OK;
00415  }
00416 
00417 
00418 VETRESULT vetMotionIlluminationInvariantParameters::loadFromStreamXML(FILE *fp)
00419  {
00420         if ( fscanf(fp, "<vetMotionIlluminationInvariantParameters>\n") == EOF )
00421                 throw "error in XML file, unable to import data.";
00422 
00423         if ( fscanf(fp, "  <threshold=\"%u\" />\n", &threshold) == EOF )
00424                 throw "error in XML file, unable to import data.";
00425 
00426         int boolTmp = 0;
00427         if ( fscanf(fp, "  <doEval=\"%u\" />\n", &boolTmp) == EOF )
00428                 throw "error in XML file, unable to import data.";
00429 
00430         if (boolTmp == 0)
00431                 doEval = false;
00432         else
00433                 doEval = true;
00434 
00435         boolTmp = 0;
00436         if ( fscanf(fp, "  <doAlert=\"%u\" />\n", &boolTmp) == EOF )
00437                 throw "error in XML file, unable to import data.";
00438 
00439         if (boolTmp == 0)
00440                 doAlert = false;
00441         else
00442                 doAlert = true;
00443 
00444         return VETRET_OK;
00445  }
00446 
00447 
00448 

Generated on Tue Jan 24 11:59:04 2006 for VETLib by  doxygen 1.4.4