DGtal  1.5.beta
dvcm-3d.cpp
1 
50 #include <iostream>
51 
52 #include "DGtal/base/Common.h"
53 #include "DGtal/helpers/StdDefs.h"
54 #include "DGtal/kernel/BasicPointPredicates.h"
55 #include "DGtal/math/linalg/EigenDecomposition.h"
56 #include "DGtal/topology/helpers/Surfaces.h"
57 #include "DGtal/topology/DigitalSurface.h"
58 #include "DGtal/topology/ImplicitDigitalSurface.h"
59 #include "DGtal/images/ImageSelector.h"
60 #include "DGtal/images/IntervalForegroundPredicate.h"
61 #include "DGtal/geometry/volumes/distance/ExactPredicateLpSeparableMetric.h"
62 #include "DGtal/geometry/surfaces/estimation/VoronoiCovarianceMeasureOnDigitalSurface.h"
63 #include "DGtal/io/colormaps/GradientColorMap.h"
64 #include "DGtal/io/viewers/Viewer3D.h"
65 #include "DGtal/io/readers/GenericReader.h"
66 #include "ConfigExamples.h"
67 
68 
70 
71 using namespace std;
72 using namespace DGtal;
73 
75 int main( int argc, char** argv )
76 {
77  QApplication application(argc,argv);
78 
79  typedef Z3i::Space Space;
80  typedef Z3i::KSpace KSpace;
81  typedef Z3i::Point Point;
82  typedef Z3i::RealPoint RealPoint;
85  typedef KSpace::Surfel Surfel;
86  typedef KSpace::Cell Cell;
87 
89  typedef functors::IntervalForegroundPredicate<Image> ThresholdedImage;
90  typedef ImplicitDigitalSurface< KSpace, ThresholdedImage > DigitalSurfaceContainer;
91 
93  typedef ExactPredicateLpSeparableMetric<Space, 2> Metric; // L2-metric type
94  typedef functors::HatPointFunction<Point,double> KernelFunction; // chi function type
95  typedef VoronoiCovarianceMeasureOnDigitalSurface< DigitalSurfaceContainer, Metric,
96  KernelFunction > VCMOnSurface;
97  typedef VCMOnSurface::Surfel2Normals::const_iterator S2NConstIterator;
99 
100  string inputFilename = examplesPath + "samples/Al.100.vol";
101  trace.info() << "File = " << inputFilename << std::endl;
102  int thresholdMin = 0;
103  trace.info() << "Min image thres. = " << thresholdMin << std::endl;
104  int thresholdMax = 1;
105  trace.info() << "Max image thres. = " << thresholdMax << std::endl;
106  const double R = 20;
107  trace.info() << "Big radius R = " << R << std::endl;
108  const double r = 3;
109  trace.info() << "Small radius r = " << r << std::endl;
110  const double trivial_r = 3;
111  trace.info() << "Trivial radius t = " << trivial_r << std::endl; // for orienting the directions given by the tensor.
112  const double T = 0.1;
113  trace.info() << "Feature thres. T = " << T << std::endl; // threshold for displaying features as red.
114 
115  const double size = 1.0; // size of displayed normals.
116 
117  KSpace ks;
118  // Reads the volume
119  trace.beginBlock( "Loading image into memory and build digital surface." );
120  Image image = GenericReader<Image>::import(inputFilename );
121  ThresholdedImage thresholdedImage( image, thresholdMin, thresholdMax );
122  trace.endBlock();
123  trace.beginBlock( "Extracting boundary by scanning the space. " );
124  ks.init( image.domain().lowerBound(),
125  image.domain().upperBound(), true );
126  SurfelAdjacency<KSpace::dimension> surfAdj( true ); // interior in all directions.
127  Surfel bel = Surfaces<KSpace>::findABel( ks, thresholdedImage, 10000 );
128  DigitalSurfaceContainer* container =
129  new DigitalSurfaceContainer( ks, thresholdedImage, surfAdj, bel, false );
130  DigitalSurface< DigitalSurfaceContainer > surface( container ); //acquired
131  trace.info() << "Digital surface has " << surface.size() << " surfels." << std::endl;
132  trace.endBlock();
133 
135  Surfel2PointEmbedding embType = Pointels; // Could be Pointels|InnerSpel|OuterSpel;
136  Metric l2; // Euclidean L2 metric
137  KernelFunction chi( 1.0, r ); // hat function with support of radius r
138  VCMOnSurface vcm_surface( surface, embType, R, r,
139  chi, trivial_r, l2, true /* verbose */ );
141 
142  trace.beginBlock( "Displaying VCM" );
143  Viewer3D<> viewer( ks );
144  Cell dummy;
145  viewer.setWindowTitle("3D VCM viewer");
146  viewer << SetMode3D( dummy.className(), "Basic" );
147  viewer.show();
148 
149  GradientColorMap<double> grad( 0, T );
150  grad.addColor( Color( 128, 128, 255 ) );
151  grad.addColor( Color( 255, 255, 255 ) );
152  grad.addColor( Color( 255, 255, 0 ) );
153  grad.addColor( Color( 255, 0, 0 ) );
154  RealVector lambda; // eigenvalues of chi-vcm
155  for ( S2NConstIterator it = vcm_surface.mapSurfel2Normals().begin(),
156  itE = vcm_surface.mapSurfel2Normals().end(); it != itE; ++it )
157  {
158  Surfel s = it->first;
159  Point kp = ks.sKCoords( s );
160  RealPoint rp( 0.5 * (double) kp[ 0 ], 0.5 * (double) kp[ 1 ], 0.5 * (double) kp[ 2 ] );
161  RealVector n = it->second.vcmNormal;
162  vcm_surface.getChiVCMEigenvalues( lambda, s );
163  double ratio = lambda[ 1 ] / ( lambda[ 0 ] + lambda[ 1 ] + lambda[ 2 ] );
164  viewer.setFillColor( grad( ratio > T ? T : ratio ) );
165  viewer << ks.unsigns( s );
166  n *= size;
167  viewer.setLineColor( Color::Black );
168  viewer.addLine( rp + n, rp - n, 0.1 );
169  }
170  viewer << Viewer3D<>::updateDisplay;
171  application.exec();
172  trace.endBlock();
173  return 0;
174 }
175 // //
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
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 template may be used to (linearly) convert scalar values in a given range into a colo...
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
Aim: A model of CDigitalSurfaceContainer which defines the digital surface as the boundary of an impl...
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
Cell unsigns(const SCell &p) const
Creates an unsigned cell from a signed one.
const Point & sKCoords(const SCell &c) const
Return its Khalimsky coordinates.
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:79
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
Aim: This class specializes the Voronoi covariance measure for digital surfaces. It adds notably the ...
Aim: Define a simple Foreground predicate thresholding image values between two constant values (the ...
CountedPtr< SH3::DigitalSurface > surface
Space::RealVector RealVector
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Surfel2PointEmbedding
Possible embeddings for surfel as point(s)
Aim: Provide a mechanism to load with the bestloader according to an image (2D or 3D) filename (by pa...
Aim: Automatically defines an adequate image type according to the hints given by the user.
Definition: ImageSelector.h:70
std::string className() const
Return the style name used for drawing this object.
Modifier class in a Display3D stream. Useful to choose your own mode for a given class....
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
int main(int argc, char **argv)
MyPointD Point
Definition: testClone2.cpp:383
KSpace::Cell Cell
ImageContainerBySTLVector< Domain, Value > Image
HyperRectDomain< Space > Domain
PointVector< 3, double > RealPoint