DGtal  1.5.beta
geometry/curves/exampleFrechetShortcut.cpp

This snippet segments a digital curve into Frechet shortcuts.

See also
Fréchet Shortcuts and Analysis of one-dimensional discrete structures
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "ConfigExamples.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/geometry/curves/FrechetShortcut.h"
#include "DGtal/geometry/curves/GreedySegmentation.h"
using namespace std;
using namespace DGtal;
using namespace Z2i;
int main( int argc, char** argv )
{
trace.beginBlock ( "Example FrechetShortcut" );
trace.info() << "Args:";
for ( int i = 0; i < argc; ++i )
trace.info() << " " << argv[ i ];
trace.info() << endl;
std::string filename;
double error;
if(argc == 1)
{
trace.info() << "Use default file and error value\n";
filename = examplesPath + "samples/plant-frechet.dat";
error = 3;
}
else
if(argc != 3)
{
trace.info() << "Please enter a filename and error value.\n";
return 0;
}
else
{
filename = argv[1];
error = atof(argv[2]);
}
ifstream instream; // input stream
instream.open (filename.c_str(), ifstream::in);
Curve c; //grid curve
c.initFromVectorStream(instream);
Board2D board;
// Display the pixels as arrows range to show the way the curve is scanned
board << c.getArrowsRange();
trace.beginBlock("Simple example");
Curve::PointsRange r = c.getPointsRange();
typedef FrechetShortcut<Curve::PointsRange::ConstIterator,int> Shortcut;
// Computation of one shortcut
Shortcut s(error);
s.init( r.begin() );
while ( ( s.end() != r.end() )
&&( s.extendFront() ) ) {}
// Computation of a greedy segmentation
typedef GreedySegmentation<Shortcut> Segmentation;
Segmentation theSegmentation( r.begin(), r.end(), Shortcut(error) );
// the segmentation is computed here
Segmentation::SegmentComputerIterator it = theSegmentation.begin();
Segmentation::SegmentComputerIterator itEnd = theSegmentation.end();
for ( ; it != itEnd; ++it) {
s=Shortcut(*it);
trace.info() << s << std::endl;
board << s;
}
board.saveEPS("FrechetShortcutExample.eps", Board2D::BoundingBox, 5000 );
#ifdef WITH_CAIRO
board.saveCairo("FrechetShortcutExample.png");
#endif
return 0;
}
// //
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
GridCurve< K2 > Curve
Definition: StdDefs.h:116
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
MessageStream error
int main(int argc, char **argv)
SaturatedSegmentation< SegmentComputer > Segmentation