DGtalTools  0.9.4
volCrop.cpp
1 
30 #include <iostream>
31 #include <DGtal/base/Common.h>
32 #include <DGtal/io/readers/VolReader.h>
33 #include <DGtal/io/writers/VolWriter.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 
49 
93 void missingParam ( std::string param )
94 {
95  trace.error() <<" Parameter: "<<param<<" is required..";
96  trace.info() <<std::endl;
97  exit ( 1 );
98 }
99 
100 
101 int main(int argc, char**argv)
102 {
103 
104  // parse command line ----------------------------------------------
105  po::options_description general_opt ( "Allowed options are: " );
106  general_opt.add_options()
107  ( "help,h", "display this message." )
108  ( "input,i", po::value<std::string>(), "Input vol file." )
109  ( "xMin", po::value<unsigned int>()->default_value(0), "x coordinate of lower point." )
110  ( "yMin", po::value<unsigned int >()->default_value(0), "y coordinate of lower point." )
111  ( "zMin", po::value<unsigned int >()->default_value(0), "z coordinate of lower point." )
112  ( "xMax", po::value<unsigned int >(), "x coordinate of upper point." )
113  ( "yMax", po::value<unsigned int>(), "y coordinate of upper point." )
114  ( "zMax", po::value<unsigned int>(), "z coordinate of upper point." )
115  ( "output,o", po::value<string>()->default_value("output.vol"),"Output filename." );
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 ( "help" ) || !vm.count("input"))
129  {
130  trace.info() << " Crops a 3D vol image from domain coordinates."<<std::endl
131  << std::endl << "Basic usage: "<<std::endl
132  << "\t volCrop --input <volFileName> --o <volOutputFileName> (both files can be independently in vol, pgm3D, p3d format)"<<std::endl
133  << general_opt << "\n";
134  std::cout << "Example:\n"
135  << "volCrop --xMin 50 --yMin 50 --zMin 10 --xMax 150 --yMax 150 --zMax 50 -i ${DGtal}/examples/samples/lobster.vol -o croppedLobster.vol \n";
136  return 0;
137  }
138 
139 
140  //Parse options
141  if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
142  if ( ! ( vm.count ( "xMax" ) ) ) missingParam ( "--xMax" );
143  if ( ! ( vm.count ( "yMax" ) ) ) missingParam ( "--yMax" );
144  if ( ! ( vm.count ( "zMax" ) ) ) missingParam ( "--zMax" );
145 
146  std::string filename = vm["input"].as<std::string>();
147  if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
148  std::string outputFileName = vm["output"].as<std::string>();
149 
150  Z3i::Point ptLow( vm["xMin"].as<unsigned int>(), vm["yMin"].as<unsigned int>(),vm["zMin"].as<unsigned int>());
151  Z3i::Point ptMax( vm["xMax"].as<unsigned int>(), vm["yMax"].as<unsigned int>(),vm["zMax"].as<unsigned int>());
152 
153  trace.beginBlock("Loading file");
155  MyImageC imageC = VolReader< MyImageC >::importVol ( filename );
156  functors::Identity df;
157 
159  Domain subDomain(ptLow, ptMax);
160  ConstImageAdapterForSubImage subImage(imageC, subDomain, df, df);
161  trace.endBlock();
162 
163  trace.beginBlock("Exporting...");
164  bool res = VolWriter< ConstImageAdapterForSubImage>::exportVol(outputFileName, subImage);
165  trace.endBlock();
166  if (res) return 0; else return 1;
167 }
168 
169 
170 
171 
void beginBlock(const std::string &keyword="")
STL namespace.
double endBlock()
Trace trace(traceWriterTerm)
std::ostream & info()
std::ostream & error()
typename Self::Domain Domain