DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
volBreadthFirstTraversal.cpp
1 
14 
15 
16 #include <iostream>
17 #include <queue>
18 #include <QImageReader>
19 #include <QtGui/qapplication.h>
20 #include "DGtal/kernel/sets/SetPredicate.h"
21 #include "DGtal/io/readers/VolReader.h"
22 #include "DGtal/io/viewers/Viewer3D.h"
23 #include "DGtal/io/DrawWithDisplay3DModifier.h"
24 #include "DGtal/io/Color.h"
25 #include "DGtal/io/colormaps/HueShadeColorMap.h"
26 #include "DGtal/images/ImageSelector.h"
27 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
28 #include "DGtal/shapes/Shapes.h"
29 #include "DGtal/helpers/StdDefs.h"
30 #include "DGtal/topology/DigitalSurface.h"
31 #include "DGtal/topology/LightImplicitDigitalSurface.h"
33 
35 
36 using namespace std;
37 using namespace DGtal;
38 using namespace Z3i;
39 
41 
42 void usage( int, char** argv )
43 {
44  std::cerr << "Usage: " << argv[ 0 ] << " <fileName.vol> <minT> <maxT>" << std::endl;
45  std::cerr << "\t - displays the boundary of the shape stored in vol file <fileName.vol>." << std::endl;
46  std::cerr << "\t - voxel v belongs to the shape iff its value I(v) follows minT <= I(v) <= maxT." << std::endl;
47 }
48 
49 int main( int argc, char** argv )
50 {
51  if ( argc < 4 )
52  {
53  usage( argc, argv );
54  return 1;
55  }
56  std::string inputFilename = argv[ 1 ];
57  unsigned int minThreshold = atoi( argv[ 2 ] );
58  unsigned int maxThreshold = atoi( argv[ 3 ] );
59 
61  trace.beginBlock( "Reading vol file into an image." );
63  Image image = VolReader<Image>::importVol(inputFilename);
64  DigitalSet set3d (image.domain());
65  SetPredicate<DigitalSet> set3dPredicate( set3d );
66  SetFromImage<DigitalSet>::append<Image>(set3d, image,
67  minThreshold, maxThreshold);
68  trace.endBlock();
70 
71 
73  trace.beginBlock( "Construct the Khalimsky space from the image domain." );
74  KSpace ks;
75  bool space_ok = ks.init( image.domain().lowerBound(),
76  image.domain().upperBound(), true );
77  if (!space_ok)
78  {
79  trace.error() << "Error in the Khamisky space construction."<<std::endl;
80  return 2;
81  }
82  trace.endBlock();
84 
86  typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
87  MySurfelAdjacency surfAdj( true ); // interior in all directions.
89 
91  trace.beginBlock( "Set up digital surface." );
95  SCell bel = Surfaces<KSpace>::findABel( ks, set3dPredicate, 100000 );
96  MyDigitalSurfaceContainer* ptrSurfContainer =
97  new MyDigitalSurfaceContainer( ks, set3dPredicate, surfAdj, bel );
98  MyDigitalSurface digSurf( ptrSurfContainer ); // acquired
99  trace.endBlock();
101 
103  trace.beginBlock( "Extracting boundary by tracking from an initial bel." );
104  typedef BreadthFirstVisitor<MyDigitalSurface> MyBreadthFirstVisitor;
105  typedef MyBreadthFirstVisitor::Node MyNode;
106  typedef MyBreadthFirstVisitor::Size MySize;
107  MyBreadthFirstVisitor visitor( digSurf, bel );
108  unsigned long nbSurfels = 0;
109  MyNode node;
110  while ( ! visitor.finished() )
111  {
112  node = visitor.current();
113  ++nbSurfels;
114  visitor.expand();
115  }
116  MySize maxDist = node.second;
117  trace.endBlock();
119 
121  trace.beginBlock( "Displaying surface in Viewer3D." );
122  QApplication application(argc,argv);
123  Viewer3D viewer;
124  viewer.show();
125  HueShadeColorMap<MySize,1> hueShade( 0, maxDist );
126  MyBreadthFirstVisitor visitor2( digSurf, bel );
127  viewer << CustomColors3D( Color::Black, Color::White )
128  << ks.unsigns( bel );
129  visitor2.expand();
130  while ( ! visitor2.finished() )
131  {
132  node = visitor2.current();
133  Color c = hueShade( node.second );
134  viewer << CustomColors3D( Color::Red, c )
135  << ks.unsigns( node.first );
136  visitor2.expand();
137  }
138  viewer << Viewer3D::updateDisplay;
139  trace.info() << "nb surfels = " << nbSurfels << std::endl;
140  trace.endBlock();
141  return application.exec();
143 }
144