DGtalTools  1.2.0
rigidTransform2D.cpp
1 
28 #include <iostream>
29 #include <DGtal/base/Common.h>
30 #include <DGtal/io/readers/GenericReader.h>
31 #include <DGtal/io/writers/GenericWriter.h>
32 #include <DGtal/helpers/StdDefs.h>
33 #include <DGtal/images/Image.h>
34 #include <DGtal/images/ImageSelector.h>
35 #include <DGtal/images/ConstImageAdapter.h>
36 #include <DGtal/images/RigidTransformation2D.h>
37 
38 #include "CLI11.hpp"
39 
40 using namespace std;
41 using namespace DGtal;
42 using namespace Z2i;
43 using namespace functors;
44 
77 int main(int argc, char**argv)
78 {
79  // parse command line using CLI ----------------------------------------------
80  CLI::App app;
81  std::string filename;
82  std::string outputFileName;
83  std::string model;
84  double angle {0};
85  double ox {0};
86  double oy {0};
87  double tx {0};
88  double ty {0};
89 
90  app.description("Apply rigid transformation on a given image.\n Typical use example:\n \t rigidTrans2D --input <RawFileName> --output <OutputFileName> --ox 1.0 --oy 1.0 -a 1.2 --tx 1 --ty 0 --m <forward|backward>\n");
91  app.add_option("-i,--input,1",filename,"Input file.")->required()->check(CLI::ExistingFile);
92  app.add_option("-o,--output,2",outputFileName,"Output file.")->required();
93  app.add_option("-m,--model",model,"Transformation model: backward, forward.")->required();
94  app.add_option("-a,--angle",angle,"Rotation angle in radians (default 0)",true);
95  app.add_option("--ox",ox,"X coordinate of origin (default 0)",true);
96  app.add_option("--oy",oy,"Y coordinate of origin (default 0)",true);
97  app.add_option("--tx",tx,"X component of translation vector (default 0)",true);
98  app.add_option("--ty",ty,"Y component of translation vector (default 0)",true);
99 
100  app.get_formatter()->column_width(40);
101  CLI11_PARSE(app, argc, argv);
102  // END parse command line using CLI ----------------------------------------------
103 
104  typedef ImageSelector<Domain, unsigned char >::Type Image;
105  typedef ForwardRigidTransformation2D < Space > ForwardTrans;
106  typedef BackwardRigidTransformation2D < Space > BackwardTrans;
107  typedef ConstImageAdapter<Image, Domain, BackwardTrans, Image::Value, Identity > MyImageBackwardAdapter;
108  typedef DomainRigidTransformation2D < Domain, ForwardTrans > MyTransformedDomain;
109  typedef MyTransformedDomain::Bounds Bounds;
110 
111  Image image = GenericReader <Image, 2>::import(filename);
112  ForwardTrans forwardTrans( RealPoint ( ox, oy ), angle, RealVector( tx, ty ) );
113  BackwardTrans backwardTrans( RealPoint ( ox, oy ), angle, RealVector( tx, ty ) );
114  MyTransformedDomain domainForwardTrans ( forwardTrans );
115  Bounds bounds = domainForwardTrans ( image.domain() );
116  Domain transformedDomain ( bounds.first, bounds.second );
117  Identity idD;
118 
119  if ( model == "forward" )
120  {
121  Image forwardTransformedImage ( transformedDomain );
122  for ( Domain::ConstIterator it = image.domain().begin(); it != image.domain().end(); ++it )
123  {
124  forwardTransformedImage.setValue ( forwardTrans ( *it ), image ( *it ) );
125  }
126  GenericWriter<Image, 2, unsigned char, Identity>::exportFile(outputFileName, forwardTransformedImage, idD);
127  }
128  else
129  {
130  MyImageBackwardAdapter backwardImageAdapter ( image, transformedDomain , backwardTrans, idD );
131  GenericWriter<MyImageBackwardAdapter, 2, unsigned char, Identity>::exportFile(outputFileName, backwardImageAdapter, idD);
132  }
133  return 0;
134 }
Definition: ATu0v1.h:57