#include #include /* Math.cpp Written by: Michael Götting (2006) University of Bielefeld AG Neuroinformatik mgoettin@techfak.uni-bielefeld.de */ using namespace std; namespace icl{ // {{{ Random functions: // this anonymous namespace holds utiliy functions, that are used only here namespace { template void uniform_image_random(Img *image, const Range &range, bool roiOnly){ for(int c=0;cgetChannels();++c){ ImgIterator it = roiOnly ? image->getROIIterator(c) : image->getIterator(c); while(it.inRegion()){ *it++ = Cast::cast( random(range.minVal, range.maxVal) ); } } } template void gaussian_image_random(Img *image,double mean, double var, const Range &range, bool roiOnly){ for(int c=0;cgetChannels();++c){ ImgIterator it = roiOnly ? image->getROIIterator(c) : image->getIterator(c); while(it.inRegion()){ *it++ = Cast::cast( gaussRandom(mean,var,range) ); } } } } void random(ImgBase *poImage, const Range &range, bool roiOnly){ ICLASSERT_RETURN( poImage ); switch(poImage->getDepth()){ #define ICL_INSTANTIATE_DEPTH(D) case depth##D: uniform_image_random(poImage->asImg(),range,roiOnly); break; ICL_INSTANTIATE_ALL_DEPTHS; #undef ICL_INSTANTIATE_DEPTH } } void gaussRandom(ImgBase *poImage, double mean, double var, const Range &minAndMax, bool roiOnly){ ICLASSERT_RETURN( poImage ); switch(poImage->getDepth()){ #define ICL_INSTANTIATE_DEPTH(D) case depth##D: gaussian_image_random(poImage->asImg(),mean,var,minAndMax,roiOnly); break; ICL_INSTANTIATE_ALL_DEPTHS; #undef ICL_INSTANTIATE_DEPTH } } //-------------------------------------------------------------------------- double gaussRandom(double mean, double var){ FUNCTION_LOG(""); double x1 = random(1.0); double x2 = random(1.0); return mean+var*var*sqrt(-2*var*log(x1))*cos(2*var*M_PI*x2); } /******************************************************** ** former implementation taken from the BCL ** float gaussRandom(float limit){ static int iset=0; static float gset; float fac,r,v1,v2; if (iset == 0) { do { v1=2*random(limit)-1; v2=2*random(limit)-1; r=v1*v1+v2*v2; } while (r >= 1); fac=sqrt(-2*log(r)/r); gset=v1*fac; iset=1; return (v2*fac); } else { iset=0; return (gset); } } ******************************************************/ // }}} // {{{ statistic functions // {{{ mean //-------------------------------------------------------------------------- template float mean(const T *pfData, int iDim) { FUNCTION_LOG(""); if (iDim < 1) {return -1;} //---- Variable initialisation ---- float fSum = 0; //---- Compute mean value ---- for(int i=0; i (const unsigned char*, int); template float mean (const int*, int); template float mean (const float*, int); //-------------------------------------------------------------------------- template float mean(const vector &vecData) { FUNCTION_LOG(""); //---- Compute mean value ---- return mean(&(vecData[0]), vecData.size()); } template float mean (const vector &); template float mean (const vector &); template float mean (const vector &); //-------------------------------------------------------------------------- template vector mean(const Img *poImg, int iChannel) { FUNCTION_LOG(""); //---- iVariable initialisation ---- vector vecMean; //---- Compute mean value ---- if (iChannel < 0) { for(int i=0;igetChannels();i++) { vecMean.push_back(mean((const T*) poImg->getDataPtr(i), poImg->getDim())); SUBSECTION_LOG("Mean for channel " << i << " = " << vecMean[i]); } } else { vecMean.push_back(mean((const T*) poImg->getDataPtr(iChannel), poImg->getDim())); SUBSECTION_LOG("Mean for channel " << iChannel << " = " << vecMean[0]); } //---- Return value ---- return vecMean; } template vector mean(const Img*, int); template vector mean(const Img*, int); //-------------------------------------------------------------------------- vector mean(const ImgBase *poImg, int iChannel) { FUNCTION_LOG(""); vector tmpVec; switch(poImg->getDepth()) { case depth8u: tmpVec = mean(poImg->asImg(), iChannel); break; case depth16s: tmpVec = mean(poImg->asImg(), iChannel); break; case depth32s: tmpVec = mean(poImg->asImg(), iChannel); break; case depth32f: tmpVec = mean(poImg->asImg(), iChannel); break; case depth64f: tmpVec = mean(poImg->asImg(), iChannel); break; } // Return return tmpVec; } // }}} // {{{ variance //-------------------------------------------------------------------------- template float variance(const T *pfData, int iDim) { FUNCTION_LOG(""); if (iDim < 1) { return -1;} //---- Variable initialisation ---- float fSum = 0, fMean; //---- Compute variance ---- fMean = mean(pfData, iDim); for(int i=0; i (const unsigned char*, int); template float variance (const int*, int); template float variance (const float*, int); //-------------------------------------------------------------------------- template float variance(const vector &vecData) { FUNCTION_LOG(""); // Compute variance return mean(&(vecData[0]), vecData.size()); } template float variance (const vector &); template float variance (const vector &); template float variance (const vector &); //-------------------------------------------------------------------------- template vector variance(const Img *poImg, int iChannel) { FUNCTION_LOG(""); //---- iVariable initialisation ---- vector vecVariance; //---- Compute variance value ---- if (iChannel < 0) { for(int i=0;igetChannels();i++) { vecVariance.push_back(variance((const T*) poImg->getDataPtr(i), poImg->getDim())); SUBSECTION_LOG("Variance for channel " << i << " = " << vecVariance[i]); } } else { vecVariance.push_back(variance((const T*) poImg->getDataPtr(iChannel), poImg->getDim())); SUBSECTION_LOG("Variance for channel " << iChannel << " = " << vecVariance[0]); } //---- Return value ---- return vecVariance; } template vector variance(const Img*, int); template vector variance(const Img*, int); //-------------------------------------------------------------------------- vector variance(const ImgBase *poImg, int iChannel) { FUNCTION_LOG(""); vector vecVar; switch(poImg->getDepth()) { case depth8u: vecVar = variance(poImg->asImg(), iChannel); break; case depth16s: vecVar = variance(poImg->asImg(), iChannel); break; case depth32s: vecVar = variance(poImg->asImg(), iChannel); break; case depth32f: vecVar = variance(poImg->asImg(), iChannel); break; case depth64f: vecVar = variance(poImg->asImg(), iChannel); break; } // Return return vecVar; } // }}} // {{{ deviation //-------------------------------------------------------------------------- template float deviation(const T *pfData, int iDim) { FUNCTION_LOG(""); if (iDim < 1) { return -1;} // Variable initialisation float fVariance = 0; // Compute variance fVariance = variance(pfData, iDim); // Return if (fVariance < 0) {return 0;} return sqrt(variance(pfData, iDim)); } template float deviation (const unsigned char*, int); template float deviation (const int*, int); template float deviation (const float*, int); //-------------------------------------------------------------------------- template float deviation(const vector &vecData) { FUNCTION_LOG(""); // Compute deviation return mean(&(vecData[0]), vecData.size()); } template float deviation (const vector &); template float deviation (const vector &); template float deviation (const vector &); //-------------------------------------------------------------------------- template vector deviation(const Img *poImg, int iChannel) { FUNCTION_LOG(""); //---- iVariable initialisation ---- vector vecDeviation; //---- Compute deviation value ---- if (iChannel < 0) { for(int i=0;igetChannels();i++) { vecDeviation.push_back(deviation((const T*) poImg->getDataPtr(i), poImg->getDim())); SUBSECTION_LOG("Deviation for channel " << i << " = " << vecDeviation[i]); } } else { vecDeviation.push_back(deviation((const T*) poImg->getDataPtr(iChannel), poImg->getDim())); SUBSECTION_LOG("Deviation for channel " << iChannel << " = " << vecDeviation[0]); } //---- Return value ---- return vecDeviation; } template vector deviation(const Img*, int); template vector deviation(const Img*, int); //-------------------------------------------------------------------------- vector deviation(const ImgBase *poImg, int iChannel) { FUNCTION_LOG(""); vector vecVar; switch(poImg->getDepth()) { case depth8u: vecVar = deviation(poImg->asImg(), iChannel); break; case depth16s: vecVar = deviation(poImg->asImg(), iChannel); break; case depth32s: vecVar = deviation(poImg->asImg(), iChannel); break; case depth32f: vecVar = deviation(poImg->asImg(), iChannel); break; case depth64f: vecVar = deviation(poImg->asImg(), iChannel); break; } // Return return vecVar; } // }}} // }}} } //namespace icl