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/RigidTransformation3D.h>
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/parsers.hpp>
40 #include <boost/program_options/variables_map.hpp>
43 using namespace DGtal;
47 namespace po = boost::program_options;
55 void missingParam ( std::string param )
57 trace.error() <<
" Parameter: "<<param<<
" is required..";
58 trace.info() <<std::endl;
63 int main(
int argc,
char**argv)
67 po::options_description general_opt (
"Allowed options are: " );
68 general_opt.add_options()
69 (
"help,h",
"display this message." )
70 (
"input,i", po::value<std::string>(),
"Input file." )
71 (
"output,o", po::value<string>(),
"Output filename." )
72 (
"model,m", po::value<string>(),
"Transformation model: backward, forward." )
73 (
"angle,a", po::value<double>(),
"Rotation angle in radians." )
74 (
"ox", po::value<double>(),
"X coordinate of origin." )
75 (
"oy", po::value<double>(),
"Y coordinate of origin." )
76 (
"oz", po::value<double>(),
"Z coordinate of origin." )
77 (
"ax", po::value<double>(),
"X component of rotation axis." )
78 (
"ay", po::value<double>(),
"Y component of rotation axis." )
79 (
"az", po::value<double>(),
"Z component of rotation axis." )
80 (
"tx", po::value<double>(),
"X component of translation vector." )
81 (
"ty", po::value<double>(),
"Y component of translation vector." )
82 (
"tz", po::value<double>(),
"Z component of translation vector." );
87 po::store(po::parse_command_line(argc, argv, general_opt), vm);
88 }
catch(
const std::exception& ex){
90 trace.info()<<
"Error checking program options: "<< ex.what()<< endl;
94 if (!parseOK || vm.count (
"help" ) ||argc<=1 )
96 trace.info() <<
"Rotate 2D image."<<std::endl
97 << std::endl <<
"Basic usage: "<<std::endl
98 <<
"rigidTrans2D --ox 1.0 --oy 1.0 --oz 1 -a 1.2 --ax 1 --ay 1 --az 0 --tx 1 --ty 0 --tz 0 --m <forward|backward> --input <RawFileName> --output <VolOutputFileName> "<<std::endl
99 << general_opt <<
"\n";
104 if ( ! ( vm.count (
"input" ) ) ) missingParam (
"--input" );
105 std::string filename = vm[
"input"].as<std::string>();
106 if ( ! ( vm.count (
"output" ) ) ) missingParam (
"--output" );
107 std::string outputFileName = vm[
"output"].as<std::string>();
108 if ( ! ( vm.count (
"model" ) ) ) missingParam (
"--model" );
109 std::string model = vm[
"model"].as<std::string>();
110 if ( ! ( vm.count (
"angle" ) ) ) missingParam (
"--angle" );
111 double angle = vm[
"angle"].as<
double>();
112 if ( ! ( vm.count (
"ox" ) ) ) missingParam (
"--ox" );
113 double ox = vm[
"ox"].as<
double>();
114 if ( ! ( vm.count (
"oy" ) ) ) missingParam (
"--oy" );
115 double oy = vm[
"oy"].as<
double>();
116 if ( ! ( vm.count (
"oz" ) ) ) missingParam (
"--oz" );
117 double oz = vm[
"oz"].as<
double>();
118 if ( ! ( vm.count (
"ax" ) ) ) missingParam (
"--ax" );
119 double ax = vm[
"ax"].as<
double>();
120 if ( ! ( vm.count (
"ay" ) ) ) missingParam (
"--ay" );
121 double ay = vm[
"ay"].as<
double>();
122 if ( ! ( vm.count (
"az" ) ) ) missingParam (
"--az" );
123 double az = vm[
"az"].as<
double>();
124 if ( ! ( vm.count (
"tx" ) ) ) missingParam (
"--tx" );
125 double tx = vm[
"tx"].as<
double>();
126 if ( ! ( vm.count (
"ty" ) ) ) missingParam (
"--ty" );
127 double ty = vm[
"ty"].as<
double>();
128 if ( ! ( vm.count (
"tz" ) ) ) missingParam (
"--tz" );
129 double tz = vm[
"tz"].as<
double>();
131 typedef ImageSelector<Domain, unsigned char >::Type Image;
132 typedef ForwardRigidTransformation3D < Space > ForwardTrans;
133 typedef BackwardRigidTransformation3D < Space > BackwardTrans;
134 typedef ConstImageAdapter<Image, Domain, BackwardTrans, Image::Value, Identity > MyImageBackwardAdapter;
135 typedef DomainRigidTransformation3D < Domain, ForwardTrans > MyTransformedDomain;
136 typedef MyTransformedDomain::Bounds Bounds;
138 Image image = GenericReader <Image, 3>::import(filename);
139 ForwardTrans forwardTrans( RealPoint ( ox, oy, oz ), RealPoint ( ax, ay, az ), angle, RealVector( tx, ty, tz ) );
140 BackwardTrans backwardTrans( RealPoint ( ox, oy, oz ), RealPoint ( ax, ay, az ), angle, RealVector( tx, ty, tz ) );
141 MyTransformedDomain domainForwardTrans ( forwardTrans );
142 Bounds bounds = domainForwardTrans ( image.domain() );
143 Domain transformedDomain ( bounds.first, bounds.second );
146 if ( model ==
"forward" )
148 Image forwardTransformedImage ( transformedDomain );
149 for ( Domain::ConstIterator it = image.domain().begin(); it != image.domain().end(); ++it )
151 forwardTransformedImage.setValue ( forwardTrans ( *it ), image ( *it ) );
153 GenericWriter<Image, 3, unsigned char, Identity>::exportFile(outputFileName, forwardTransformedImage,
" ", idD);
157 MyImageBackwardAdapter backwardImageAdapter ( image, transformedDomain , backwardTrans, idD );
158 GenericWriter<MyImageBackwardAdapter, 3, unsigned char, Identity>::exportFile(outputFileName, backwardImageAdapter,
" ", idD);