DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
exampleFMM3D.cpp
1 
33 #include <iostream>
34 #include <QtGui/qapplication.h>
35 #include "DGtal/io/viewers/Viewer3D.h"
36 #include "DGtal/io/DrawWithDisplay3DModifier.h"
37 #include "DGtal/io/Color.h"
38 #include "DGtal/base/Common.h"
39 #include "DGtal/helpers/StdDefs.h"
40 #include "DGtal/shapes/Shapes.h"
41 #include "DGtal/io/colormaps/HueShadeColorMap.h"
42 #include "DGtal/io/colormaps/GradientColorMap.h"
43 
44 using namespace std;
45 using namespace DGtal;
46 using namespace Z3i;
47 
48 
50 #include <boost/program_options/options_description.hpp>
51 #include <boost/program_options/parsers.hpp>
52 #include <boost/program_options/variables_map.hpp>
53 
54 namespace po = boost::program_options;
55 
56 //image
57 #include "DGtal/io/readers/VolReader.h"
58 #include "DGtal/images/ImageContainerBySTLVector.h"
59 #include "DGtal/images/ImageContainerBySTLMap.h"
60 #include "DGtal/images/ConstImageAdapter.h"
61 #include "DGtal/base/BasicFunctors.h"
62 #include "DGtal/kernel/BasicPointPredicates.h"
63 
64 //frontier
65 #include "DGtal/topology/SurfelAdjacency.h"
66 #include "DGtal/topology/helpers/FrontierPredicate.h"
67 #include "DGtal/topology/LightExplicitDigitalSurface.h"
68 
69 // FMM
70 #include "DGtal/geometry/volumes/distance/FMM.h"
71 
72 // Standard services - public :
73 int main( int argc, char** argv )
74 {
75 
77  // parse command line
78  po::options_description general_opt("Allowed options are");
79  general_opt.add_options()
80  ("help,h", "display this message")
81  ("inputImage,i", po::value<string>(), "Grey-level image (vol format)" )
82  ("threshold,t", po::value<int>()->default_value(1), "Set defined by the voxels whose value is below t" )
83  ("width,w", po::value<double>()->default_value(3.0), "Band width where DT is computed" );
84 
85  po::variables_map vm;
86  po::store(po::parse_command_line(argc, argv, general_opt), vm);
87  po::notify(vm);
88  if(vm.count("help")||argc<=1)
89  {
90  trace.info()<< "3d incremental DT transform" << std::endl
91  << "Basic usage: "<<std::endl
92  << argv[0] << " [other options] -i <vol file> -t <threshold> " << std::endl
93  << general_opt << "\n";
94  return 0;
95  }
96 
97  //Parse options
98  //threshold
99  int t = vm["threshold"].as<int>();
100  //width
101  double maximalWidth = vm["width"].as<double>();
102 
103 
105  // image binarization and surface extraction
106  //types
109  typedef FrontierPredicate<KSpace, BinaryImage> SurfelPredicate;
111  typedef Frontier::SurfelConstIterator SurfelIterator;
112 
113  if (!(vm.count("inputImage")))
114  {
115  trace.emphase() << "you should use option -i" << std::endl;
116  return 0;
117  }
118 
119  //reading image
120  string imageFileName = vm["inputImage"].as<std::string>();
121  trace.emphase() << imageFileName <<std::endl;
122  DGtal::trace.beginBlock("image reading...");
123  LabelImage labelImage = VolReader<LabelImage>::importVol( imageFileName);
125 
126  DGtal::trace.beginBlock("binarization...");
127 
128  DefaultFunctor g;
129  Thresholder<LabelImage::Value> thresholder( t );
130  BinaryImage binaryImage(labelImage, labelImage.domain(), g, thresholder);
131  trace.info() << "threshold: "
132  << t
133  << std::endl;
134 
135  //space and starting bel
136  KSpace ks;
137  Domain d = labelImage.domain();
138  ks.init( d.lowerBound(), d.upperBound(), true );
139  KSpace::SCell bel;
140 
141  try {
142  //getting a bel
143  bel = Surfaces<KSpace>::findABel( ks, binaryImage, d.size() );
144 
145  trace.info() << "starting bel: "
146  << bel
147  << std::endl;
148 
149  } catch (DGtal::InputException i) {
150  trace.emphase() << "starting bel not found" << std::endl;
151  return 0;
152  }
153 
154  //implicit frontier
155  SCellToIncidentPoints<KSpace> functor( ks );
156  std::pair<Point,Point> bpair = functor(bel);
157  SurfelPredicate surfelPredicate( ks, binaryImage,
158  binaryImage( bpair.second ),
159  binaryImage( bpair.first ) );
160  Frontier frontier( ks, surfelPredicate,
161  SurfelAdjacency<KSpace::dimension>( true ), bel );
162 
164 
167  typedef ImageContainerBySTLMap<Domain,double> DistanceImage;
168  typedef DigitalSetFromMap<DistanceImage> PointSet;
172 
173  DGtal::trace.beginBlock("FMM...");
174 
177  DistanceImage image( d, 0.0 );
178  PointSet points(image);
179  FMM::initFromBelsRange( ks, frontier.begin(), frontier.end(), image, points, 0.5 );
181 
184  FMM fmm( image, points, d.predicate(), d.size(), maximalWidth );
185  fmm.compute();
186  trace.info() << fmm << std::endl;
188 
190 
192  //visualisation
193  QApplication application(argc,argv);
194  Viewer3D viewer;
195  viewer.show();
196 
197  //
198  GradientColorMap<double> colorMap( 0, 2*maximalWidth );
199  colorMap.addColor( Color( 255, 0, 0 ) );
200  colorMap.addColor( Color( 0, 250, 0 ) );
201  for (DistanceImage::const_iterator it = image.begin(), itEnd = image.end();
202  it != itEnd; ++it)
203  {
204  Point p = it->first;
205  viewer << CustomColors3D( colorMap(it->second), colorMap(it->second) ) ;
206  viewer << p;
207  }
208 
209  Vector extent = d.extent();
210  double a = -extent[0]/2, b = extent[1]/2;
211  double c = 0, mu = (a+b);
212  trace.info() << "clipping plane ("
213  << a << ", " << b << ", " << c << ", " << mu << ")"
214  << std::endl;
215  viewer << ClippingPlane(a,b,c,mu);
216 
217  viewer << Viewer3D::updateDisplay;
218  return application.exec();
219 }
220 // //