DGtalTools  0.9.4
itk2vol.cpp
1 
29 #include <iostream>
31 #include <fstream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/base/BasicFunctors.h"
35 #include "DGtal/images/ImageContainerBySTLVector.h"
36 #include "DGtal/io/writers/GenericWriter.h"
37 #include "DGtal/io/readers/ITKReader.h"
38 
39 #include <boost/program_options/options_description.hpp>
40 #include <boost/program_options/parsers.hpp>
41 #include <boost/program_options/variables_map.hpp>
42 
43 using namespace std;
44 using namespace DGtal;
45 
72 namespace po = boost::program_options;
74 
75 int main( int argc, char** argv )
76 {
79 
80  // parse command line ----------------------------------------------
81  po::options_description general_opt("Allowed options are ");
82  general_opt.add_options()
83  ("help,h", "display this message")
84  ("input,i", po::value<std::string>(), "Any file format in the ITK library (mhd, mha, ...) " )
85  ("output,o", po::value<std::string>(), "volumetric file (.vol, .pgm3d) " )
86  ("inputMin", po::value<int>()->default_value(-1000), "set minimum density threshold on Hounsfield scale")
87  ("inputMax", po::value<int>()->default_value(3000), "set maximum density threshold on Hounsfield scale");
88 
89 
90 
91  bool parseOK=true;
92  po::variables_map vm;
93  try{
94  po::store(po::parse_command_line(argc, argv, general_opt), vm);
95  }catch(const std::exception& ex){
96  parseOK=false;
97  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
98  }
99  po::notify(vm);
100  if( !parseOK || vm.count("help")||argc<=1)
101  {
102  std::cout << "Usage: " << argv[0] << " [input] [output]\n"
103  << "Converts itk file into a volumetric file (.vol, .pgm3d). "
104  << general_opt << "\n";
105  std::cout << "Example:\n"
106  << "itk2vol -i image.mhd --inputMin -500 --inputMax -100 -o sample.vol \n";
107  return 0;
108  }
109 
110  if(! vm.count("input") ||! vm.count("output"))
111  {
112  trace.error() << " Input and output filename are needed to be defined" << endl;
113  return 0;
114  }
115 
116 
117  string inputFilename = vm["input"].as<std::string>();
118  string outputFilename = vm["output"].as<std::string>();
119  int inputMin = vm["inputMin"].as<int>();
120  int inputMax = vm["inputMax"].as<int>();
122 
123  trace.info() << "Reading input input file " << inputFilename ;
124  Image3D inputImage = ITKReader< Image3D >::importITK(inputFilename);
125  trace.info() << " [done] " << std::endl ;
126  trace.info() << " converting into vol file... " ;
127  RescalFCT rescaleCustom(inputMin, inputMax, 0, 255);
128 
129  DGtal::GenericWriter<Image3D, 3, unsigned char, RescalFCT>::exportFile(outputFilename, inputImage, "UInt8Array3D", rescaleCustom);
130 
131 
132  trace.info() << " [done] " << std::endl ;
133 
134 
135  return 0;
136 
137 }
138 
139 
STL namespace.
static bool exportFile(const std::string &filename, const TContainer &anImage, const TFunctor &aFunctor=TFunctor())
Trace trace(traceWriterTerm)
std::ostream & info()
std::ostream & error()