ANIMA  4.0
animaGraphCutFilter.hxx
Go to the documentation of this file.
1 #pragma once
2 
3 #include "animaGraphCutFilter.h"
4 
5 namespace anima
6 {
7 
8 template <typename TInput, typename TOutput>
9 void GraphCutFilter<TInput, TOutput>::SetInputImage(unsigned int i, const TInput* image)
10 {
11  if (i == 0)
12  this->SetNthInput(0, const_cast<TInput*>(image));
13  else
14  {
15  this->SetNthInput(m_NbInputs, const_cast<TInput*>(image));
16  m_NbInputs++;
17  }
18 
19  m_TLinksFilter->SetInputImage(i, image);
20  m_Graph3DFilter->SetInputImage(i, image);
21 }
22 
23 template <typename TInput, typename TOutput>
25 {
26  this->SetNthInput(1, const_cast<TMask*>(mask));
27  m_Graph3DFilter->SetMask( mask );
28 }
29 
30 template <typename TInput, typename TOutput>
32 {
33  this->SetNthInput(m_NbInputs, const_cast<TMask*>(mask));
34  m_IndexSourcesMask = m_NbInputs;
35  m_NbInputs++;
36  m_TLinksFilter->SetInputSeedSourcesMask( mask );
37 }
38 
39 template <typename TInput, typename TOutput>
41 {
42  this->SetNthInput(m_NbInputs, const_cast<TMask*>(mask));
43  m_IndexSinksMask = m_NbInputs;
44  m_NbInputs++;
45  m_TLinksFilter->SetInputSeedSinksMask( mask );
46 }
47 
48 
49 template <typename TInput, typename TOutput>
51 {
52  this->SetNthInput(m_NbInputs, const_cast<TSeedProba*>(mask));
53  m_IndexSourcesProba = m_NbInputs;
54  m_NbInputs++;
55  m_TLinksFilter->SetInputSeedSourcesProba( mask );
56 }
57 
58 template <typename TInput, typename TOutput>
60 {
61  this->SetNthInput(m_NbInputs, const_cast<TSeedProba*>(mask));
62  m_IndexSinksProba = m_NbInputs;
63  m_NbInputs++;
64  m_TLinksFilter->SetInputSeedSinksProba( mask );
65 }
66 
67 template <typename TInput, typename TOutput>
68 itk::Image <unsigned char,3>::ConstPointer GraphCutFilter<TInput, TOutput>::GetMask()
69 {
70  return static_cast< const TMask * >
71  ( this->itk::ProcessObject::GetInput(1) );
72 }
73 
74 template <typename TInput, typename TOutput>
75 itk::Image <unsigned char,3>::ConstPointer GraphCutFilter<TInput, TOutput>::GetInputSeedSourcesMask()
76 {
77  return static_cast< const TMask * >
78  ( this->itk::ProcessObject::GetInput(m_IndexSourcesMask) );
79 }
80 
81 template <typename TInput, typename TOutput>
82 itk::Image <unsigned char,3>::ConstPointer GraphCutFilter<TInput, TOutput>::GetInputSeedSinksMask()
83 {
84  return static_cast< const TMask * >
85  ( this->itk::ProcessObject::GetInput(m_IndexSinksMask) );
86 }
87 
88 template <typename TInput, typename TOutput>
89 itk::Image <double,3>::ConstPointer GraphCutFilter<TInput, TOutput>::GetInputSeedSourcesProba()
90 {
91  return static_cast< const TSeedProba * >
92  ( this->itk::ProcessObject::GetInput(m_IndexSourcesProba) );
93 }
94 
95 template <typename TInput, typename TOutput>
96 itk::Image <double,3>::ConstPointer GraphCutFilter<TInput, TOutput>::GetInputSeedSinksProba()
97 {
98  return static_cast< const TSeedProba * >
99  ( this->itk::ProcessObject::GetInput(m_IndexSinksProba) );
100 }
101 
102 template <typename TInputImage, typename TOutput>
103 itk::DataObject::Pointer GraphCutFilter <TInputImage, TOutput>::MakeOutput(unsigned int idx)
104 {
105  itk::DataObject::Pointer output;
106 
107  switch ( idx )
108  {
109  case 0:
110  output = ( TOutput::New() ).GetPointer();
111  break;
112  case 1:
113  output = ( TOutput::New() ).GetPointer();
114  break;
115  default:
116  std::cerr << "No output " << idx << std::endl;
117  output = NULL;
118  break;
119  }
120  return output.GetPointer();
121 }
122 
123 template <typename TInputImage, typename TOutput>
125 {
126  return dynamic_cast< TOutput* >( this->itk::ProcessObject::GetOutput(0) );
127 }
128 template <typename TInputImage, typename TOutput>
130 {
131  return dynamic_cast< TOutput* >( this->itk::ProcessObject::GetOutput(1) );
132 }
133 
134 template <typename TInput, typename TOutput>
135 void
138 {
139  if( m_OutputFilename != "" )
140  {
141  std::cout << "Writing graph cut output image to: " << m_OutputFilename << std::endl;
142  anima::writeImage<TOutput>(m_OutputFilename, this->GetOutput());
143  }
144  if( m_OutputBackgroundFilename != "" )
145  {
146  std::cout << "Writing graph cut output image to: " << m_OutputBackgroundFilename << std::endl;
147  anima::writeImage<TOutput>(m_OutputBackgroundFilename, this->GetOutputBackground());
148  }
149 }
150 
151 template <typename TInput, typename TOutput>
152 void
155 {
156  std::cout << "Computing graph cut..." << std::endl;
157 
158  m_TLinksFilter->SetAlpha( this->GetAlpha() );
159  m_TLinksFilter->SetMultiVarSources( this->GetMultiVarSources() );
160  m_TLinksFilter->SetMultiVarSinks( this->GetMultiVarSinks() );
161  m_TLinksFilter->SetTLinkMode( this->GetTLinkMode() );
162  m_TLinksFilter->SetVerbose( this->GetVerbose() );
163  m_TLinksFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
164  m_TLinksFilter->SetTol( m_Tol );
165 
166  m_Graph3DFilter->SetSigma( this->GetSigma() );
167  m_Graph3DFilter->SetUseSpectralGradient( this->GetUseSpectralGradient() );
168  m_Graph3DFilter->SetMatrix( this->GetMatrix() );
169  m_Graph3DFilter->SetMatFilename( this->GetMatrixGradFilename() );
170  m_Graph3DFilter->SetVerbose( this->GetVerbose() );
171  m_Graph3DFilter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
172  m_Graph3DFilter->SetTol( m_Tol );
173 
174  m_Graph3DFilter->GraftNthOutput( 0, this->GetOutput() );
175  m_Graph3DFilter->GraftNthOutput( 1, this->GetOutputBackground() );
176 
177  try
178  {
179  m_Graph3DFilter->Update();
180  }
181  catch (itk::ExceptionObject &e)
182  {
183  std::cerr << e << std::endl;
184  exit(-1);
185  }
186 
187  this->GraftNthOutput( 0 , m_Graph3DFilter->GetOutput() );
188  this->GraftNthOutput( 1 , m_Graph3DFilter->GetOutputBackground() );
189 }
190 
191 
192 } //end of namespace anima
void SetInputImage(unsigned int i, const TInput *image)
itk::Image< PixelTypeD, 3 > TSeedProba
TMask::ConstPointer GetInputSeedSourcesMask()
void GenerateData() ITK_OVERRIDE
TMask::ConstPointer GetInputSeedSinksMask()
void SetInputSeedSinksProba(const TSeedProba *mask)
void SetMask(const TMask *mask)
itk::Image< PixelTypeUC, 3 > TMask
void SetInputSeedSourcesProba(const TSeedProba *mask)
void SetInputSeedSourcesMask(const TMask *mask)
TMask::ConstPointer GetMask()
void SetInputSeedSinksMask(const TMask *mask)
Class performing grah cut segmentation. First the sources and sinks probabilities maps are computed u...
TSeedProba::ConstPointer GetInputSeedSinksProba()
TSeedProba::ConstPointer GetInputSeedSourcesProba()