DGtalTools  0.9.4
volTrValues.cpp
1 
30 #include <iostream>
31 #include <DGtal/base/Common.h>
32 #include <DGtal/io/readers/GenericReader.h>
33 #include <DGtal/io/writers/GenericWriter.h>
34 #include <DGtal/helpers/StdDefs.h>
35 #include <DGtal/images/ImageContainerBySTLVector.h>
36 #include <DGtal/images/ConstImageAdapter.h>
37 #include <boost/program_options/options_description.hpp>
38 #include <boost/program_options/parsers.hpp>
39 #include <boost/program_options/variables_map.hpp>
40 
41 
42 using namespace std;
43 using namespace DGtal;
44 using namespace Z3i;
45 
46 namespace po = boost::program_options;
47 
48 
97 void missingParam ( std::string param )
98 {
99  trace.error() <<" Parameter: "<<param<<" is required..";
100  trace.info() <<std::endl;
101  exit ( 1 );
102 }
103 
104 
105 int main(int argc, char**argv)
106 {
107 
108  // parse command line ----------------------------------------------
109  po::options_description general_opt ( "Allowed options are: " );
110  general_opt.add_options()
111  ( "help,h", "display this message." )
112  ( "input,i", po::value<std::string>(), "Input vol file." )
113  ( "output,o", po::value<std::string>()->default_value("output.vol"),"Output filename." )
114  ("inputVals,s", po::value<std::vector<unsigned int > >()->multitoken(), "specify the values which will be transformed with the output values (given with --outputVals)." )
115  ("outputVals,r", po::value<std::vector<unsigned int > >()->multitoken(), "specify the output values to transformed according to the input values (given with --inputVals)." ) ;
116  bool parseOK=true;
117 
118  po::variables_map vm;
119 
120 
121  try{
122  po::store ( po::parse_command_line ( argc, argv, general_opt ), vm );
123  }catch(const std::exception& ex){
124  parseOK=false;
125  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
126  }
127  po::notify ( vm );
128  if (!parseOK || !vm.count("inputVals")|| !vm.count("outputVals") || !vm.count("input") || !vm.count("output") || vm.count ( "help" ))
129  {
130  trace.info() << "Apply basic vol image transform from the input values to output values."<<std::endl
131  << std::endl << "Basic usage: "<<std::endl
132  << "\t volTrValues --input <volFileName> --o <volOutputFileName> -s 1 99 -r 100 200 "<<std::endl
133  << "\t => all voxel of values 1 (resp. 99) will be 100 (resp. 200) in the resulting image. "<<std::endl
134  << general_opt << "\n";
135  if( !vm.count("inputVals")){
136  missingParam("inputVals");
137  }
138  if( !vm.count("outputVals")){
139  missingParam("outputVals");
140  }
141  if( !vm.count("input")){
142  missingParam("input");
143  }
144  if( !vm.count("output")){
145  missingParam("output");
146  }
147  return 0;
148  }
149 
150 
151  //Parse options
152 
153 
154  std::string filename = vm["input"].as<std::string>();
155  if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
156  std::string outputFileName = vm["output"].as<std::string>();
157 
158 
159  std::vector<unsigned int> inputVals = vm["inputVals"].as<std::vector<unsigned int > >();
160  std::vector<unsigned int> outputVals = vm["outputVals"].as<std::vector<unsigned int > >();
161 
162  if(inputVals.size()!=outputVals.size()){
163  trace.error()<< "Transformation not possible the two sets of input/output values should have the same size." << std::endl;
164  exit(1);
165  }
166 
167  trace.beginBlock("Loading file");
169  MyImageC image = GenericReader< MyImageC >::import( filename );
170  trace.endBlock();
171  unsigned int val;
172  for(MyImageC::Domain::ConstIterator it = image.domain().begin(),
173  itend = image.domain().end(); it != itend; ++it)
174  {
175  val = image(*it);
176  for(unsigned int i = 0; i< inputVals.size(); i++){
177  if(inputVals.at(i)==val){
178  image.setValue( *it , outputVals.at(i));
179  }
180  }
181  }
182 
183 
184  trace.beginBlock("Exporting...");
185  bool res = GenericWriter<MyImageC>::exportFile(outputFileName, image);
186  trace.endBlock();
187 
188  if (res) return 0; else return 1;
189 }
190 
191 
192 
193 
void beginBlock(const std::string &keyword="")
STL namespace.
double endBlock()
Trace trace(traceWriterTerm)
std::ostream & info()
std::ostream & error()