File failed to load: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/config/TeX-MML-AM_CHTML/MathJax.js
DGtal 2.0.0
greedy-plane-segmentation.cpp
Go to the documentation of this file.
1
16
29
30
49#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/viewers/PolyscopeViewer.h"
59#include "DGtal/images/ImageSelector.h"
60#include "DGtal/images/imagesSetsUtils/SetFromImage.h"
61#include "DGtal/topology/DigitalSurface.h"
62#include "DGtal/topology/DigitalSetBoundary.h"
63#include "DGtal/graph/BreadthFirstVisitor.h"
64#include "DGtal/geometry/surfaces/COBANaivePlaneComputer.h"
65#include "DGtal/helpers/StdDefs.h"
66#include "ConfigExamples.h"
67
69
70using namespace std;
71using namespace DGtal;
72
73
75using namespace Z3i;
78// We choose the DigitalSetBoundary surface container in order to
79// segment connected or unconnected surfaces.
85typedef SurfelSet::iterator SurfelSetIterator;
91struct SegmentedPlane {
94};
96
98
99int main( int argc, char** argv )
100{
101
103 trace.info() << "Segments the surface at given threshold within given volume into digital planes of rational width num/den." << std::endl;
104 // Setting default options: ----------------------------------------------
105 // input file used:
106 string inputFilename = examplesPath + "samples/Al.100.vol" ;
107 trace.info() << "input file used " << inputFilename << std::endl;
108 // parameter threshold
109 unsigned int threshold = 0;
110 trace.info() << "the value that defines the isosurface in the image (an integer between 0 and 255)= " << threshold<< std::endl;
111 // parameter widthNum
112 unsigned int widthNum = 1;
113 trace.info() << "the numerator of the rational width (a non-null integer) =" << widthNum<< std::endl;
114 // parameter widthDen
115 unsigned int widthDen = 1;
116 trace.info() << "the denominator of the rational width (a non-null integer)= " << widthDen<< std::endl;
117
119
122 Image image = VolReader<Image>::importVol(inputFilename);
123 DigitalSet set3d (image.domain());
124 SetFromImage<DigitalSet>::append<Image>(set3d, image, threshold,255);
126
128 trace.beginBlock( "Set up digital surface." );
129 // We initializes the cellular grid space used for defining the
130 // digital surface.
131 KSpace ks;
132 bool ok = ks.init( set3d.domain().lowerBound(),
133 set3d.domain().upperBound(), true );
134 if ( ! ok ) std::cerr << "[KSpace.init] Failed." << std::endl;
135 SurfelAdjacency<KSpace::dimension> surfAdj( true ); // interior in all directions.
136 MyDigitalSurfaceContainer* ptrSurfContainer =
137 new MyDigitalSurfaceContainer( ks, set3d, surfAdj );
138 MyDigitalSurface digSurf( ptrSurfContainer ); // acquired
139 trace.endBlock();
141
143 trace.beginBlock( "Segment into planes." );
144 std::set<Vertex> processedVertices;
145 std::vector<SegmentedPlane*> segmentedPlanes;
146 std::map<Vertex,SegmentedPlane*> v2plane;
147 Point p;
148 Dimension axis;
149 unsigned int j = 0;
150 unsigned int nb = digSurf.size();
151 for ( ConstIterator it = digSurf.begin(), itE= digSurf.end(); it != itE; ++it )
152 {
153 if ( ( (++j) % 50 == 0 ) || ( j == nb ) ) trace.progressBar( j, nb );
154 Vertex v = *it;
155 if ( processedVertices.find( v ) != processedVertices.end() ) // already in set
156 continue; // process to next vertex
157
158 SegmentedPlane* ptrSegment = new SegmentedPlane;
159 segmentedPlanes.push_back( ptrSegment ); // to delete them afterwards.
160 axis = ks.sOrthDir( v );
161 ptrSegment->plane.init( axis, 500, widthNum, widthDen );
162 // The visitor takes care of all the breadth-first traversal.
163 Visitor visitor( digSurf, v );
164 while ( ! visitor.finished() )
165 {
166 Visitor::Node node = visitor.current();
167 v = node.first;
168 if ( processedVertices.find( v ) == processedVertices.end() )
169 { // Vertex is not in processedVertices
170 axis = ks.sOrthDir( v );
171 p = ks.sCoords( ks.sDirectIncident( v, axis ) );
172 bool isExtended = ptrSegment->plane.extend( p );
173 if ( isExtended )
174 { // surfel is in plane.
175 processedVertices.insert( v );
176 v2plane[ v ] = ptrSegment;
177 visitor.expand();
178 }
179 else // surfel is not in plane and should not be used in the visit.
180 visitor.ignore();
181 }
182 else // surfel is already in some plane.
183 visitor.ignore();
184 }
185 // Assign random color for each plane.
186 ptrSegment->color = Color( rand() % 256, rand() % 256, rand() % 256, 255 );
187 }
188 trace.endBlock();
190
192 PolyscopeViewer<> viewer( ks );
193 for ( std::map<Vertex,SegmentedPlane*>::const_iterator
194 it = v2plane.begin(), itE = v2plane.end();
195 it != itE; ++it )
196 {
197 viewer << it->second->color;
198 viewer << ks.unsigns( it->first );
199 }
201
203 for ( std::vector<SegmentedPlane*>::iterator
204 it = segmentedPlanes.begin(), itE = segmentedPlanes.end();
205 it != itE; ++it )
206 delete *it;
207 segmentedPlanes.clear();
208 v2plane.clear();
210
211 viewer.show();
212 return 0;
213}
214
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
const Node & current() const
Aim: A class that contains the COBA algorithm (Emilie Charrier, Lilian Buzer, DGCI2008) for recognizi...
void init(Dimension axis, InternalInteger diameter, InternalInteger widthNumerator=NumberTraits< InternalInteger >::ONE, InternalInteger widthDenominator=NumberTraits< InternalInteger >::ONE)
bool extend(const Point &p)
Structure representing an RGB triple with alpha component.
Definition Color.h:77
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of a given...
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
DigitalSurfaceContainer::SurfelConstIterator ConstIterator
ConstIterator begin() const
ConstIterator end() const
Aim: implements association bewteen points lying in a digital domain and values.
Definition Image.h:70
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Dimension sOrthDir(const SCell &s) const
Given a signed surfel [s], returns its orthogonal direction (ie, the coordinate where the surfel is c...
Cell unsigns(const SCell &p) const
Creates an unsigned cell from a signed one.
Point sCoords(const SCell &c) const
Return its digital coordinates.
SCell sDirectIncident(const SCell &p, Dimension k) const
Return the direct incident cell of [p] along [k] (the incident cell along [k])
void show() override
Starts the event loop and display of elements.
Aim: Represent adjacencies between surfel elements, telling if it follows an interior to exterior ord...
DigitalSurface< MyDigitalSurfaceContainer > MyDigitalSurface
SurfelSet::iterator SurfelSetIterator
COBANaivePlaneComputer< Z3, InternalInteger > NaivePlaneComputer
MyDigitalSurface::SurfelSet SurfelSet
BreadthFirstVisitor< MyDigitalSurface > Visitor
DigitalSetBoundary< KSpace, DigitalSet > MyDigitalSurfaceContainer
DGtal::int64_t InternalInteger
DigitalSetBoundary< KSpace, DigitalSet > MyDigitalSurfaceContainer
Z3i this namespace gathers the standard of types for 3D imagery.
KhalimskySpaceND< 3, Integer > KSpace
Definition StdDefs.h:146
DigitalSetSelector< Domain, BIG_DS+HIGH_BEL_DS >::Type DigitalSet
Definition StdDefs.h:173
DGtal is the top-level namespace which contains all DGtal functions and types.
std::int64_t int64_t
signed 94-bit integer.
Definition BasicTypes.h:73
DGtal::uint32_t Dimension
Definition Common.h:119
Trace trace
STL namespace.
ImageContainerBySTLVector< Domain, Value > Type
static void append(Set &aSet, const ForegroundPredicate &isForeground, typename Image::Domain::ConstIterator itBegin, typename Image::Domain::ConstIterator itEnd)
static ImageContainer importVol(const std::string &filename, const Functor &aFunctor=Functor())
int main()
Definition testBits.cpp:56
Image image(domain)
TriMesh::Vertex Vertex