9 template <
typename TInput,
typename TOutput>
13 this->SetNthInput(0, const_cast<TInput*>(image));
16 this->SetNthInput(m_NbInputs, const_cast<TInput*>(image));
22 m_imagesVectorIt.push_back(inputIterator);
23 m_imagesVector.push_back(image);
26 template <
typename TInput,
typename TOutput>
29 this->SetNthInput(m_NbInputs, const_cast<TSeedMask*>(mask));
30 m_IndexSourcesMask = m_NbInputs;
34 template <
typename TInput,
typename TOutput>
37 this->SetNthInput(m_NbInputs, const_cast<TSeedMask*>(mask));
38 m_IndexSinksMask = m_NbInputs;
42 template <
typename TInput,
typename TOutput>
45 this->SetNthInput(m_NbInputs, const_cast<TSeedProba*>(mask));
46 m_IndexSourcesProba = m_NbInputs;
50 template <
typename TInput,
typename TOutput>
53 this->SetNthInput(m_NbInputs, const_cast<TSeedProba*>(mask));
54 m_IndexSinksProba = m_NbInputs;
58 template <
typename TInput,
typename TOutput>
62 ( this->itk::ProcessObject::GetInput(m_IndexSourcesMask) );
65 template <
typename TInput,
typename TOutput>
69 ( this->itk::ProcessObject::GetInput(m_IndexSinksMask) );
72 template <
typename TInput,
typename TOutput>
76 ( this->itk::ProcessObject::GetInput(m_IndexSourcesProba) );
79 template <
typename TInput,
typename TOutput>
83 ( this->itk::ProcessObject::GetInput(m_IndexSinksProba) );
86 template <
typename TInput,
typename TOutput>
89 itk::DataObject::Pointer output;
94 output = ( TOutput::New() ).GetPointer();
97 output = ( TOutput::New() ).GetPointer();
100 std::cerr <<
"No output " << idx << std::endl;
104 return output.GetPointer();
107 template <
typename TInput,
typename TOutput>
110 return dynamic_cast< TOutput *
>( this->itk::ProcessObject::GetOutput(0) );
113 template <
typename TInput,
typename TOutput>
116 return dynamic_cast< TOutput *
>( this->itk::ProcessObject::GetOutput(1) );
120 template <
typename TInput,
typename TOutput>
125 std::cout <<
"Computing T-Links..." << std::endl;
127 typename TOutput::Pointer output1 = this->GetOutputSources();
128 output1->SetRegions(this->GetInput(0)->GetLargestPossibleRegion());
129 output1->CopyInformation(this->GetInput(0));
131 output1->FillBuffer(0);
133 typename TOutput::Pointer output2 = this->GetOutputSinks();
134 output2->SetRegions(this->GetInput(0)->GetLargestPossibleRegion());
135 output2->CopyInformation(this->GetInput(0));
137 output2->FillBuffer(0);
139 if(this->m_TLinkMode==
singleGaussianTLink && (this->GetInputSeedSourcesMask().IsNull() && this->GetInputSeedSinksMask().IsNull()))
141 std::cerr <<
"-- Error in T-Links computation: Single Gaussian mode requires sources or sinks mask" << std::endl;
144 if(this->m_TLinkMode==
stremTLink && (this->GetInputSeedSourcesProba().IsNull() && this->GetInputSeedSinksProba().IsNull()))
146 std::cerr <<
"-- Error in T-Links computation: Strem mode requires sources or sinks probabilties" << std::endl;
150 m_NbModalities = m_imagesVectorIt.size();
155 computeSingleGaussian();
167 while (!outIterator1.IsAtEnd())
169 double val1 = m_Alpha * outIterator1.Get();
170 outIterator1.Set(val1);
172 double val2 = m_Alpha * outIterator2.Get();
173 outIterator2.Set(val2);
180 template <
typename TInput,
typename TOutput>
186 std::cout <<
"Use of strem method..." << std::endl;
187 OutRegionIteratorType outIterator1(this->GetOutputSources(), this->GetOutputSources()->GetLargestPossibleRegion());
188 OutRegionIteratorType outIterator2(this->GetOutputSinks(), this->GetOutputSinks()->GetLargestPossibleRegion());
193 while(!sourcesIterator.IsAtEnd())
195 outIterator1.Set(static_cast<OutputPixelType>(sourcesIterator.Get()));
196 outIterator2.Set(static_cast<OutputPixelType>(sinksIterator.Get()));
205 template <
typename TInput,
typename TOutput>
210 if(this->GetInputSeedSourcesMask().IsNotNull())
212 std::cout <<
"Computing single gaussian for sources..." << std::endl;
213 computeSingleGaussianSeeds(this->GetInputSeedSourcesMask(), this->GetOutputSources(), m_MultiVarSources, this->GetInputSeedSinksMask());
215 if(this->GetInputSeedSinksMask().IsNotNull())
217 std::cout <<
"Computing single gaussian for sinks..." << std::endl;
218 computeSingleGaussianSeeds(this->GetInputSeedSinksMask(), this->GetOutputSinks(), m_MultiVarSinks, this->GetInputSeedSourcesMask());
222 template <
typename TInput,
typename TOutput>
230 double epsilon=1e-37;
233 std::vector<double> moy, var;
234 moy.resize(m_NbModalities,0);
235 var.resize(m_NbModalities,0);
237 double nbSeeds = 0.0;
238 std::vector<double> som;
239 som.resize(m_NbModalities,0);
241 std::vector<double> som2;
242 som2.resize(m_NbModalities,0);
244 for (
unsigned int k = 0; k < m_NbModalities; k++ )
246 m_imagesVectorIt[k].GoToBegin();
248 while(!seedIterator.IsAtEnd())
250 if(seedIterator.Get()!=0)
253 for (
unsigned int m = 0; m < m_NbModalities; m++)
255 som [m] += m_imagesVectorIt[m].Get();
256 som2[m] += std::pow(static_cast<double>(m_imagesVectorIt[m].Get()),2.0);
260 for (
unsigned int k = 0; k < m_NbModalities; k++ )
262 ++m_imagesVectorIt[k];
266 for (
unsigned int m = 0; m < m_NbModalities; m++)
268 moy[m] = som[m]/nbSeeds;
269 var[m] = ((som2[m]/nbSeeds) - (moy[m]*moy[m]));
278 std::cout <<
"mean of modality " << m <<
": " << moy[m] << std::endl;
279 std::cout <<
"variance of modality " << m <<
": " << var[m] << std::endl;
280 std::cout << std::endl;
287 std::vector<double> covar;
288 if (m_NbModalities > 1)
291 for (
unsigned int m = 0; m < m_NbModalities-1; m++)
293 for (
unsigned int n = m+1; n < m_NbModalities; n++)
298 covar.resize(nb_paires,0);
299 som.resize(nb_paires,0);
301 seedIterator.GoToBegin();
302 for (
unsigned int i = 0; i < m_NbModalities; i++ )
304 m_imagesVectorIt[i].GoToBegin();
307 while(!seedIterator.IsAtEnd())
309 if(seedIterator.Get()!=0)
312 for (
unsigned int m = 0; m < m_NbModalities-1; m++)
314 for (
unsigned int n = m+1; n < m_NbModalities; n++)
316 som[pos] += (m_imagesVectorIt[m].Get()-moy[m]) * (m_imagesVectorIt[n].Get()-moy[n]);
322 for (
unsigned int k = 0; k < m_NbModalities; k++ )
324 ++m_imagesVectorIt[k];
331 for (
unsigned int m = 0; m < m_NbModalities-1; m++)
333 for (
unsigned int n = m+1; n < m_NbModalities; n++)
335 covar[pos] = som[pos]/(nbSeeds-1);
345 covarMatrix.SetSize(m_NbModalities,m_NbModalities);
346 for (
unsigned int m = 0; m < m_NbModalities; m++)
348 covarMatrix(m,m) = var[m];
351 for (
unsigned int m = 0; m < m_NbModalities-1; m++)
353 for (
unsigned int n = m+1; n < m_NbModalities; n++)
355 covarMatrix(m,n) = covarMatrix(n,m) = covar[pos];
361 covarMatrix=covarMatrix.GetInverse();
366 d.SetSize(1,m_NbModalities);
368 seedIterator.GoToBegin();
369 for (
unsigned int i = 0; i < m_NbModalities; i++ )
371 m_imagesVectorIt[i].GoToBegin();
374 TSeedMask::Pointer seedMaskOpp2 = TSeedMask::New();
375 seedMaskOpp2->SetRegions(seedMask->GetLargestPossibleRegion());
376 seedMaskOpp2->CopyInformation(seedMask);
377 seedMaskOpp2->Allocate();
380 if(seedMaskOpp.IsNull())
382 seedMaskOpp2->FillBuffer(0);
387 while(!seedOppIterator.IsAtEnd())
389 seedOppIterator2.Set(seedOppIterator.Get());
395 seedIterator.GoToBegin();
396 seedOppIterator2.GoToBegin();
397 while(!seedIterator.IsAtEnd())
399 for (
unsigned int m = 0; m < m_NbModalities; m++)
401 d(0,m) = m_imagesVectorIt[m].Get() - moy[m];
404 prob = d.GetVnlMatrix()*covarMatrix.GetVnlMatrix()*d.GetTranspose();
406 outIterator.Set( std::exp(-0.5*prob(0,0)) );
408 if(seedIterator.Get()!=0)
410 outIterator.Set(1.0);
412 if(seedOppIterator2.Get()!=0)
414 outIterator.Set(0.0);
419 for (
unsigned int k = 0; k < m_NbModalities; k++ )
421 ++m_imagesVectorIt[k];
void SetInputSeedSourcesMask(const TSeedMask *mask)
TSeedMask::ConstPointer GetInputSeedSourcesMask()
TOutput * GetOutputSinks()
void SetInputSeedSinksMask(const TSeedMask *mask)
TOutput * GetOutputSources()
void computeSingleGaussian()
TSeedProba::ConstPointer GetInputSeedSinksProba()
itk::Image< PixelTypeD, 3 > TSeedProba
itk::ImageRegionConstIterator< TSeedProba > SeedProbaRegionConstIteratorType
itk::DataObject::Pointer MakeOutput(unsigned int idx)
void SetInputSeedSinksProba(const TSeedProba *mask)
TSeedMask::ConstPointer GetInputSeedSinksMask()
itk::ImageRegionConstIterator< TSeedMask > SeedMaskRegionConstIteratorType
void SetInputImage(unsigned int i, const TInput *image)
itk::ImageRegionIterator< TOutput > OutRegionIteratorType
itk::ImageRegionConstIterator< TInput > InConstIteratorType
itk::Matrix< double, 1, 1 > MatrixTypeRes
void GenerateData() ITK_OVERRIDE
itk::Image< PixelTypeUC, 3 > TSeedMask
itk::VariableSizeMatrix< NumericType > DoubleVariableSizeMatrixType
itk::ImageRegionIterator< TSeedMask > SeedMaskRegionIteratorType
TOutput::Pointer OutputImagePointer
void SetInputSeedSourcesProba(const TSeedProba *mask)
TSeedProba::ConstPointer GetInputSeedSourcesProba()
void computeSingleGaussianSeeds(TSeedMask::ConstPointer seedMask, OutputImagePointer output, double multiVar, TSeedMask::ConstPointer seedMaskOpp=ITK_NULLPTR)