/************************************************************************/ /* */ /* Copyright 2014 by Thorsten Beier and Ullrich Koethe */ /* */ /* This file is part of the VIGRA computer vision library. */ /* The VIGRA Website is */ /* http://hci.iwr.uni-heidelberg.de/vigra/ */ /* Please direct questions, bug reports, and contributions to */ /* ullrich.koethe@iwr.uni-heidelberg.de or */ /* vigra@informatik.uni-hamburg.de */ /* */ /* Permission is hereby granted, free of charge, to any person */ /* obtaining a copy of this software and associated documentation */ /* files (the "Software"), to deal in the Software without */ /* restriction, including without limitation the rights to use, */ /* copy, modify, merge, publish, distribute, sublicense, and/or */ /* sell copies of the Software, and to permit persons to whom the */ /* Software is furnished to do so, subject to the following */ /* conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the */ /* Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */ /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */ /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */ /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */ /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */ /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */ /* OTHER DEALINGS IN THE SOFTWARE. */ /* */ /************************************************************************/ /* RE-IMPLEMENTAION OF THE WORK OF: */ /* Pierrick Coupe - pierrick.coupe@gmail.com */ /* Jose V. Manjon - jmanjon@fis.upv.es */ /* Brain Imaging Center, Montreal Neurological Institute. */ /* Mc Gill University */ /* */ /************************************************************************/ /* The ONLM filter is described in: */ /* */ /* P. Coupe, P. Yger, S. Prima, P. Hellier, C. Kervrann, C. Barillot. */ /* An Optimized Blockwise Non Local Means Denoising Filter */ /* for 3D Magnetic Resonance Images */ /* . IEEE Transactions on Medical Imaging, 27(4):425-441, */ /* Avril 2008 */ /************************************************************************/ #ifndef VIGRA_NON_LOCAL_MEAN #define VIGRA_NON_LOCAL_MEAN /*std*/ #include /*vigra*/ #include "multi_array.hxx" #include "multi_convolution.hxx" #include "error.hxx" #include "threading.hxx" #include "gaussians.hxx" namespace vigra{ struct NonLocalMeanParameter{ NonLocalMeanParameter( const double sigmaSpatial = 2.0, const int searchRadius = 3, const int patchRadius = 1, const double sigmaMean = 1.0, const int stepSize = 2, const int iterations=1, const int nThreads = 8, const bool verbose = true ): sigmaSpatial_(sigmaSpatial), searchRadius_(searchRadius), patchRadius_(patchRadius), sigmaMean_(sigmaMean), stepSize_(stepSize), iterations_(iterations), nThreads_(nThreads), verbose_(verbose){ } double sigmaSpatial_; int searchRadius_; int patchRadius_; double sigmaMean_; int stepSize_; int iterations_; int nThreads_; bool verbose_; }; // this has no template since this should // be the same class for different dimensions // and pixel types class RatioPolicyParameter{ public: RatioPolicyParameter( const double sigma = 5.0, const double meanRatio = 0.95, const double varRatio = 0.5, const double epsilon = 0.00001 ): sigma_(sigma), meanRatio_(meanRatio), varRatio_(varRatio), epsilon_(epsilon){ } double sigma_; double meanRatio_; double varRatio_; double epsilon_; }; template class RatioPolicy{ public: typedef RatioPolicyParameter ParameterType; typedef typename NumericTraits::RealPromote PixelType; typedef typename NumericTraits::ValueType ValueType; RatioPolicy(const ParameterType & param) : meanRatio_(static_cast(param.meanRatio_)), varRatio_(static_cast(param.varRatio_)), epsilon_(static_cast(param.epsilon_)), sigmaSquared_(param.sigma_*param.sigma_){ } bool usePixel(const PixelType & meanA, const PixelType & varA)const{ return sum(meanA) > epsilon_ && sum(varA) > epsilon_; } bool usePixelPair( const PixelType & meanA, const PixelType & varA, const PixelType & meanB, const PixelType & varB )const{ // Compute mean ratio of mean and variance const ValueType m = mean(meanA/meanB); const ValueType v = mean(varA/varB); return (m > meanRatio_ && m < (1.0 / meanRatio_) && v > varRatio_ && v < (1.0 / varRatio_)); } ValueType distanceToWeight(const PixelType & /*meanA*/, const PixelType & /*varA*/, const ValueType distance){ return exp(-distance /sigmaSquared_); } private: ValueType meanRatio_; ValueType varRatio_; ValueType epsilon_; ValueType sigmaSquared_; }; // this has no template since this should // be the same class for different dimensions // and pixel types class NormPolicyParameter{ public: NormPolicyParameter( const double sigma = 5.0, const double meanDist = 0.95, const double varRatio = 0.5, const double epsilon = 0.00001 ): sigma_(sigma), meanDist_(meanDist), varRatio_(varRatio), epsilon_(epsilon){ } double sigma_; double meanDist_; double varRatio_; double epsilon_; }; template inline bool equal(const TinyVector & a,const TinyVector b){ for(int i=0;i struct BorderHelper; template< int DIM> struct BorderHelper{ template static bool isInside(const COORD & ,const IMAGE & ){ return true; } template static bool isOutside(const COORD & ,const IMAGE & ){ return false; } template static void mirrorIfIsOutsidePoint(COORD & ,IMAGE & ){ } }; template< int DIM> struct BorderHelper{ template static bool isInside(const COORD & c,const IMAGE & img){ return img.isInside(c); } template static bool isOutside(const COORD & c,const IMAGE & img){ return img.isOutside(c); } template static void mirrorIfIsOutsidePoint(COORD & coord,const IMAGE & img){ for(int c=0;c= img.shape(c)) coord[c] = 2 * img.shape(c) - coord[c] - 1; } } }; template class NormPolicy{ public: typedef NormPolicyParameter ParameterType; typedef typename NumericTraits::RealPromote PixelType; typedef typename NumericTraits::ValueType ValueType; NormPolicy(const ParameterType & param) : meanDist_(static_cast(param.meanDist_)), varRatio_(static_cast(param.varRatio_)), epsilon_(static_cast(param.epsilon_)), sigmaSquared_(param.sigma_*param.sigma_){ } bool usePixel(const PixelType & /*meanA*/, const PixelType & varA)const{ return sum(varA)>epsilon_; } bool usePixelPair( const PixelType & meanA, const PixelType & varA, const PixelType & meanB, const PixelType & varB )const{ // Compute mean ratio of mean and variance const ValueType m = squaredNorm(meanA-meanB); const ValueType v = mean(varA/varB); //std::cout<<"norms "< varRatio_ && v < (1.0 / varRatio_)); } ValueType distanceToWeight(const PixelType & /*meanA*/, const PixelType & /*varA*/, const ValueType distance){ return exp(-distance /sigmaSquared_); } private: ValueType meanDist_; ValueType varRatio_; ValueType epsilon_; ValueType sigmaSquared_; }; // UnstridedArrayTag template class BlockWiseNonLocalMeanThreadObject{ typedef PIXEL_TYPE_IN PixelTypeIn; typedef typename NumericTraits::RealPromote RealPromotePixelType; typedef typename NumericTraits::ValueType RealPromoteScalarType; typedef typename MultiArray::difference_type Coordinate; typedef NonLocalMeanParameter ParameterType; public: typedef void result_type; typedef MultiArrayView InArrayView; typedef MultiArrayView MeanArrayView; typedef MultiArrayView VarArrayView; typedef MultiArrayView EstimateArrayView; typedef MultiArrayView LabelArrayView; typedef std::vector BlockAverageVectorType; typedef std::vector BlockGaussWeightVectorType; typedef SMOOTH_POLICY SmoothPolicyType; // range type typedef TinyVector RangeType; //typedef boost::mutex MutexType; typedef threading::thread ThreadType; typedef threading::mutex MutexType; BlockWiseNonLocalMeanThreadObject( const InArrayView & inImage, MeanArrayView & meanImage, VarArrayView & varImage, EstimateArrayView & estimageImage, LabelArrayView & labelImage, const SmoothPolicyType & smoothPolicy, const ParameterType & param, const size_t nThreads, MutexType & estimateMutex, MultiArray<1,int> & progress ) : inImage_(inImage), meanImage_(meanImage), varImage_(varImage), estimageImage_(estimageImage), labelImage_(labelImage), smoothPolicy_(smoothPolicy), param_(param), lastAxisRange_(), threadIndex_(), nThreads_(nThreads), estimateMutexPtr_(&estimateMutex), progress_(progress), average_(std::pow( (double)(2*param.patchRadius_+1), DIM) ), gaussWeight_(std::pow( (double)(2*param.patchRadius_+1), DIM) ), shape_(inImage.shape()), totalSize_() { totalSize_ = 1; for(int dim=0;dim void processSinglePixel(const Coordinate & xyz); template void processSinglePair( const Coordinate & xyz,const Coordinate & nxyz,RealPromoteScalarType & wmax,RealPromoteScalarType & totalweight); template RealPromoteScalarType patchDistance(const Coordinate & xyz,const Coordinate & nxyz); template void patchExtractAndAcc(const Coordinate & xyz,const RealPromoteScalarType weight); template void patchAccMeanToEstimate(const Coordinate & xyz,const RealPromoteScalarType globalSum); bool isAlwaysInside(const Coordinate & coord)const{ const Coordinate r = (Coordinate(param_.searchRadius_) + Coordinate(param_.patchRadius_) +1 ); const Coordinate test1 = coord - r; const Coordinate test2 = coord + r; return inImage_.isInside(test1) && inImage_.isInside(test2); } void initalizeGauss(); void progressPrinter(const int counter); // array views InArrayView inImage_; MeanArrayView meanImage_; VarArrayView varImage_; EstimateArrayView estimageImage_; LabelArrayView labelImage_; // policy object SmoothPolicyType smoothPolicy_; // param obj. ParameterType param_; // thread related; RangeType lastAxisRange_; size_t threadIndex_; size_t nThreads_; MutexType * estimateMutexPtr_; MultiArrayView<1,int> progress_; // computations BlockAverageVectorType average_; BlockGaussWeightVectorType gaussWeight_; Coordinate shape_; size_t totalSize_; }; template inline void BlockWiseNonLocalMeanThreadObject::initalizeGauss(){ Coordinate xyz; const int pr = param_.patchRadius_; Gaussian gaussian(param_.sigmaSpatial_); int c=0; RealPromoteScalarType sum = RealPromoteScalarType(0.0); if(DIM==2){ for (xyz[1] = -pr; xyz[1] <=pr; ++xyz[1]) for (xyz[0] = -pr; xyz[0] <=pr; ++xyz[0],++c){ const RealPromoteScalarType distance = norm(xyz); const RealPromoteScalarType w =gaussian(distance); sum+=w; gaussWeight_[c]=w; } } if(DIM==3){ for (xyz[2] = -pr; xyz[2] <=pr; ++xyz[2]) for (xyz[1] = -pr; xyz[1] <=pr; ++xyz[1]) for (xyz[0] = -pr; xyz[0] <=pr; ++xyz[0],++c){ const RealPromoteScalarType distance = norm(xyz); const RealPromoteScalarType w =gaussian(distance); sum+=w; gaussWeight_[c]=w; } } if(DIM==4){ for (xyz[3] = -pr; xyz[3] <=pr; ++xyz[3]) for (xyz[2] = -pr; xyz[2] <=pr; ++xyz[2]) for (xyz[1] = -pr; xyz[1] <=pr; ++xyz[1]) for (xyz[0] = -pr; xyz[0] <=pr; ++xyz[0],++c){ const RealPromoteScalarType distance = norm(xyz); const RealPromoteScalarType w =gaussian(distance); sum+=w; gaussWeight_[c]=w; } } // normalize for(size_t i=0;i inline void BlockWiseNonLocalMeanThreadObject::progressPrinter(const int counter){ progress_[threadIndex_] = counter; if(threadIndex_==nThreads_-1){ if(counter%100 == 0){ int c=0; for(size_t ti=0;ti void BlockWiseNonLocalMeanThreadObject::operator()(){ const int start = lastAxisRange_[0]; const int end = lastAxisRange_[1]; const int stepSize = param_.stepSize_; this->initalizeGauss(); Coordinate xyz; int c=0; if(param_.verbose_ && threadIndex_==nThreads_-1){ std::cout<<"progress"; } if(DIM==2){ for (xyz[1] = start; xyz[1] < end; xyz[1] += stepSize) for (xyz[0] = 0; xyz[0] < shape_[0]; xyz[0] += stepSize){ if(isAlwaysInside(xyz)) this->processSinglePixel(xyz); else this->processSinglePixel(xyz); if(param_.verbose_) this->progressPrinter(c); ++c; } } if(DIM==3){ for (xyz[2] = start; xyz[2] < end; xyz[2] += stepSize) for (xyz[1] = 0; xyz[1] < shape_[1]; xyz[1] += stepSize) for (xyz[0] = 0; xyz[0] < shape_[0]; xyz[0] += stepSize){ if(isAlwaysInside(xyz)) this->processSinglePixel(xyz); else this->processSinglePixel(xyz); if(param_.verbose_) this->progressPrinter(c); ++c; } } if(DIM==4){ for (xyz[3] = start; xyz[3] < end; xyz[3] += stepSize) for (xyz[2] = 0; xyz[2] < shape_[2]; xyz[2] += stepSize) for (xyz[1] = 0; xyz[1] < shape_[1]; xyz[1] += stepSize) for (xyz[0] = 0; xyz[0] < shape_[0]; xyz[0] += stepSize){ if(isAlwaysInside(xyz)) this->processSinglePixel(xyz); else this->processSinglePixel(xyz); if(param_.verbose_) this->progressPrinter(c); ++c; } } if(param_.verbose_ && threadIndex_==nThreads_-1){ std::cout<<"\rprogress "< template inline void BlockWiseNonLocalMeanThreadObject::processSinglePixel( const Coordinate & xyz ){ Coordinate nxyz(SkipInitialization); std::fill(average_.begin(),average_.end(),RealPromotePixelType(0.0)); RealPromoteScalarType totalweight = 0.0; if(smoothPolicy_.usePixel(meanImage_[xyz],varImage_[xyz])){ RealPromoteScalarType wmax = 0.0; const Coordinate start = xyz- Coordinate(param_.searchRadius_); const Coordinate end = xyz+ Coordinate(param_.searchRadius_); if(DIM==2){ for (nxyz[1] = start[1]; nxyz[1] <= end[1]; nxyz[1]++) for (nxyz[0] = start[0]; nxyz[0] <= end[0]; nxyz[0]++){ //nxyz = xyz + nxyz; if(equal(nxyz,xyz)) continue; this->processSinglePair(xyz,nxyz,wmax,totalweight); } } else if(DIM==3){ for (nxyz[2] = start[2]; nxyz[2] <= end[2]; nxyz[2]++) for (nxyz[1] = start[1]; nxyz[1] <= end[1]; nxyz[1]++) for (nxyz[0] = start[0]; nxyz[0] <= end[0]; nxyz[0]++){ if(equal(nxyz,xyz)) continue; this->processSinglePair(xyz,nxyz,wmax,totalweight); } } else if(DIM==4){ for (nxyz[3] = start[3]; nxyz[3] <= end[3]; nxyz[3]++) for (nxyz[2] = start[2]; nxyz[2] <= end[2]; nxyz[2]++) for (nxyz[1] = start[1]; nxyz[1] <= end[1]; nxyz[1]++) for (nxyz[0] = start[0]; nxyz[0] <= end[0]; nxyz[0]++){ if(equal(nxyz,xyz)) continue; this->processSinglePair(xyz,nxyz,wmax,totalweight); } } if (wmax == 0.0){ wmax = 1.0; } // give pixel xyz as much weight as // the maximum weighted other patch this->patchExtractAndAcc(xyz,wmax); totalweight += wmax; // this if seems total useless to me if (totalweight != 0.0){ this->patchAccMeanToEstimate(xyz,totalweight); } } else{ const double wmax = 1.0; this->patchExtractAndAcc(xyz,wmax); totalweight += wmax; this->patchAccMeanToEstimate(xyz,totalweight); } } template template inline void BlockWiseNonLocalMeanThreadObject::processSinglePair( const Coordinate & xyz, const Coordinate & nxyz, RealPromoteScalarType & wmax, RealPromoteScalarType & totalweight ){ if(BorderHelper::isInside(nxyz,inImage_)){ //if(ALWAYS_INSIDE || inImage_.isInside(nxyz)){ if(smoothPolicy_.usePixel(meanImage_[nxyz],varImage_[nxyz])){ // here we check if to patches fit to each other // one patch is around xyz // other patch is arround nxyz if(smoothPolicy_.usePixelPair(meanImage_[xyz],varImage_[xyz],meanImage_[nxyz],varImage_[nxyz])){ const RealPromoteScalarType distance =this->patchDistance(xyz,nxyz); const RealPromoteScalarType w = smoothPolicy_.distanceToWeight(meanImage_[xyz],varImage_[xyz],distance); wmax = std::max(w,wmax); this->patchExtractAndAcc(nxyz,w); totalweight+= w; } } } } template template inline typename BlockWiseNonLocalMeanThreadObject::RealPromoteScalarType BlockWiseNonLocalMeanThreadObject::patchDistance( const Coordinate & pA, const Coordinate & pB ){ // TODO : use a acculator like think to make this more beautiful ? const int f = param_.patchRadius_; Coordinate offset(SkipInitialization),nPa(SkipInitialization),nPb(SkipInitialization); int acu = 0; RealPromoteScalarType distancetotal = 0; int c =0 ; //this->mirrorIfIsOutsidePoint(nPa); // this->mirrorIfIsOutsidePoint(nPb); #define VIGRA_NLM_IN_LOOP_CODE \ nPa = pA+offset; \ nPb = pB+offset; \ BorderHelper::mirrorIfIsOutsidePoint(nPa,inImage_); \ BorderHelper::mirrorIfIsOutsidePoint(nPb,inImage_); \ const RealPromoteScalarType gaussWeight = gaussWeight_[c]; \ const RealPromotePixelType vA = inImage_[nPa]; \ const RealPromotePixelType vB = inImage_[nPb]; \ distancetotal += gaussWeight*vigra::sizeDividedSquaredNorm(vA-vB); \ ++acu; if(DIM==2){ for (offset[1] = -f; offset[1] <= f; ++offset[1]) for (offset[0] = -f; offset[0] <= f; ++offset[0],++c){ VIGRA_NLM_IN_LOOP_CODE; } } else if(DIM==3){ for (offset[2] = -f; offset[2] <= f; ++offset[2]) for (offset[1] = -f; offset[1] <= f; ++offset[1]) for (offset[0] = -f; offset[0] <= f; ++offset[0],++c){ VIGRA_NLM_IN_LOOP_CODE; } } else if(DIM==4){ for (offset[3] = -f; offset[3] <= f; ++offset[3]) for (offset[2] = -f; offset[2] <= f; ++offset[2]) for (offset[1] = -f; offset[1] <= f; ++offset[1]) for (offset[0] = -f; offset[0] <= f; ++offset[0],++c){ VIGRA_NLM_IN_LOOP_CODE; } } #undef VIGRA_NLM_IN_LOOP_CODE return distancetotal / acu; } template template inline void BlockWiseNonLocalMeanThreadObject::patchExtractAndAcc( const Coordinate & xyz, const RealPromoteScalarType weight ){ Coordinate xyzPos(SkipInitialization),abc(SkipInitialization); Coordinate nhSize3(param_.patchRadius_); const int ns = 2 * param_.patchRadius_ + 1; int count = 0; // todo: remove abc vector #define VIGRA_NLM_IN_LOOP_CODE \ xyzPos = xyz + abc - nhSize3; \ if(BorderHelper::isOutside(xyzPos,inImage_)) \ average_[count] += inImage_[xyz]* weight; \ else \ average_[count] += inImage_[xyzPos]* weight; \ count++ if(DIM==2){ for (abc[1] = 0; abc[1] < ns; abc[1]++) for (abc[0] = 0; abc[0] < ns; abc[0]++){ VIGRA_NLM_IN_LOOP_CODE; } } else if(DIM==3){ for (abc[2] = 0; abc[2] < ns; abc[2]++) for (abc[1] = 0; abc[1] < ns; abc[1]++) for (abc[0] = 0; abc[0] < ns; abc[0]++){ VIGRA_NLM_IN_LOOP_CODE; } } else if(DIM==4){ for (abc[3] = 0; abc[3] < ns; abc[3]++) for (abc[2] = 0; abc[2] < ns; abc[2]++) for (abc[1] = 0; abc[1] < ns; abc[1]++) for (abc[0] = 0; abc[0] < ns; abc[0]++){ VIGRA_NLM_IN_LOOP_CODE; } } #undef VIGRA_NLM_IN_LOOP_CODE } template template inline void BlockWiseNonLocalMeanThreadObject::patchAccMeanToEstimate( const Coordinate & xyz, const RealPromoteScalarType globalSum ){ Coordinate abc(SkipInitialization),xyzPos(SkipInitialization),nhSize(param_.patchRadius_); int count = 0 ; const int ns = 2 * param_.patchRadius_ + 1; #define VIGRA_NLM_IN_LOOP_CODE \ xyzPos = xyz + abc - nhSize; \ if(BorderHelper::isInside(xyzPos,inImage_)){ \ estimateMutexPtr_->lock(); \ RealPromotePixelType value = estimageImage_[xyzPos]; \ const RealPromoteScalarType gw = gaussWeight_[count]; \ RealPromotePixelType tmp =(average_[count] / globalSum); \ tmp*=gw; \ value +=tmp; \ estimageImage_[xyzPos] = value; \ labelImage_[xyzPos]+=gw; \ estimateMutexPtr_->unlock(); \ } \ count++ if(DIM==2){ for (abc[1] = 0; abc[1] < ns; abc[1]++) for (abc[0] = 0; abc[0] < ns; abc[0]++){ VIGRA_NLM_IN_LOOP_CODE; } } if(DIM==3){ for (abc[2] = 0; abc[2] < ns; abc[2]++) for (abc[1] = 0; abc[1] < ns; abc[1]++) for (abc[0] = 0; abc[0] < ns; abc[0]++){ VIGRA_NLM_IN_LOOP_CODE; } } if(DIM==4){ for (abc[3] = 0; abc[3] < ns; abc[3]++) for (abc[2] = 0; abc[2] < ns; abc[2]++) for (abc[1] = 0; abc[1] < ns; abc[1]++) for (abc[0] = 0; abc[0] < ns; abc[0]++){ VIGRA_NLM_IN_LOOP_CODE; } } #undef VIGRA_NLM_IN_LOOP_CODE } template inline void gaussianMeanAndVariance( const vigra::MultiArrayView & inArray, const double sigma, vigra::MultiArrayView & meanArray, vigra::MultiArrayView & varArray, vigra::MultiArrayView & tmpArray ){ // compute mean and variance vigra::gaussianSmoothMultiArray(inArray, meanArray, sigma); // square raw data (use estimate Image to store temp. results) for(int scanOrderIndex=0;scanOrderIndex inline void gaussianMeanAndVariance( const vigra::MultiArrayView & inArray, const double sigma, vigra::MultiArrayView & meanArray, vigra::MultiArrayView & varArray ){ vigra::MultiArray tmpArray(inArray.shape()); gaussianMeanAndVariance(inArray,sigma,meanArray,varArray,tmpArray); } namespace detail_non_local_means{ template void nonLocalMean1Run( const vigra::MultiArrayView & image, const SMOOTH_POLICY & smoothPolicy, const NonLocalMeanParameter param, vigra::MultiArrayView outImage ){ typedef PIXEL_TYPE_IN PixelTypeIn; typedef typename vigra::NumericTraits::RealPromote RealPromotePixelType; typedef typename vigra::NumericTraits::ValueType RealPromoteScalarType; typedef SMOOTH_POLICY SmoothPolicyType; typedef BlockWiseNonLocalMeanThreadObject ThreadObjectType; // inspect parameter vigra_precondition(param.stepSize_>=1,"NonLocalMean Parameter: \"stepSize>=1\" violated"); vigra_precondition(param.searchRadius_>=1, "NonLocalMean Parameter: \"searchRadius >=1\" violated"); vigra_precondition(param.patchRadius_>=1,"NonLocalMean Parameter: \"searchRadius >=1\" violated"); vigra_precondition(param.stepSize_-1<=param.patchRadius_,"NonLocalMean Parameter: \"stepSize -1 <= patchRadius\" violated"); // allocate arrays vigra::MultiArray meanImage(image.shape()); vigra::MultiArray varImage(image.shape()); vigra::MultiArray estimageImage(image.shape()); vigra::MultiArray labelImage(image.shape()); // compute mean and variance // last argument is a "buffer" since within "gaussianMeanAndVariance" another array is needed // ==> to avoid an unnecessary allocation we use the estimageImage as a buffer //gaussianMeanAndVariance(image,param.sigmaMean_,meanImage,varImage,estimageImage); gaussianMeanAndVariance(image,param.sigmaMean_,meanImage,varImage); // initialize labelImage = RealPromoteScalarType(0.0); estimageImage = RealPromotePixelType(0.0); /////////////////////////////////////////////////////////////// { // MULTI THREAD CODE STARTS HERE typedef threading::thread ThreadType; typedef threading::mutex MutexType; MutexType estimateMutex; //typedef boost::thread ThreadType; const size_t nThreads = param.nThreads_; MultiArray<1,int> progress = MultiArray<1,int>(typename MultiArray<1,int>::difference_type(nThreads)); // allocate all thread objects // each thread object works on a portion of the data std::vector threadObjects(nThreads, ThreadObjectType(image, meanImage, varImage, estimageImage, labelImage, smoothPolicy, param, nThreads, estimateMutex,progress) ); // thread ptr std::vector threadPtrs(nThreads); for(size_t i=0; ijoin(); for(size_t i=0; i void nonLocalMean( const vigra::MultiArrayView & image, const SMOOTH_POLICY & smoothPolicy, const NonLocalMeanParameter param, vigra::MultiArrayView outImage ){ detail_non_local_means::nonLocalMean1Run(image,smoothPolicy,param,outImage); if(param.iterations_>1){ vigra::MultiArray tmp(outImage.shape()); for(size_t i=0;i(param.iterations_-1);++i){ tmp=outImage; detail_non_local_means::nonLocalMean1Run(tmp,smoothPolicy,param,outImage); } } } } // end namespace vigra #endif