DGtal  0.9.4beta
geometry/curves/greedy-dss-decomposition.cpp

This example outputs a greedy segmentation of a closed and 4-connected digital curve into DSSs.

* $ ./examples/geometry/curves/greedy-dss-decomposition
* 
greedy-dss-decomposition.png
Greedy segmentation into DSSs
See also
Digital straight lines and segments and Analysis of one-dimensional discrete structures
geometry/curves/greedy-dss-decomposition.cpp
#include <cmath>
#include <iostream>
#include <sstream>
#include "DGtal/base/Common.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/io/Color.h"
#include "DGtal/io/colormaps/GradientColorMap.h"
#include "DGtal/shapes/Shapes.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/geometry/curves/ArithmeticalDSSComputer.h"
#include "DGtal/geometry/curves/FreemanChain.h"
#include "DGtal/geometry/curves/GreedySegmentation.h"
using namespace std;
using namespace DGtal;
using namespace Z2i;
int main( )
{
trace.beginBlock ( "Example dgtalboard-5-greedy-dss" );
typedef FreemanChain<int> Contour4;
typedef GreedySegmentation<DSS4> Decomposition4;
// A Freeman chain code is a string composed by the coordinates of the first pixel, and the list of elementary displacements.
std::stringstream ss(stringstream::in | stringstream::out);
ss << "31 16 11121212121212212121212212122122222322323233323333333323333323303330330030300000100010010010001000101010101111" << endl;
// Construct the Freeman chain
Contour4 theContour( ss );
// Segmentation
Decomposition4 theDecomposition( theContour.begin(),theContour.end(),DSS4() );
// Draw the domain and the contour
Point p1( 0, 0 );
Point p2( 31, 31 );
Domain domain( p1, p2 );
Board2D aBoard;
aBoard << SetMode( domain.className(), "Grid" )
<< domain
<< SetMode( "PointVector", "Grid" );
// Draw each segment
string styleName = "";
for ( Decomposition4::SegmentComputerIterator
it = theDecomposition.begin(),
itEnd = theDecomposition.end();
it != itEnd; ++it )
{
aBoard << SetMode( "ArithmeticalDSS", "Points" )
<< it->primitive();
aBoard << SetMode( "ArithmeticalDSS", "BoundingBox" )
<< CustomStyle( "ArithmeticalDSS/BoundingBox",
new CustomPenColor( Color::Blue ) )
<< it->primitive();
}
aBoard.saveSVG("greedy-dss-decomposition.svg");
aBoard.saveEPS("greedy-dss-decomposition.eps");
#ifdef WITH_CAIRO
aBoard.saveCairo("greedy-dss-decomposition.png");
#endif
return 0;
}
// //