DGtalTools 2.0.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
103int main( int argc, char** argv )
104{
105 // parse command line CLI ----------------------------------------------
106 CLI::App app;
107 std::string fileName;
108 double h {1.0};
109
110 app.description("Estimates tangent using a binomial convolver.\n Typical use example:\n \t tangentBC [options] --input <fileName>\n");
111 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);
112 app.add_option("--GridStep", h, "Grid step (default 1.0)");
113
114 app.get_formatter()->column_width(40);
115 CLI11_PARSE(app, argc, argv);
116 // END parse command line using CLI ----------------------------------------------
117
118 if(filenameOpt->count()>0){
119 std::string extension = fileName.substr( fileName.find_last_of(".") + 1 );
120 bool isSDP = extension == "sdp";
121 typedef Z2i::Space Space;
122 typedef Space::Point Point;
123 typedef PointVector<2, double> RealPoint;
124 typedef Space::Integer Integer;
125 typedef FreemanChain<Integer> FreemanChain;
126 typedef std::vector< Point > Storage;
127 typedef Storage::const_iterator ConstIteratorOnPoints;
128
129 std::vector< FreemanChain > vectFcs;
130 if(!isSDP)
131 {
132 vectFcs =
133 PointListReader< Point >:: getFreemanChainsFromFile<Integer> (fileName);
134 }
135 for(unsigned int i=0; i<vectFcs.size() || (i==0 && isSDP); i++){
136 Storage vectPts;
137 bool isClosed;
138 if(!isSDP)
139 {
140 isClosed = vectFcs.at(i).isClosed();
141 std::cout << "# grid curve " << i << "/" << vectFcs.size() << " "
142 << ( (isClosed)?"closed":"open" ) << std::endl;
143 FreemanChain::getContourPoints( vectFcs.at(i), vectPts );
144 }
145 else
146 {
147 vectPts = PointListReader<Z2i::Point>::getPointsFromFile(fileName);
148 Z2i::Point pf =vectPts[0];
149 Z2i::Point pl =vectPts[vectPts.size()-1];
150 isClosed = (pf[0]-pl[0])+(pf[1]-pl[1]) <= 1;
151 }
152
153 // Binomial
154 std::cout << "# Curvature estimation from binomial convolution" << std::endl;
155 typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
156 std::cout << "# mask size = " <<
157 MyBinomialConvolver::suggestedSize( h, vectPts.begin(), vectPts.end() ) << std::endl;
158 typedef
159 TangentFromBinomialConvolverFunctor< MyBinomialConvolver, RealPoint >
160 TangentBCFct;
161 BinomialConvolverEstimator< MyBinomialConvolver, TangentBCFct>
162 BCTangentEstimator;
163
164 BCTangentEstimator.init( h, vectPts.begin(), vectPts.end(), isClosed );
165
166 std::vector<RealPoint> tangents( vectPts.size() );
167 BCTangentEstimator.eval( vectPts.begin(), vectPts.end(),
168 tangents.begin() );
169
170 // Output
171 std::cout << "# id tangent.x tangent.y angle(atan2(y,x)) x y" << std::endl;
172 unsigned int j = 0;
173 for ( ConstIteratorOnPoints
174 it = vectPts.begin(), it_end = vectPts.end();
175 it != it_end; ++it, ++j )
176 {
177 double x = tangents[ j ][ 0 ];
178 double y = tangents[ j ][ 1 ];
179 std::cout << j << std::setprecision( 15 )
180 << " " << x << " " << y
181 << " " << atan2( y, x ) << " " << (*it)[0] << " " << (*it)[1]
182 << std::endl;
183 }
184
185 }
186
187 }
188 return 0;
189}
190
Definition ATu0v1.h:57