DGtal  0.9.4beta
greedy-plane-segmentation.cpp
1 
48 #include <iostream>
50 #include <vector>
51 #include <set>
52 #include <map>
53 #include <iostream>
54 
55 #include "DGtal/base/Common.h"
56 #include "DGtal/io/readers/VolReader.h"
57 
58 #include "DGtal/io/Display3D.h"
59 #include "DGtal/io/viewers/Viewer3D.h"
60 #include "DGtal/io/DrawWithDisplay3DModifier.h"
61 #include "DGtal/images/ImageSelector.h"
62 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
63 #include "DGtal/topology/DigitalSurface.h"
64 #include "DGtal/topology/DigitalSetBoundary.h"
65 #include "DGtal/graph/BreadthFirstVisitor.h"
66 #include "DGtal/geometry/surfaces/COBANaivePlaneComputer.h"
67 #include "DGtal/helpers/StdDefs.h"
68 #include "ConfigExamples.h"
69 
71 
72 using namespace std;
73 using namespace DGtal;
74 
75 
77 using namespace Z3i;
78 typedef DGtal::int64_t InternalInteger;
80 // We choose the DigitalSetBoundary surface container in order to
81 // segment connected or unconnected surfaces.
84 typedef MyDigitalSurface::ConstIterator ConstIterator;
85 typedef MyDigitalSurface::Vertex Vertex;
86 typedef MyDigitalSurface::SurfelSet SurfelSet;
87 typedef SurfelSet::iterator SurfelSetIterator;
93 struct SegmentedPlane {
94  NaivePlaneComputer plane;
95  Color color;
96 };
98 
100 
101 int main( int argc, char** argv )
102 {
103 
105  trace.info() << "Segments the surface at given threshold within given volume into digital planes of rational width num/den." << std::endl;
106  // Setting default options: ----------------------------------------------
107  // input file used:
108  string inputFilename = examplesPath + "samples/Al.100.vol" ;
109  trace.info() << "input file used " << inputFilename << std::endl;
110  // parameter threshold
111  unsigned int threshold = 0;
112  trace.info() << "the value that defines the isosurface in the image (an integer between 0 and 255)= " << threshold<< std::endl;
113  // parameter widthNum
114  unsigned int widthNum = 1;
115  trace.info() << "the numerator of the rational width (a non-null integer) =" << widthNum<< std::endl;
116  // parameter widthDen
117  unsigned int widthDen = 1;
118  trace.info() << "the denominator of the rational width (a non-null integer)= " << widthDen<< std::endl;
119 
121 
123  QApplication application(argc,argv);
125  Image image = VolReader<Image>::importVol(inputFilename);
126  DigitalSet set3d (image.domain());
127  SetFromImage<DigitalSet>::append<Image>(set3d, image, threshold,255);
129 
131  trace.beginBlock( "Set up digital surface." );
132  // We initializes the cellular grid space used for defining the
133  // digital surface.
134  KSpace ks;
135  bool ok = ks.init( set3d.domain().lowerBound(),
136  set3d.domain().upperBound(), true );
137  if ( ! ok ) std::cerr << "[KSpace.init] Failed." << std::endl;
138  SurfelAdjacency<KSpace::dimension> surfAdj( true ); // interior in all directions.
139  MyDigitalSurfaceContainer* ptrSurfContainer =
140  new MyDigitalSurfaceContainer( ks, set3d, surfAdj );
141  MyDigitalSurface digSurf( ptrSurfContainer ); // acquired
142  trace.endBlock();
144 
146  trace.beginBlock( "Segment into planes." );
147  std::set<Vertex> processedVertices;
148  std::vector<SegmentedPlane*> segmentedPlanes;
149  std::map<Vertex,SegmentedPlane*> v2plane;
150  Point p;
151  Dimension axis;
152  unsigned int j = 0;
153  unsigned int nb = digSurf.size();
154  for ( ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
155  {
156  if ( ( (++j) % 50 == 0 ) || ( j == nb ) ) trace.progressBar( j, nb );
157  Vertex v = *it;
158  if ( processedVertices.find( v ) != processedVertices.end() ) // already in set
159  continue; // process to next vertex
160 
161  SegmentedPlane* ptrSegment = new SegmentedPlane;
162  segmentedPlanes.push_back( ptrSegment ); // to delete them afterwards.
163  axis = ks.sOrthDir( v );
164  ptrSegment->plane.init( axis, 500, widthNum, widthDen );
165  // The visitor takes care of all the breadth-first traversal.
166  Visitor visitor( digSurf, v );
167  while ( ! visitor.finished() )
168  {
169  Visitor::Node node = visitor.current();
170  v = node.first;
171  if ( processedVertices.find( v ) == processedVertices.end() )
172  { // Vertex is not in processedVertices
173  axis = ks.sOrthDir( v );
174  p = ks.sCoords( ks.sDirectIncident( v, axis ) );
175  bool isExtended = ptrSegment->plane.extend( p );
176  if ( isExtended )
177  { // surfel is in plane.
178  processedVertices.insert( v );
179  v2plane[ v ] = ptrSegment;
180  visitor.expand();
181  }
182  else // surfel is not in plane and should not be used in the visit.
183  visitor.ignore();
184  }
185  else // surfel is already in some plane.
186  visitor.ignore();
187  }
188  // Assign random color for each plane.
189  ptrSegment->color = Color( rand() % 256, rand() % 256, rand() % 256, 255 );
190  }
191  trace.endBlock();
193 
195  Viewer3D<> viewer( ks );
196  viewer.show();
197  for ( std::map<Vertex,SegmentedPlane*>::const_iterator
198  it = v2plane.begin(), itE = v2plane.end();
199  it != itE; ++it )
200  {
201  viewer << CustomColors3D( it->second->color, it->second->color );
202  viewer << ks.unsigns( it->first );
203  }
204  viewer << Viewer3D<>::updateDisplay;
206 
208  for ( std::vector<SegmentedPlane*>::iterator
209  it = segmentedPlanes.begin(), itE = segmentedPlanes.end();
210  it != itE; ++it )
211  delete *it;
212  segmentedPlanes.clear();
213  v2plane.clear();
215 
216  return application.exec();
217 }
void beginBlock(const std::string &keyword="")
void progressBar(const double currentValue, const double maximalValue)
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:137
DigitalSurfaceContainer::SurfelConstIterator ConstIterator
DGtal::uint32_t Dimension
Definition: Common.h:120
KSpace::SurfelSet SurfelSet
STL namespace.
double endBlock()
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of a given...
SCell sDirectIncident(const SCell &p, Dimension k) const
Dimension sOrthDir(const SCell &s) const
bool init(const Point &lower, const Point &upper, bool isClosed)
Aim: implements methods to read a "Vol" file format.
Definition: VolReader.h:89
Surfel Vertex
Defines the type for a vertex.
Point sCoords(const SCell &c) const
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
Aim: Define utilities to convert a digital set into an image.
Definition: SetFromImage.h:63
static Dimension size()
std::pair< Vertex, Data > Node
FIXME.
std::ostream & info()
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
const Domain & domain() const
Definition: Image.h:192
Cell unsigns(const SCell &p) const
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...