DGtalTools  1.2.0
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 
38 #include "CLI11.hpp"
39 
40 using namespace std;
41 using namespace DGtal;
42 using namespace Z3i;
43 
44 
92 void missingParam ( std::string param )
93 {
94  trace.error() <<" Parameter: "<<param<<" is required..";
95  trace.info() <<std::endl;
96  exit ( 1 );
97 }
98 
99 
100 int main(int argc, char**argv)
101 {
102 
103  // parse command line using CLI ----------------------------------------------
104  CLI::App app;
105  std::string inputFileName;
106  std::string outputFileName {"result.vol"};
107 
108  std::vector <unsigned int> vectImgPlane {0,1};
109  unsigned int dimFlip {0};
110 
111  app.description("Flip 2D slice image of an 3D vol image (mirror transformation)\nBasic usage:\n \t volFlip --input <volFileName> --imagePlane 0 1 --flipDimension 0 --o <volOutputFileName> (vol, longvol, p3d format)\n Example:\n volFlip --imagePlane 0 1 --flipDimension 0 -i ${DGtal}/examples/samples/lobster.vol -o flippedXxyLobster.vol \n The resulting Z slice images (Z= cst) of flippedXxyLobster.p3d will appears flipped according the x axis. \n");
112  app.add_option("-i,--input,1", inputFileName, "Input vol file." )
113  ->required()
114  ->check(CLI::ExistingFile);
115 
116  app.add_option("--imagePlane", vectImgPlane, "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)", true)
117  ->expected(2);
118  app.add_option("--flipDimension",dimFlip,"specify which axis will be used to apply the flip.", true);
119  app.add_option("-o,--output, 2",outputFileName, "Output filename.", true );
120 
121 
122  app.get_formatter()->column_width(40);
123  CLI11_PARSE(app, argc, argv);
124  // END parse command line using CLI ----------------------------------------------
125 
126 
127  unsigned int dimFirstImg = vectImgPlane.at(0);
128  unsigned int dimSecondImg = vectImgPlane.at(1);
129 
130  unsigned int normalImgDim = (dimFirstImg!=2 && dimSecondImg!=2)? 2 :( (dimFirstImg!=1 && dimSecondImg!=1)? 1: 0 );
131 
132 
133  trace.beginBlock("Loading file");
134  typedef ImageContainerBySTLVector<Z3i::Domain, unsigned char> Image3D;
135  Image3D imageSRC = GenericReader<Image3D>::import ( inputFileName );
136  trace.endBlock();
137  Image3D imageRes(imageSRC.domain());
138  for(int i=0; i <= imageSRC.domain().upperBound()[normalImgDim]; i++){
139  Point startPoint(0,0, 0);
140  startPoint[normalImgDim]=i;
141  for( Domain::ConstSubRange::ConstIterator
142  it = imageSRC.domain().subRange(dimFirstImg, dimSecondImg,
143  startPoint).begin(),
144  itend = imageSRC.domain().subRange(dimFirstImg, dimSecondImg, startPoint).end();
145  it != itend; ++it){
146  Point pt = *it;
147  pt[dimFlip]= imageSRC.domain().upperBound()[dimFlip] - pt[dimFlip] ;
148  imageRes.setValue(*it, imageSRC(pt));
149  }
150  }
151 
152  trace.beginBlock("Exporting...");
153  bool res = VolWriter<Image3D>::exportVol(outputFileName, imageRes);
154  trace.endBlock();
155  if (res) return 0; else return 1;
156 }
Definition: ATu0v1.h:57