6 template <
typename TInputImage,
typename TMaskImage>
9 this->SetNthInput(0, const_cast<TMaskImage*>(mask));
12 template <
typename TInputImage,
typename TMaskImage>
15 this->SetNthInput(m_NbInputs, const_cast<TInputImage*>(image));
16 m_IndexImage1 = m_NbInputs;
20 template <
typename TInputImage,
typename TMaskImage>
23 this->SetNthInput(m_NbInputs, const_cast<TInputImage*>(image));
24 m_IndexImage2 = m_NbInputs;
28 template <
typename TInputImage,
typename TMaskImage>
31 this->SetNthInput(m_NbInputs, const_cast<TInputImage*>(image));
32 m_IndexImage3 = m_NbInputs;
35 template <
typename TInputImage,
typename TMaskImage>
38 this->SetNthInput(m_NbInputs, const_cast<TInputImage*>(image));
39 m_IndexImage4 = m_NbInputs;
43 template <
typename TInputImage,
typename TMaskImage>
46 this->SetNthInput(m_NbInputs, const_cast<TInputImage*>(image));
47 m_IndexImage5 = m_NbInputs;
52 template <
typename TInputImage,
typename TMaskImage>
55 return static_cast< const TMaskImage *
> 56 ( this->itk::ProcessObject::GetInput(0) );
59 template <
typename TInputImage,
typename TMaskImage>
62 return static_cast< const TInputImage *
> 63 ( this->itk::ProcessObject::GetInput(m_IndexImage1) );
66 template <
typename TInputImage,
typename TMaskImage>
69 return static_cast< const TInputImage *
> 70 ( this->itk::ProcessObject::GetInput(m_IndexImage2) );
73 template <
typename TInputImage,
typename TMaskImage>
76 return static_cast< const TInputImage *
> 77 ( this->itk::ProcessObject::GetInput(m_IndexImage3) );
80 template <
typename TInputImage,
typename TMaskImage>
83 return static_cast< const TInputImage *
> 84 ( this->itk::ProcessObject::GetInput(m_IndexImage4) );
87 template <
typename TInputImage,
typename TMaskImage>
90 return static_cast< const TInputImage *
> 91 ( this->itk::ProcessObject::GetInput(m_IndexImage5) );
94 template <
typename TInputImage,
typename TMaskImage>
99 std::vector<double> min1D( 1, 255.0 );
100 std::vector<double> max1D( 1, 0.0 );
104 initiaRandom->SetMinValues( min1D );
105 initiaRandom->SetMaxValues( max1D );
106 initiaRandom->SetDimensionGaussian(1);
107 initiaRandom->SetNbGaussian(3);
110 estimator ->SetMaxIterations( 100 );
111 estimator ->SetModelMinDistance( 1e-3 );
112 estimator ->SetMaxIterationsConc( 10 );
113 estimator ->SetStremMode(
false );
114 estimator ->SetRejectionRatio( m_Robust );
115 estimator ->SetMask( this->GetMask() );
116 estimator ->SetInputImage1( this->GetInputImage1() );
117 estimator ->SetVerbose(
false );
119 std::vector<unsigned int> emSteps( 1, 60 );
120 std::vector<unsigned int> iterSteps( 1, 60 );
123 strategy->SetEstimator( estimator );
124 strategy->SetInitializer( initiaRandom );
125 strategy->SetStrategy( emSteps, iterSteps );
126 itk::CStyleCommand::Pointer callback = itk::CStyleCommand::New();
128 strategy ->AddObserver(itk::ProgressEvent(), callback );
130 std::cout << std::endl;
135 if ( !strategy->GetSolutionMap( solutionClassifier, solutionClassifierAlpha ) )
137 std::cerr <<
"-- Error: No solution found in first modality" << std::endl;
141 m_Solution1D = solutionClassifier.begin() ->second;
142 m_Solution1DAlphas = solutionClassifierAlpha.begin() ->second;
145 template <
typename TInputImage,
typename TMaskImage>
151 classifier->SetInputImage1(this->GetInputImage1());
152 classifier->SetMask(this->GetMask());
153 classifier->SetGaussianModel(m_Solution1D);
154 classifier->SetAlphas(m_Solution1DAlphas);
155 classifier->SetTol(m_Tol);
156 classifier->Update();
159 std::vector<InputIteratorType> classesIt;
160 for(
int i = 0; i < 3; i++)
163 im->SetRegions(this->GetMask()->GetLargestPossibleRegion());
164 im->CopyInformation(this->GetMask());
167 m_ImagesClasses.push_back(im);
170 classesIt.push_back(imIt);
173 InputConstIteratorType classifIt(classifier->GetOutput(), classifier->GetOutput()->GetLargestPossibleRegion() );
174 while(!classifIt.IsAtEnd())
176 if(!(classifIt.Get() == 0 || classifIt.Get() > 3 ))
178 classesIt[ classifIt.Get() - 1 ].Set(1);
181 for(
unsigned int i = 0; i < classesIt.size(); i++)
188 template <
typename TInputImage,
typename TMaskImage>
192 const double resolution = 1;
193 const double bandwidth = 5;
199 std::vector< std::vector<int> > selectionRules( 3, std::vector<int>( 3, 0 ) );
200 selectionRules[ 1 ][ 0 ] = 1;
202 selectionRules[ 2 ][ 0 ] = -1;
204 selectionRules[ 2 ][ 0 ] = 1;
206 m_SelectedMax.resize( 3, std::vector<double>( 3, 0.0 ) );
207 m_Stds.resize(3, std::vector<double>( 3, 0.0 ));
209 typedef double MeasurementType;
210 const unsigned int MeasurementVectorLength = 1;
212 typedef itk::Statistics::ListSample< MeasurementVectorType > ListSampleType;
213 typedef itk::Statistics::DenseFrequencyContainer2 FrequencyContainerType;
214 typedef itk::Statistics::Histogram< MeasurementType,FrequencyContainerType > HistogramType;
215 typedef itk::Statistics::SampleToHistogramFilter<ListSampleType,HistogramType> FilterType;
221 for (
unsigned int im = 1; im < m_NumberOfModalities; im++ )
223 for (
unsigned int c = 0; c < m_ImagesClasses.size(); c++ )
226 typename MaskFilterType::Pointer maskFilter = MaskFilterType::New();
227 typename MinMaxCalculatorType::Pointer calculator = MinMaxCalculatorType::New();
228 maskFilter->SetDirectionTolerance(m_Tol);
229 maskFilter->SetCoordinateTolerance(m_Tol);
230 maskFilter->SetInput( m_ImagesVector[ im ] );
231 maskFilter->SetMaskImage( m_ImagesClasses[ c ] );
233 maskFilter->SetOutsideValue( valMaxInput );
234 maskFilter->Update();
235 calculator->SetImage( maskFilter->GetOutput() );
236 calculator->ComputeMinimum();
239 maskFilter->SetOutsideValue( valMinInput );
240 maskFilter->Update();
241 calculator->SetImage( maskFilter->GetOutput() );
242 calculator->ComputeMaximum();
245 double minV =
static_cast<double>(minimumResult);
246 double maxV =
static_cast<double>(maximumResult);
248 unsigned int bins =
static_cast<unsigned int>(std::floor( maxV - minV + 1.0 ));
250 ListSampleType::Pointer listSample = ListSampleType::New();
251 listSample->SetMeasurementVectorSize( MeasurementVectorLength );
254 InputIteratorType classesCIt (m_ImagesClasses[ c ], m_ImagesClasses[ c ]->GetLargestPossibleRegion() );
256 MeasurementVectorType mv;
257 while (!classesCIt.IsAtEnd())
259 if(classesCIt.Get()!=0)
261 mv[0] = static_cast <MeasurementType> (tmpIt.Get());
262 listSample->PushBack(mv);
269 const unsigned int numberOfComponents = 1;
270 HistogramType::SizeType size( numberOfComponents );
272 HistogramType::MeasurementVectorType min( numberOfComponents );
273 HistogramType::MeasurementVectorType max( numberOfComponents );
277 FilterType::Pointer filter = FilterType::New();
278 filter->SetInput( listSample );
279 filter->SetHistogramSize( size );
280 filter->SetHistogramBinMinimum( min );
281 filter->SetHistogramBinMaximum( max );
284 HistogramType::ConstPointer histogram = filter->GetOutput();
286 double bins2 =
static_cast<double>(bins) / resolution;
287 unsigned int smoothedBins =
static_cast<unsigned int>(bins2 + 1);
289 HistogramType::SizeType size2(MeasurementVectorLength);
290 size2.Fill(smoothedBins);
292 HistogramType::MeasurementVectorType lowerBound;
293 lowerBound.SetSize(smoothedBins);
294 lowerBound.Fill(minV);
296 HistogramType::MeasurementVectorType upperBound;
297 upperBound.SetSize(smoothedBins);
298 upperBound.Fill(maxV);
300 HistogramType::Pointer smoothed = HistogramType::New();
301 smoothed->SetMeasurementVectorSize(MeasurementVectorLength);
302 smoothed->Initialize(size2, lowerBound, upperBound );
303 smoothed->SetFrequency(0);
305 std::vector<double> smoothedFrequency(smoothedBins,0);
307 for (
unsigned int i = 0; i < bins; ++i)
309 for (
unsigned int j = 0; j < smoothedBins; ++j)
312 double valueI =
static_cast<double>(i) / static_cast<double>(bins -1) * (maxV - minV) + minV;
313 double valueJ =
static_cast<double>(j) / static_cast<double>(smoothedBins -1) * (maxV - minV) + minV;
314 double kvalue = (valueI - valueJ) / bandwidth;
315 double norm = std::exp( -0.5 * kvalue * kvalue ) / std::sqrt( 2.0 * M_PI );
316 double value = norm *
static_cast<double>(histogram->GetFrequency(i));
317 smoothedFrequency[j] += value;
321 for (
unsigned int i = 0; i < smoothedBins; i++ )
323 smoothedFrequency[i]/=(
static_cast<double>( histogram->GetTotalFrequency()) * bandwidth);
327 std::vector<unsigned int> maxs;
328 const int localMax = 3;
329 for (
unsigned int i = 0; i < smoothedBins; i++ )
331 int testVal = static_cast <
int> (i) - localMax;
332 unsigned int jmin = testVal < 0 ? 0 : static_cast <unsigned int> (testVal);
334 unsigned int jmax = ( i + localMax ) > smoothedBins ? smoothedBins : ( i + localMax );
337 for (
unsigned int j = jmin; j < jmax; j++ )
339 if ( (i != j) && (smoothedFrequency[i] <= smoothedFrequency[j]) )
346 if ( ismax && ( smoothedFrequency[i] > 0.001 ) )
354 std::cerr <<
"--Error in Hierarchical Initializer: no maximum value" << std::endl;
361 switch ( selectionRules[ im ][ c ] )
365 value = (
static_cast<double>(maxs[0]) / static_cast<double>(bins-1) ) * (maxV - minV) + minV;
366 m_SelectedMax[im][c] = value;
371 value = (
static_cast<double>(maxs[ maxs.size() - 1 ]) / static_cast<double>(bins-1) ) * (maxV - minV) + minV;
372 m_SelectedMax[ im ][ c ] = value;
378 unsigned int absMaxIndex = 0;
379 unsigned int absMaxValue = 0;
380 for (
unsigned int i = 0; i < maxs.size(); i++ )
382 if ( maxs[ i ] > absMaxValue )
384 absMaxValue = maxs[ i ];
388 value = (
static_cast<double>(maxs[ absMaxIndex ]) / static_cast<double>(bins-1) ) * (maxV - minV) + minV;
389 m_SelectedMax[ im ][ c ] = value;
398 template <
typename TInputImage,
typename TMaskImage>
402 ImageTypeD::Pointer diff = ImageTypeD::New();
403 diff->SetRegions(this->GetMask()->GetLargestPossibleRegion());
404 diff->CopyInformation(this->GetMask());
409 std::vector<InputIteratorType> classesItVec;
410 for(
unsigned int i = 0; i < m_ImagesClasses.size(); i++)
412 InputIteratorType classesIt (m_ImagesClasses[i],m_ImagesClasses[i]->GetLargestPossibleRegion() );
413 classesItVec.push_back(classesIt);
416 std::vector<InputConstIteratorType> imagesVectorItVec;
417 for(
unsigned int i = 0; i < m_NumberOfModalities; i++)
419 InputConstIteratorType imagesVectorIt (m_ImagesVector[i], m_ImagesVector[i]->GetLargestPossibleRegion() );
420 imagesVectorItVec.push_back(imagesVectorIt);
423 const double estimatorFactor = 1.4918;
425 for (
unsigned int im = 1; im < m_NumberOfModalities; im++ )
427 for (
unsigned int c = 0; c < m_ImagesClasses.size(); c++ )
430 for(
unsigned int i = 0; i < m_ImagesClasses.size(); i++)
432 classesItVec[i].GoToBegin();
434 for(
unsigned int i = 0; i < m_NumberOfModalities; i++)
436 imagesVectorItVec[i].GoToBegin();
440 while (!diffIt.IsAtEnd())
443 if(classesItVec[c].Get() > 0)
445 diffIt.Set(std::fabs(imagesVectorItVec[im].Get()-m_SelectedMax[im][c]));
448 for(
unsigned int i = 0; i < m_ImagesClasses.size(); i++)
452 for(
unsigned int i = 0; i < m_NumberOfModalities; i++)
454 ++imagesVectorItVec[i];
458 m_Stds[im][c] = estimatorFactor * regionMedianValue(diff,m_ImagesClasses[c]);
463 template <
typename TInputImage,
typename TMaskImage>
467 m_GaussianModel.clear();
470 for (
unsigned int i = 0; i < m_NbClasses; i++ )
472 GaussianFunctionType::Pointer densityFunction1 = GaussianFunctionType::New();
473 densityFunction1->SetMeasurementVectorSize( m_NbClasses );
474 GaussianFunctionType::MeanVectorType mean1( m_NbClasses );
475 GaussianFunctionType::CovarianceMatrixType cov1;
476 cov1.SetSize( m_NbClasses, m_NbClasses );
480 GaussianFunctionType::MeanVectorType t1mean = (m_Solution1D[i])->GetMean();
481 GaussianFunctionType::CovarianceMatrixType t1var = (m_Solution1D[i])->GetCovariance();
483 mean1[0] = t1mean[0];
484 mean1[1] = m_SelectedMax[1][i];
485 mean1[2] = m_SelectedMax[2][i];
487 cov1[0][0] = t1var[0][0];
488 cov1[1][1] = m_Stds[1][i]*m_Stds[1][i];
489 cov1[2][2] = m_Stds[2][i]*m_Stds[2][i];
491 densityFunction1->SetMean( mean1 );
492 densityFunction1->SetCovariance( cov1 );
494 m_GaussianModel.push_back(densityFunction1);
495 m_Alphas.push_back(m_Solution1DAlphas[i]);
499 template <
typename TInputImage,
typename TMaskImage>
506 std::vector<double> regionValueVector;
508 while(!imageIt.IsAtEnd())
510 if ( maskIt.Get() != 0 )
512 regionValueVector.push_back(imageIt.Get());
517 std::sort (regionValueVector.begin(), regionValueVector.end());
518 return regionValueVector[regionValueVector.size()/2];
521 template <
typename TInputImage,
typename TMaskImage>
524 m_ImagesVector.clear();
526 if(m_IndexImage1<m_NbMaxImages){m_ImagesVector.push_back(this->GetInputImage1());}
527 if(m_IndexImage2<m_NbMaxImages){m_ImagesVector.push_back(this->GetInputImage2());}
528 if(m_IndexImage3<m_NbMaxImages){m_ImagesVector.push_back(this->GetInputImage3());}
529 if(m_IndexImage4<m_NbMaxImages){m_ImagesVector.push_back(this->GetInputImage4());}
530 if(m_IndexImage5<m_NbMaxImages){m_ImagesVector.push_back(this->GetInputImage5());}
532 m_NumberOfModalities = m_ImagesVector.size();
534 this->ComputeSolution1D();
535 this->ComputeInitialT1Classification();
536 this->ComputeHistogram();
537 this->ComputeVariances();
538 this->FillInitialGaussianModel();
TInputImage::ConstPointer GetInputImage4()
TMaskImage::ConstPointer GetMask()
std::map< double, std::vector< double > > ModelMap2
void Update() ITK_OVERRIDE
void SetInputImage5(const TInputImage *image)
InputImageType::Pointer InputImagePointer
TInputImage::ConstPointer GetInputImage2()
itk::ImageRegionIterator< ImageTypeD > ImageIteratorTypeD
TInputImage::ConstPointer GetInputImage5()
void ComputeInitialT1Classification()
void SetMask(const TMaskImage *mask)
void SetInputImage2(const TInputImage *image)
TInputImage::ConstPointer GetInputImage3()
void SetInputImage1(const TInputImage *image)
itk::ImageRegionIterator< InputImageType > InputIteratorType
double regionMedianValue(itk::Image< double, 3 >::Pointer image, typename TInputImage::Pointer mask)
void FillInitialGaussianModel()
InputImageType::PixelType InputImagePixelType
TInputImage::ConstPointer GetInputImage1()
void eventCallback(itk::Object *caller, const itk::EventObject &event, void *clientData)
itk::VariableLengthVector< double > MeasurementVectorType
void SetInputImage4(const TInputImage *image)
void SetInputImage3(const TInputImage *image)
itk::SmartPointer< Self > Pointer
itk::SmartPointer< Self > Pointer
itk::ImageRegionConstIterator< InputImageType > InputConstIteratorType
itk::SmartPointer< Self > Pointer
itk::SmartPointer< Self > Pointer
std::map< double, std::vector< GaussianFunctionType::Pointer > > ModelMap