DGtalTools  0.9.2
freeman2sdp.cpp
1 
29 #include <iostream>
31 
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 
35 //image
36 #include "DGtal/io/readers/PointListReader.h"
37 
38 
39 //contour
40 #include "DGtal/geometry/curves/FreemanChain.h"
41 
42 
43 //boost
44 #include <boost/program_options/options_description.hpp>
45 #include <boost/program_options/parsers.hpp>
46 #include <boost/program_options/variables_map.hpp>
47 
48 //STL
49 #include <vector>
50 #include <string>
51 
52 using namespace DGtal;
53 
103 namespace po = boost::program_options;
105 
106 int main( int argc, char** argv )
107 {
108  // parse command line ----------------------------------------------
109  po::options_description general_opt("Allowed options are: ");
110  general_opt.add_options()
111  ("help,h", "display this message")
112  ("input,i", po::value<std::string>(), "Input freeman chain file name.")
113  ("info", "adds some info as comments at the beginning of the file.")
114  ("oneLine,o", " output the digital contour in one line like: X0 Y0 X1 Y1 ... XN YN");
115 
116 
117 
118  typedef FreemanChain<Z2i::Integer> FreemanChain;
119 
120 
121  bool parseOK=true;
122  po::variables_map vm;
123  try{
124  po::store(po::parse_command_line(argc, argv, general_opt), vm);
125  }catch(const std::exception& ex){
126  parseOK=false;
127  trace.info()<< "Error checking program options: "<< ex.what()<< std::endl;
128  }
129 
130  po::notify(vm);
131  if(!parseOK||vm.count("help")||argc<=1 || (!(vm.count("input")) ) )
132  {
133  trace.info()<< "Transform freeman chain into a Sequence of Discrete Points. Result is given to std output " <<std::endl << "Basic usage: "<<std::endl
134  << "\t freeman2sdp [input] > out.sdp "<<std::endl
135  << general_opt << "\n";
136  trace.info() << "Example:\n"
137  << "freeman2sdp -i ${DGtal}/tests/samples/contourS.fc > contourS.sdp \n";
138 
139  return 0;
140  }
141 
142  bool oneline = vm.count("oneLine");
143  if( vm.count("input") ){
144  std::string fileName = vm["input"].as<std::string>();
145  std::vector< FreemanChain > vectFcs = PointListReader< Z2i::Point >:: getFreemanChainsFromFile<Z2i::Integer> (fileName);
146 
147  for(unsigned int i=0; i< vectFcs.size(); i++){
148  bool isClosed = vectFcs.at(i).isClosed();
149  std::cout << "# grid curve " << i+1 << "/" << vectFcs.size()
150  << ( (isClosed)?" closed":" open" ) << std::endl;
151  if ( vm.count("info"))
152  std::cout << "# SDP contour" << i+1<< "/" << vectFcs.size() << " "
153  << "# size=" << vectFcs.at(i).size() << std::endl;
154  std::vector<Z2i::Point> vectPts;
155  FreemanChain::getContourPoints( vectFcs.at(i), vectPts );
156  for(unsigned int k=0; k < vectPts.size(); k++){
157  std::cout << vectPts.at(k)[0] << " "<< vectPts.at(k)[1];
158  if(!oneline){
159  std::cout << std::endl;
160  }
161  }
162  std::cout << std::endl;
163  }
164 
165  }
166 
167 
168 }
169