ANIMA  4.0
animaGraphCut.cxx
Go to the documentation of this file.
1 #include <tclap/CmdLine.h>
2 
3 #include <itkTimeProbe.h>
5 #include "animaGraphCutFilter.h"
6 
7 int main(int argc, const char** argv)
8 {
9 
10  // Parsing arguments
11  TCLAP::CmdLine cmd("INRIA / IRISA - VisAGeS/Empenn Team", ' ',ANIMA_VERSION);
12 
13  TCLAP::MultiArg<std::string> inputArg("i","input","Input images to segment (up to 5, e.g. T1, T2, DP, FLAIR, T1-Gd)",
14  true,"input images",cmd);
15 
16  TCLAP::ValueArg<std::string> maskFileArg("m","mask","Brain mask",false,"","brain mask",cmd);
17  TCLAP::ValueArg<std::string> sourcesMaskFileArg("","sof","Sources input image",true,"","Sources input image",cmd);
18  TCLAP::ValueArg<std::string> sinksMaskFileArg("","sif","Sinks input image",true,"","Sinks input image",cmd);
19  TCLAP::ValueArg<std::string> sourcesProbaFileArg("","sopf","Probabilities sources input image",false,"","Probabilities sources input image",cmd);
20  TCLAP::ValueArg<std::string> sinksProbaFileArg("","sipf","Probabilities sinks input image",false,"","Probabilities sinks input image",cmd);
21  TCLAP::SwitchArg verboseArg("V","verbose","verbose mode (default: false)",cmd,false);
22 
23  TCLAP::SwitchArg notUseSpecGradArg("U","no-usg","Do not use spectral gradient (default: false)",cmd,false);
24  TCLAP::ValueArg<int> TLinkModeArg("","mode","Graph cut computation mode (0: single Gaussian, 1: STREM mode, default: 0)",false,0,"graph cut computation mode",cmd);
25  TCLAP::ValueArg<double> multiVarSourcesArg("","mv","Coefficient to multiply the variance value of the sources seed (default: 1)",false,1,"sources multiply variance",cmd);
26  TCLAP::ValueArg<double> multiVarSinksArg("","ms","Coefficient to multiply the variance value of the seed (default: 1)",false,1,"sinks multiply variance",cmd);
27  TCLAP::ValueArg<double> sigmaArg("s","sigma","sigma value (default: 0.6)",false,0.6,"sigma",cmd);
28  TCLAP::ValueArg<double> alphaArg("a","alpha","Alpha value (default: 10)",false,10,"alpha",cmd);
29  TCLAP::ValueArg<std::string> matrixGradArg("","mat","Spectral gradient matrix file",false,"","spectral gradient matrix file",cmd);
30 
31  TCLAP::ValueArg<std::string> outputGCFileArg("o","out","Output segmentation",true,"","output segmentation",cmd);
32 
33  try
34  {
35  cmd.parse(argc,argv);
36  }
37  catch (TCLAP::ArgException& e)
38  {
39  std::cerr << "Error: " << e.error() << "for argument " << e.argId() << std::endl;
40  return(1);
41  }
42 
43  const unsigned int Dimension = 3;
44  typedef itk::Image <double,Dimension> InputImageTypeD;
45  typedef itk::Image <unsigned char,Dimension> InputImageTypeUC;
46 
47  // Create instance of graph cut filter
49  FilterTypeGraphCut::Pointer GraphCutFilter = FilterTypeGraphCut::New();
50 
51  for (unsigned int i = 0;i < inputArg.getValue().size();++i)
52  GraphCutFilter->SetInputImage(i,anima::readImage<InputImageTypeD>(inputArg.getValue()[i]));
53 
54  if( sourcesProbaFileArg.getValue()!="" )
55  {
56  GraphCutFilter->SetInputSeedSourcesProba( anima::readImage<InputImageTypeD>(sourcesProbaFileArg.getValue()) );
57  }
58 
59  if( sinksProbaFileArg.getValue()!="" )
60  {
61  GraphCutFilter->SetInputSeedSinksProba( anima::readImage<InputImageTypeD>(sinksProbaFileArg.getValue()) );
62  }
63 
64  if( maskFileArg.getValue()!="" )
65  GraphCutFilter->SetMask( anima::readImage<InputImageTypeUC>(maskFileArg.getValue()) );
66  else
67  {
68  InputImageTypeUC::Pointer maskImage = InputImageTypeUC::New();
69  maskImage->SetRegions(GraphCutFilter->GetInput(0)->GetLargestPossibleRegion());
70  maskImage->SetSpacing(GraphCutFilter->GetInput(0)->GetSpacing());
71  maskImage->SetOrigin(GraphCutFilter->GetInput(0)->GetOrigin());
72  maskImage->SetDirection(GraphCutFilter->GetInput(0)->GetDirection());
73 
74  maskImage->Allocate();
75  maskImage->FillBuffer(1);
76 
77  GraphCutFilter->SetMask(maskImage);
78  }
79 
80  if( sourcesMaskFileArg.getValue()!="" )
81  {
82  GraphCutFilter->SetInputSeedSourcesMask( anima::readImage<InputImageTypeUC>(sourcesMaskFileArg.getValue()) );
83  }
84 
85  if( sinksMaskFileArg.getValue()!="" )
86  {
87  GraphCutFilter->SetInputSeedSinksMask( anima::readImage<InputImageTypeUC>(sinksMaskFileArg.getValue()) );
88  }
89 
90  // Set parameters
91  GraphCutFilter->SetVerbose( verboseArg.getValue() );
92  GraphCutFilter->SetUseSpectralGradient( !(notUseSpecGradArg.getValue()) );
93  GraphCutFilter->SetTLinkMode( (TLinkMode) TLinkModeArg.getValue() );
94  GraphCutFilter->SetMultiVarSources( multiVarSourcesArg.getValue() );
95  GraphCutFilter->SetMultiVarSinks( multiVarSinksArg.getValue() );
96  GraphCutFilter->SetAlpha( alphaArg.getValue() );
97  GraphCutFilter->SetSigma( sigmaArg.getValue() );
98  GraphCutFilter->SetMatrixGradFilename( matrixGradArg.getValue() );
99  GraphCutFilter->SetOutputFilename( outputGCFileArg.getValue() );
100 
101 
102  // Process
103  itk::TimeProbe timer;
104 
105  timer.Start();
106 
107  try
108  {
109  GraphCutFilter->Update();
110  GraphCutFilter->WriteOutputs();
111  }
112  catch (itk::ExceptionObject &e)
113  {
114  std::cerr << e << std::endl;
115  return(1);
116  }
117 
118  timer.Stop();
119 
120  std::cout << "Elapsed Time: " << timer.GetTotal() << timer.GetUnit() << std::endl;
121 
122  return EXIT_SUCCESS;
123 
124 }
TLinkMode
int main(int argc, const char **argv)
Class performing grah cut segmentation. First the sources and sinks probabilities maps are computed u...