DGtal  0.9.2
dvcm-3d.cpp
1 
29 #include <iostream>
31 
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/kernel/BasicPointPredicates.h"
35 #include "DGtal/math/linalg/EigenDecomposition.h"
36 #include "DGtal/topology/helpers/Surfaces.h"
37 #include "DGtal/topology/DigitalSurface.h"
38 #include "DGtal/topology/ImplicitDigitalSurface.h"
39 #include "DGtal/images/ImageSelector.h"
40 #include "DGtal/images/IntervalForegroundPredicate.h"
41 #include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
42 #include "DGtal/geometry/surfaces/estimation/VoronoiCovarianceMeasureOnDigitalSurface.h"
43 #include "DGtal/io/colormaps/GradientColorMap.h"
44 #include "DGtal/io/viewers/Viewer3D.h"
45 #include "DGtal/io/readers/GenericReader.h"
46 #include "ConfigExamples.h"
47 
48 
50 
51 using namespace std;
52 using namespace DGtal;
53 
55 int main( int argc, char** argv )
56 {
57  QApplication application(argc,argv);
58 
59  typedef Z3i::Space Space;
60  typedef Z3i::KSpace KSpace;
61  typedef Z3i::Point Point;
62  typedef Z3i::RealPoint RealPoint;
65  typedef KSpace::Surfel Surfel;
66  typedef KSpace::Cell Cell;
67 
69  typedef functors::IntervalForegroundPredicate<Image> ThresholdedImage;
70  typedef ImplicitDigitalSurface< KSpace, ThresholdedImage > DigitalSurfaceContainer;
71 
73  typedef ExactPredicateLpSeparableMetric<Space, 2> Metric; // L2-metric type
74  typedef functors::HatPointFunction<Point,double> KernelFunction; // chi function type
75  typedef VoronoiCovarianceMeasureOnDigitalSurface< DigitalSurfaceContainer, Metric,
76  KernelFunction > VCMOnSurface;
77  typedef VCMOnSurface::Surfel2Normals::const_iterator S2NConstIterator;
79 
80  string inputFilename = examplesPath + "samples/Al.100.vol";
81  trace.info() << "File = " << inputFilename << std::endl;
82  int thresholdMin = 0;
83  trace.info() << "Min image thres. = " << thresholdMin << std::endl;
84  int thresholdMax = 1;
85  trace.info() << "Max image thres. = " << thresholdMax << std::endl;
86  const double R = 20;
87  trace.info() << "Big radius R = " << R << std::endl;
88  const double r = 3;
89  trace.info() << "Small radius r = " << r << std::endl;
90  const double trivial_r = 3;
91  trace.info() << "Trivial radius t = " << trivial_r << std::endl; // for orienting the directions given by the tensor.
92  const double T = 0.1;
93  trace.info() << "Feature thres. T = " << T << std::endl; // threshold for displaying features as red.
94 
95  const double size = 1.0; // size of displayed normals.
96 
97  KSpace ks;
98  // Reads the volume
99  trace.beginBlock( "Loading image into memory and build digital surface." );
100  Image image = GenericReader<Image>::import(inputFilename );
101  ThresholdedImage thresholdedImage( image, thresholdMin, thresholdMax );
102  trace.endBlock();
103  trace.beginBlock( "Extracting boundary by scanning the space. " );
104  ks.init( image.domain().lowerBound(),
105  image.domain().upperBound(), true );
106  SurfelAdjacency<KSpace::dimension> surfAdj( true ); // interior in all directions.
107  Surfel bel = Surfaces<KSpace>::findABel( ks, thresholdedImage, 10000 );
108  DigitalSurfaceContainer* container =
109  new DigitalSurfaceContainer( ks, thresholdedImage, surfAdj, bel, false );
110  DigitalSurface< DigitalSurfaceContainer > surface( container ); //acquired
111  trace.info() << "Digital surface has " << surface.size() << " surfels." << std::endl;
112  trace.endBlock();
113 
115  Surfel2PointEmbedding embType = Pointels; // Could be Pointels|InnerSpel|OuterSpel;
116  Metric l2; // Euclidean L2 metric
117  KernelFunction chi( 1.0, r ); // hat function with support of radius r
118  VCMOnSurface vcm_surface( surface, embType, R, r,
119  chi, trivial_r, l2, true /* verbose */ );
121 
122  trace.beginBlock( "Displaying VCM" );
123  Viewer3D<> viewer( ks );
124  Cell dummy;
125  viewer.setWindowTitle("3D VCM viewer");
126  viewer << SetMode3D( dummy.className(), "Basic" );
127  viewer.show();
128 
129  GradientColorMap<double> grad( 0, T );
130  grad.addColor( Color( 128, 128, 255 ) );
131  grad.addColor( Color( 255, 255, 255 ) );
132  grad.addColor( Color( 255, 255, 0 ) );
133  grad.addColor( Color( 255, 0, 0 ) );
134  RealVector lambda; // eigenvalues of chi-vcm
135  for ( S2NConstIterator it = vcm_surface.mapSurfel2Normals().begin(),
136  itE = vcm_surface.mapSurfel2Normals().end(); it != itE; ++it )
137  {
138  Surfel s = it->first;
139  Point kp = ks.sKCoords( s );
140  RealPoint rp( 0.5 * (double) kp[ 0 ], 0.5 * (double) kp[ 1 ], 0.5 * (double) kp[ 2 ] );
141  RealVector n = it->second.vcmNormal;
142  vcm_surface.getChiVCMEigenvalues( lambda, s );
143  double ratio = lambda[ 1 ] / ( lambda[ 0 ] + lambda[ 1 ] + lambda[ 2 ] );
144  viewer.setFillColor( grad( ratio > T ? T : ratio ) );
145  viewer << ks.unsigns( s );
146  n *= size;
147  viewer.setLineColor( Color::Black );
148  viewer.addLine( rp + n, rp - n, 0.1 );
149  }
150  viewer << Viewer3D<>::updateDisplay;
151  application.exec();
152  trace.endBlock();
153  return 0;
154 }
155 // //
void beginBlock(const std::string &keyword="")
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Aim: Automatically defines an adequate image type according to the hints given by the user...
Definition: ImageSelector.h:66
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:130
Aim: SpaceND is a utility class that defines the fundamental structure of a Digital Space in ND...
Definition: SpaceND.h:95
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
KhalimskySpaceND< 2, Integer > KSpace
Definition: StdDefs.h:77
STL namespace.
double endBlock()
Aim: Represents a set of n-1-cells in a nD space, together with adjacency relation between these cell...
Aim: implements separable l_p metrics with exact predicates.
Aim: This class specializes the Voronoi covariance measure for digital surfaces. It adds notably the ...
Aim: Provide a mechanism to load with the bestloader according to an image (2D or 3D) filename (by pa...
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
Aim: Define a simple Foreground predicate thresholding image values between two constant values (the ...
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
const Domain & domain() const
Definition: Image.h:192
Space::RealPoint RealPoint
Definition: StdDefs.h:97
Space::RealVector RealVector
Definition: StdDefs.h:98
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...
Surfel2PointEmbedding
Possible embeddings for surfel as point(s)
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of an impl...
Modifier class in a Display3D stream. Useful to choose your own mode for a given class. Realizes the concept CDrawableWithDisplay3D.