DGtal  1.5.beta
example2DBijectiveRotations.cpp
1 
17 #pragma once
18 
27 #include <iostream>
28 #include "DGtal/images/ImageContainerBySTLVector.h"
29 #include "DGtal/helpers/StdDefs.h"
30 #include "DGtal/base/Common.h"
31 #include "DGtal/io/Color.h"
32 #include "DGtal/io/readers/PPMReader.h"
33 #include "DGtal/io/writers/GenericWriter.h"
34 #include "DGtal/images/RigidTransformation2D.h"
35 
36 
38 // Inclusions
39 #include "DGtal/images/bijectiverotations/QSH.h"
40 #include "DGtal/images/bijectiverotations/CDLR.h"
41 #include "DGtal/images/bijectiverotations/RBC.h"
42 #include "DGtal/images/bijectiverotations/OTC.h"
43 #include "DGtal/images/bijectiverotations/CBDR.h"
44 #include "DGtal/images/bijectiverotations/Rotationtables.h"
45 
46 
47 using namespace std;
48 using namespace DGtal;
49 using namespace functors;
50 using namespace Z2i;
51 
52 std::vector<std::string> supportedBijectiveRotation = {
53  "OTC", "CBDR", "CDLR", "QSH" , "RBC"
54 };
55 
56 // Generic function to handle rotation based on type
57 template <typename TImage,typename TBijectiveRotation>
58 TImage
59 performRotation( const TImage& img, TBijectiveRotation& obj )
60 {
61  const auto& sbr = supportedBijectiveRotation;
62  std::string structName = obj.tostring();
63  TImage output( img.domain() );
64  if ( std::find( sbr.begin(), sbr.end(), structName ) != sbr.end() )
65  {
66  trace.beginBlock ( obj.tostring() );
67  output = obj.rotateImage( img );
68  trace.endBlock ();
69  }
70  else
71  throw std::runtime_error("Unsupported bijective rotation : " + structName);
72  return output;
73 }
74 
75 void usage( const std::string& cmd )
76 {
77  std::cout << "Usage: " << cmd << " <image> <angle> [<method>] [black|*white*] [detect]" << "\n"
78  << "\t Compute the rotated <image> by an angle <angle> (in degrees) \n"
79  << "\t - <method> in { OTC, CBDR, CDLR, QSH, *RBC* }\n"
80  << std::endl;
81 }
82 
83 
84 int main( int argc, char* argv[] )
85 {
86  if ( argc < 3 ) { usage( argv[ 0 ] ); return 1; }
87  std::string fname = argv[ 1 ];
88  double degree = std::atof( argv[ 2 ] );
89  double angle = degree * M_PI / 180.0;
90  std::string method = ( argc > 3 ) ? argv[ 3 ] : "RBC";
91 
93  typedef ForwardRigidTransformation2D < Space > ForwardTrans;
94  typedef DomainRigidTransformation2D < Domain, ForwardTrans > MyDomainTransformer;
95  typedef MyDomainTransformer::Bounds Bounds;
96 
97  Image image = DGtal::PPMReader<Image>::importPPM ( fname );
98  Image output = image;
99  int W=image.domain().myUpperBound[0]+1;
100  int H=image.domain().myUpperBound[1]+1;
101  trace.info() << "Image has size " << W << "x" << H << std::endl;
102  Point c(W/2, H/2);
103 
104  if ( method == "QSH" )
105  {
106  DGtal::QSH<Space> rotQSH(angle,c);
107  output = performRotation<Image,QSH<Space>> (image,rotQSH);
108  }
109  else if ( method == "CDLR" )
110  {
111  auto linf = std::make_shared<DGtal::LinfPolicy<Space,Domain,DGtal::CDLR_naiverotation<Space>>>();
112  DGtal::CDLR<DGtal::SpaceND<2, DGtal::int32_t> > rotDSL(angle, c, linf);
113  output = performRotation<Image,CDLR<Space>> (image,rotDSL);
114  }
115  else if ( method == "RBC" )
116  {
117  DGtal::RBC_vec<Space,RealPoint> rot_rbcvec(2*max(W,H));
118  rot_rbcvec.setAngle() = angle;
119  rot_rbcvec.center() = c;
120  DGtal::RBC<Space,RealPoint> rot_RBC(rot_rbcvec,angle,c);
121  output = performRotation<Image,DGtal::RBC<Space,RealPoint>> (image,rot_RBC);
122  }
123  else if ( method == "CBDR" )
124  {
125  auto linfCBDR = std::make_shared<DGtal::LinfPolicy<Space,Domain,DGtal::CBDR_naiverotation<Space>>>();
126  const int n = 4;
127  const int kmax=30;
128  DGtal::CBDR<Space,RealPoint> rot_CBDR(angle,c,n,kmax,linfCBDR,true);
129  output = performRotation<Image,DGtal::CBDR<Space,RealPoint>> (image,rot_CBDR);
130  }
131 
132  else if ( method == "OTC" )
133  {
134  int rwidth = 2;
135  std::vector< std::vector< int > > tableOTC = DGtal::functions::loadOTCTable<Space>("../tables/",rwidth);
136  DGtal::OTC<Space,RealPoint> rot_OTC( tableOTC, rwidth, c, W, H );
137  int alpha = int( round( angle * 180.0 / M_PI ) );
138  rot_OTC.set_angle( alpha ); // in degree
139  output = performRotation<Image,DGtal::OTC<Space,RealPoint>> (image,rot_OTC);
140  }
141 
142 
143  std::string out_fname = "rotated-image-" + method + "-" + std::to_string( int(degree) );
144  out_fname += ".ppm";
145  output >> out_fname;
146  return 0;
147 }
void usage(int, char **argv)
CDLR : Rotation with Discrete Line Reflections,.
Definition: CDLR.h:53
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
const Domain & domain() const
Definition: Image.h:192
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
CBDR : Composition of Bijective Digitized Reflections,.
Definition: CBDR.h:57
OTC : Optimal Transport through Circle bijective rotation.
Definition: OTC.h:52
static ImageContainer importPPM(const std::string &aFilename, const Functor &aFunctor=functors::ColorRGBEncoder< Value >(), bool topbotomOrder=true)
QSH : Quasi Shears represents a bijective rotation through shears.
Definition: QSH.h:50
RBC : Bijective Rotation through Circles.
Definition: RBC_vec.h:56
RBC : Bijective Rotation through Circles.
Definition: RBC.h:55
int main(int argc, char **argv)
int max(int a, int b)
std::vector< std::string > supportedBijectiveRotation
double angle(const DGtal::Z2i::RealPoint &point)
Image image(domain)
ImageContainerBySTLVector< Domain, Value > Image