DGtalTools 2.1.0
Loading...
Searching...
No Matches
tangentBC.cpp
1
31#include <cmath>
32#include <iostream>
33#include <vector>
34#include <string>
35
36#include "CLI11.hpp"
37
38#include "DGtal/base/Common.h"
39#include "DGtal/helpers/StdDefs.h"
40#include "DGtal/io/readers/PointListReader.h"
41
42//Grid curve
43#include "DGtal/geometry/curves/FreemanChain.h"
44#include "DGtal/geometry/curves/GridCurve.h"
45
46//Estimators
47#include "DGtal/geometry/curves/BinomialConvolver.h"
48
49using namespace DGtal;
50
52
104int main( int argc, char** argv )
105{
106 // parse command line CLI ----------------------------------------------
107 CLI::App app;
108 std::string fileName;
109 double h {1.0};
110
111 app.description("Estimates tangent using a binomial convolver.\n Typical use example:\n \t tangentBC [options] --input <fileName>\n");
112 auto filenameOpt = app.add_option("--input,-i,1",fileName,"input file name: FreemanChain (.fc) or a sequence of discrete points (.sdp).")->required()->check(CLI::ExistingFile);
113 app.add_option("--GridStep", h, "Grid step (default 1.0)");
114
115 app.get_formatter()->column_width(40);
116 CLI11_PARSE(app, argc, argv);
117 // END parse command line using CLI ----------------------------------------------
118
119 if(filenameOpt->count()>0){
120 std::string extension = fileName.substr( fileName.find_last_of(".") + 1 );
121 bool isSDP = extension == "sdp";
122 typedef Z2i::Space Space;
123 typedef Space::Point Point;
124 typedef PointVector<2, double> RealPoint;
125 typedef Space::Integer Integer;
126 typedef FreemanChain<Integer> FreemanChain;
127 typedef std::vector< Point > Storage;
128 typedef Storage::const_iterator ConstIteratorOnPoints;
129
130 std::vector< FreemanChain > vectFcs;
131 if(!isSDP)
132 {
133 vectFcs =
134 PointListReader< Point >:: getFreemanChainsFromFile<Integer> (fileName);
135 }
136 for(unsigned int i=0; i<vectFcs.size() || (i==0 && isSDP); i++){
137 Storage vectPts;
138 bool isClosed;
139 if(!isSDP)
140 {
141 isClosed = vectFcs.at(i).isClosed();
142 std::cout << "# grid curve " << i << "/" << vectFcs.size() << " "
143 << ( (isClosed)?"closed":"open" ) << std::endl;
144 FreemanChain::getContourPoints( vectFcs.at(i), vectPts );
145 }
146 else
147 {
148 vectPts = PointListReader<Z2i::Point>::getPointsFromFile(fileName);
149 Z2i::Point pf =vectPts[0];
150 Z2i::Point pl =vectPts[vectPts.size()-1];
151 isClosed = (pf[0]-pl[0])+(pf[1]-pl[1]) <= 1;
152 }
153
154 // Binomial
155 std::cout << "# Curvature estimation from binomial convolution" << std::endl;
156 typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
157 std::cout << "# mask size = " <<
158 MyBinomialConvolver::suggestedSize( h, vectPts.begin(), vectPts.end() ) << std::endl;
159 typedef
160 TangentFromBinomialConvolverFunctor< MyBinomialConvolver, RealPoint >
161 TangentBCFct;
162 BinomialConvolverEstimator< MyBinomialConvolver, TangentBCFct>
163 BCTangentEstimator;
164
165 BCTangentEstimator.init( h, vectPts.begin(), vectPts.end(), isClosed );
166
167 std::vector<RealPoint> tangents( vectPts.size() );
168 BCTangentEstimator.eval( vectPts.begin(), vectPts.end(),
169 tangents.begin() );
170
171 // Output
172 std::cout << "# id tangent.x tangent.y angle(atan2(y,x)) x y" << std::endl;
173 unsigned int j = 0;
174 for ( ConstIteratorOnPoints
175 it = vectPts.begin(), it_end = vectPts.end();
176 it != it_end; ++it, ++j )
177 {
178 double x = tangents[ j ][ 0 ];
179 double y = tangents[ j ][ 1 ];
180 std::cout << j << std::setprecision( 15 )
181 << " " << x << " " << y
182 << " " << atan2( y, x ) << " " << (*it)[0] << " " << (*it)[1]
183 << std::endl;
184 }
185
186 }
187
188 }
189 return 0;
190}
191
Definition ATu0v1.h:57