DGtalTools  0.9.4
volFlip.cpp
1 
29 #include <iostream>
30 #include <DGtal/base/Common.h>
31 #include <DGtal/helpers/StdDefs.h>
32 #include "DGtal/io/readers/GenericReader.h"
33 #include "DGtal/io/writers/GenericWriter.h"
34 
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  ( "imagePlane",po::value<std::vector <unsigned int> >()->multitoken(),
110  "arg= {0,1,2} x {0,1,2} defines the axis of the slice image which will be transformed (by default arg= 0 1 i.e. the slice image defined in the X,Y plane (Z=cst)" )
111  ( "flipDimension", po::value<unsigned int>(), "specify which axis will be used to apply the flip." )
112  ( "output,o", po::value<string>()->default_value("output.vol"),"Output filename." );
113  bool parseOK=true;
114 
115  po::variables_map vm;
116 
117 
118  try{
119  po::store ( po::parse_command_line ( argc, argv, general_opt ), vm );
120  }catch(const std::exception& ex){
121  parseOK=false;
122  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
123  }
124  po::notify ( vm );
125  if (!parseOK || ! ( vm.count ( "input" ) ) || ! ( vm.count ( "output" ) ) || ! ( vm.count ( "imagePlane" ) )
126  || ! ( vm.count ( "flipDimension" ) ) || vm.count ( "help" ))
127  {
128  trace.info() << "Flip 2D slice image of an 3D vol image (mirror transformation)"<<std::endl
129  << std::endl << "Basic usage: "<<std::endl
130  << "\t volFlip --input <volFileName> --imagePlane 0 1 --flipDimension 0 --o <volOutputFileName> (vol, longvol, p3d format)"<<std::endl
131  << general_opt << "\n";
132  std::cout << "Example:\n"
133  << "volFlip --imagePlane 0 1 --flipDimension 0 -i ${DGtal}/examples/samples/lobster.vol -o flippedXxyLobster.p3d \n The resulting Z slice images (Z= cst) of flippedXxyLobster.p3d will appears flipped according the x axis. ";
134  return 0;
135  }
136 
137 
138  //Parse options
139  if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
140  if ( ! ( vm.count ( "output" ) ) ) missingParam ( "--output" );
141  if ( ! ( vm.count ( "imagePlane" ) ) || vm["imagePlane"].as<std::vector<unsigned int > >().size()!=2 ) missingParam ( "--imagePlane" );
142  if ( ! ( vm.count ( "flipDimension" ) ) ) missingParam ( "--flipDimension" );
143 
144 
145  std::string inputFilename = vm["input"].as<std::string>();
146  std::string outputFileName = vm["output"].as<std::string>();
147 
148  unsigned int dimFirstImg = vm["imagePlane"].as<std::vector<unsigned int > >().at(0);
149  unsigned int dimSecondImg = vm["imagePlane"].as<std::vector<unsigned int > >().at(1);
150  unsigned int dimFlip = vm["flipDimension"].as<unsigned int>();
151 
152  unsigned int normalImgDim = (dimFirstImg!=2 && dimSecondImg!=2)? 2 :( (dimFirstImg!=1 && dimSecondImg!=1)? 1: 0 );
153 
154 
155  trace.beginBlock("Loading file");
157  Image3D imageSRC = GenericReader<Image3D>::import ( inputFilename );
158  trace.endBlock();
159  Image3D imageRes(imageSRC.domain());
160  for(int i=0; i <= imageSRC.domain().upperBound()[normalImgDim]; i++){
161  Point startPoint(0,0, 0);
162  startPoint[normalImgDim]=i;
164  it = imageSRC.domain().subRange(dimFirstImg, dimSecondImg,
165  startPoint).begin(),
166  itend = imageSRC.domain().subRange(dimFirstImg, dimSecondImg, startPoint).end();
167  it != itend; ++it){
168  Point pt = *it;
169  pt[dimFlip]= imageSRC.domain().upperBound()[dimFlip] - pt[dimFlip] ;
170  imageRes.setValue(*it, imageSRC(pt));
171  }
172  }
173 
174 
175  trace.beginBlock("Exporting...");
176  bool res = VolWriter< Image3D>::exportVol(outputFileName, imageRes);
177  trace.endBlock();
178  if (res) return 0; else return 1;
179 }
180 
181 
182 
183 
void beginBlock(const std::string &keyword="")
STL namespace.
double endBlock()
Trace trace(traceWriterTerm)
std::ostream & info()
const Domain & domain() const
typename Self::Point Point
std::ostream & error()