Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "cmtkTypedArrayNoiseEstimatorMaximumLikelihood.h"
00032
00033 namespace
00034 cmtk
00035 {
00036
00037
00038
00039 TypedArrayNoiseEstimatorMaximumLikelihood::TypedArrayNoiseEstimatorMaximumLikelihood
00040 ( const TypedArray* data, const size_t histogramBins )
00041 {
00042
00043 UNUSED(histogramBins);
00044
00045 size_t overriddenHistogramBins = 255;
00046 Histogram<unsigned int>::SmartPtr histogram( data->GetHistogram( overriddenHistogramBins ) );
00047
00048
00049 size_t idx = 0;
00050 while ( (idx < (overriddenHistogramBins-1)) && ( (*histogram)[idx] <= (*histogram)[idx+1] ) )
00051 {
00052 ++idx;
00053 }
00054
00055 while ( (idx < (overriddenHistogramBins-1)) && ( (*histogram)[idx] > (*histogram)[idx+1] ) )
00056 {
00057 ++idx;
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067 int numBinsToUse = idx;
00068
00069 float sigmaUpperBound = 100;
00070
00071
00072 double stepSize = 0.1;
00073
00074
00075
00076 double sigmaThresh = 0.1;
00077
00078
00079 double maxLikelySigma = 23.0;
00080 double prevMaxLikelySigma = 0.0;
00081
00082 while ( sqrt( pow( maxLikelySigma - prevMaxLikelySigma, 2 ) ) > sigmaThresh )
00083 {
00084
00085 std::cout << "K: " << numBinsToUse << "\tprevMaxLikelySigma: " << prevMaxLikelySigma << "\tmaxLikelySigma: " << maxLikelySigma << std::endl;
00086 prevMaxLikelySigma = maxLikelySigma;
00087
00088 double curSigmaMinimizer;
00089 double minSigmaMinimizer = std::numeric_limits<double>::max();
00090
00091 const double bin0Squared = pow( histogram->BinToValue( 0 ), 2 );
00092 const double binKSquared = pow( histogram->BinToValue(numBinsToUse-1), 2 );
00093
00094
00095
00096
00097
00098 for ( double sigma = 1; sigma < sigmaUpperBound; sigma += stepSize )
00099 {
00100 const double twoSigmaSquared = 2 * pow( sigma, 2 );
00101 double sumForSearch = 0.0;
00102
00103
00104 for ( int j = 1; j <= numBinsToUse; j++ )
00105 {
00106 const double curAddend = (*histogram)[j]
00107 * ( exp( -1 * ( pow( (double)(histogram->BinToValue(j-1)), 2 ) / twoSigmaSquared ) )
00108 - exp( -1 * ( pow( (double)(histogram->BinToValue( j )), 2 ) / twoSigmaSquared ) ) );
00109 sumForSearch += curAddend;
00110 }
00111
00112 curSigmaMinimizer =
00113 ( exp( -bin0Squared / twoSigmaSquared ) - exp( -binKSquared / twoSigmaSquared ) ) - sumForSearch;
00114
00115 if ( curSigmaMinimizer < minSigmaMinimizer )
00116 {
00117 minSigmaMinimizer = curSigmaMinimizer;
00118 maxLikelySigma = sigma;
00119 }
00120 }
00121 numBinsToUse = static_cast<int>( Superclass::EstimateNumBinsToUse( data, histogram, maxLikelySigma ) );
00122 }
00123
00124 std::cout << maxLikelySigma << std::endl;
00125
00126
00127 this->m_NoiseLevelSigma = static_cast<Types::DataItem>( 0 );
00128 }
00129
00130 }