31#include "DGtal/base/Common.h"
38#include "DGtal/io/readers/GenericReader.h"
39#include "DGtal/images/ImageSelector.h"
40#include "DGtal/images/imagesSetsUtils/SetFromImage.h"
41#include "DGtal/images/IntervalForegroundPredicate.h"
42#include "DGtal/topology/SurfelAdjacency.h"
43#include "DGtal/topology/helpers/Surfaces.h"
44#include "DGtal/topology/LightImplicitDigitalSurface.h"
45#include <DGtal/topology/SetOfSurfels.h>
47#include "DGtal/images/ImageHelper.h"
48#include "DGtal/topology/DigitalSurface.h"
49#include "DGtal/graph/DepthFirstVisitor.h"
50#include "DGtal/graph/GraphVisitorRange.h"
53#include "DGtal/geometry/surfaces/estimation/IIGeometricFunctors.h"
54#include "DGtal/geometry/surfaces/estimation/IntegralInvariantVolumeEstimator.h"
55#include "DGtal/geometry/surfaces/estimation/IntegralInvariantCovarianceEstimator.h"
58#include "DGtal/io/viewers/PolyscopeViewer.h"
59#include "DGtal/io/colormaps/GradientColorMap.h"
62using namespace functors;
139const Color AXIS_COLOR_RED( 200, 20, 20, 255 );
140const Color AXIS_COLOR_GREEN( 20, 200, 20, 255 );
141const Color AXIS_COLOR_BLUE( 20, 20, 200, 255 );
142const double AXIS_LINESIZE = 0.05;
150void missingParam( std::string param )
152 trace.error() <<
" Parameter: " << param <<
" is required.";
153 trace.info() << std::endl;
157int main(
int argc,
char** argv )
161 std::string inputFileName;
162 double re_convolution_kernel;
163 double noiseLevel {0.5};
164 unsigned int threshold {8};
165 int minImageThreshold {0};
166 int maxImageThreshold {255};
167 std::string mode {
"mean"};
168 std::string export_dat_filename;
169 bool exportOnly {
false};
170 std::vector< double> vectScale;
171 bool normalization {
false};
173 app.description(
"Visualisation of 3d curvature from .vol file using curvature from Integral Invarian\nBasic usage:\n \t3dCurvatureViewerNoise file.vol --radius 5 --mode mean \n Below are the different available modes: \n\t - \"mean\" for the mean curvature \n \t - \"mean\" for the mean curvature\n\t - \"gaussian\" for the Gaussian curvature\n\t - \"k1\" for the first principal curvature\n\t - \"k2\" for the second principal curvature\n\t - \"prindir1\" for the first principal curvature direction\n\t - \"prindir2\" for the second principal curvature direction\n\t - \"normal\" for the normal vector");
177 app.add_option(
"-i,--input,1", inputFileName,
"vol file (.vol, .longvol .p3d, .pgm3d and if DGTAL_WITH_ITK is selected: dicom, dcm, mha, mhd). For longvol, dicom, dcm, mha or mhd formats, the input values are linearly scaled between 0 and 255." )
179 ->check(CLI::ExistingFile);
181 app.add_option(
"--radius,-r", re_convolution_kernel,
"Kernel radius for IntegralInvariant" )
183 app.add_option(
"--threshold,-t", threshold,
"Min size of SCell boundary of an object");
184 app.add_option(
"--minImageThreshold,-l",minImageThreshold,
"set the minimal image threshold to define the image object (object defined by the voxel with intensity belonging to ]minImageThreshold, maxImageThreshold ] ).");
185 app.add_option(
"--maxImageThreshold,-u",maxImageThreshold,
"set the maximal image threshold to define the image object (object defined by the voxel with intensity belonging to ]minImageThreshold, maxImageThreshold] ).");
186 app.add_option(
"--mode,-m", mode,
"type of output : mean, gaussian, k1, k2, prindir1, prindir2 or normal(default mean)")
187 -> check(CLI::IsMember({
"mean",
"gaussian",
"k1",
"k2",
"prindir1",
"prindir2",
"normal" }));
188 app.add_option(
"--exportDAT,-d",export_dat_filename,
"Export resulting curvature (for mean, gaussian, k1 or k2 mode) in a simple data file each line representing a surfel." );
189 app.add_flag(
"--exportOnly", exportOnly,
"Used to only export the result without the 3d Visualisation (usefull for scripts).");
191 app.add_option(
"--imageScale,-s", vectScale,
"scaleX, scaleY, scaleZ: re sample the source image according with a grid of size 1.0/scale (usefull to compute curvature on image defined on anisotropic grid). Set by default to 1.0 for the three axis.")
194 app.add_option(
"--normalization,-n",normalization,
"When exporting to OBJ, performs a normalization so that the geometry fits in [-1/2,1/2]^3") ;
196 app.get_formatter()->column_width(40);
197 CLI11_PARSE(app, argc, argv);
200 bool neededArgsGiven=
true;
203 bool enable_visu = !exportOnly;
204 bool enable_dat = export_dat_filename !=
"";
206 if( !enable_visu && !enable_dat )
208 trace.error() <<
"You should specify what you want to export with --export and/or --exportDat, or remove --exportOnly." << std::endl;
209 neededArgsGiven =
false;
214 std::vector< double > aGridSizeReSample;
215 if( vectScale.size() == 3)
217 aGridSizeReSample.push_back(1.0/vectScale.at(0));
218 aGridSizeReSample.push_back(1.0/vectScale.at(1));
219 aGridSizeReSample.push_back(1.0/vectScale.at(2));
223 aGridSizeReSample.push_back(1.0);
224 aGridSizeReSample.push_back(1.0);
225 aGridSizeReSample.push_back(1.0);
231 typedef Z3i::Space::RealPoint RealPoint;
232 typedef Z3i::Point Point;
233 typedef ImageSelector< Z3i::Domain, int>::Type Image;
234 typedef DGtal::functors::BasicDomainSubSampler< HyperRectDomain<SpaceND<3, int> >,
235 DGtal::int32_t,
double > ReSampler;
236 typedef DGtal::ConstImageAdapter<Image, Image::Domain, ReSampler,
237 Image::Value, DGtal::functors::Identity > SamplerImageAdapter;
238 typedef IntervalForegroundPredicate< SamplerImageAdapter > ImagePredicate;
239 typedef BinaryPointPredicate<DomainPredicate<Image::Domain>, ImagePredicate, AndBoolFct2 > Predicate;
240 typedef Z3i::KSpace KSpace;
241 typedef KSpace::SCell SCell;
242 typedef KSpace::Cell Cell;
244 trace.beginBlock(
"Loading the file");
245 Image image = GenericReader<Image>::import( inputFileName );
247 PointVector<3,int> shiftVector3D( 0 ,0, 0 );
248 DGtal::functors::BasicDomainSubSampler< HyperRectDomain< SpaceND< 3, int > >,
249 DGtal::int32_t,
double > reSampler(image.domain(),
250 aGridSizeReSample, shiftVector3D);
251 const functors::Identity identityFunctor{};
252 SamplerImageAdapter sampledImage ( image, reSampler.getSubSampledDomain(), reSampler, identityFunctor );
253 ImagePredicate predicateIMG = ImagePredicate( sampledImage, minImageThreshold, maxImageThreshold );
254 DomainPredicate<Z3i::Domain> domainPredicate( sampledImage.domain() );
256 Predicate predicate(domainPredicate, predicateIMG, andF );
259 Z3i::Domain domain = sampledImage.domain();
261 bool space_ok = K.init( domain.lowerBound()-Z3i::Domain::Point::diagonal(),
262 domain.upperBound()+Z3i::Domain::Point::diagonal(),
true );
265 trace.error() <<
"Error in the Khalimsky space construction."<<std::endl;
268 CanonicSCellEmbedder< KSpace > embedder( K );
269 SurfelAdjacency< Z3i::KSpace::dimension > Sadj(
true );
275 typedef KSpace::SurfelSet SurfelSet;
276 typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
277 typedef DigitalSurface< MySetOfSurfels > MyDigitalSurface;
279 trace.beginBlock(
"Extracting surfaces");
280 std::vector< std::vector<SCell > > vectConnectedSCell;
281 Surfaces<KSpace>::extractAllConnectedSCell(vectConnectedSCell,K, Sadj, predicate,
false);
282 std::ofstream outDat;
285 trace.info() <<
"Exporting curvature as dat file: "<< export_dat_filename <<std::endl;
286 outDat.open( export_dat_filename.c_str() );
287 outDat <<
"# data exported from 3dCurvatureViewer implementing the II curvature estimator (Coeurjolly, D.; Lachaud, J.O; Levallois, J., (2013). Integral based Curvature"
288 <<
" Estimators in Digital Geometry. DGCI 2013.) " << std::endl;
289 outDat <<
"# format: surfel coordinates (in Khalimsky space) curvature: "<< mode << std::endl;
292 trace.info()<<
"Number of components= "<<vectConnectedSCell.size()<<std::endl;
295 if( vectConnectedSCell.size() == 0 )
297 trace.error()<<
"No surface component exists. Please check the vol file threshold parameter.";
298 trace.info()<<std::endl;
302 typedef PolyscopeViewer<Z3i::Space, Z3i::KSpace> Viewer;
304 viewer.allowReuseList =
true;
306 for(
unsigned int i = 0; i<vectConnectedSCell.size(); ++i )
308 if( vectConnectedSCell[i].size() <= threshold )
313 MySetOfSurfels aSet(K, Sadj);
315 for( std::vector<SCell>::const_iterator it = vectConnectedSCell.at(i).begin();
316 it != vectConnectedSCell.at(i).end();
319 aSet.surfelSet().insert( *it);
322 MyDigitalSurface digSurf( aSet );
325 typedef DepthFirstVisitor<MyDigitalSurface> Visitor;
326 typedef GraphVisitorRange< Visitor > VisitorRange;
327 typedef VisitorRange::ConstIterator SurfelConstIterator;
328 VisitorRange range(
new Visitor( digSurf, *digSurf.begin() ) );
329 SurfelConstIterator abegin = range.begin();
330 SurfelConstIterator aend = range.end();
332 VisitorRange range2(
new Visitor( digSurf, *digSurf.begin() ) );
333 SurfelConstIterator abegin2 = range2.begin();
335 trace.beginBlock(
"Curvature computation on a component");
336 if( ( mode.compare(
"gaussian") == 0 ) || ( mode.compare(
"mean") == 0 )
337 || ( mode.compare(
"k1") == 0 ) || ( mode.compare(
"k2") == 0 ))
339 typedef double Quantity;
340 std::vector< Quantity > results;
341 std::back_insert_iterator< std::vector< Quantity > > resultsIterator( results );
342 if ( mode.compare(
"mean") == 0 )
344 typedef functors::IIMeanCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
345 typedef IntegralInvariantVolumeEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
347 MyIICurvatureFunctor functor;
348 functor.init( h, re_convolution_kernel );
350 MyIIEstimator estimator( functor );
351 estimator.attach( K, predicate );
352 estimator.setParams( re_convolution_kernel/h );
353 estimator.init( h, abegin, aend );
355 estimator.eval( abegin, aend, resultsIterator );
357 else if ( mode.compare(
"gaussian") == 0 )
359 typedef functors::IIGaussianCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
360 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
362 MyIICurvatureFunctor functor;
363 functor.init( h, re_convolution_kernel );
365 MyIIEstimator estimator( functor ); estimator.attach( K,
366 predicate ); estimator.setParams( re_convolution_kernel/h );
367 estimator.init( h, abegin, aend );
369 estimator.eval( abegin, aend, resultsIterator );
371 else if ( mode.compare(
"k1") == 0 )
373 typedef functors::IIFirstPrincipalCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
374 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
376 MyIICurvatureFunctor functor;
377 functor.init( h, re_convolution_kernel );
379 MyIIEstimator estimator( functor );
380 estimator.attach( K, predicate );
381 estimator.setParams( re_convolution_kernel/h );
382 estimator.init( h, abegin, aend );
384 estimator.eval( abegin, aend, resultsIterator );
386 else if ( mode.compare(
"k2") == 0 )
388 typedef functors::IISecondPrincipalCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
389 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
391 MyIICurvatureFunctor functor;
392 functor.init( h, re_convolution_kernel );
394 MyIIEstimator estimator( functor );
395 estimator.attach( K, predicate );
396 estimator.setParams( re_convolution_kernel/h );
397 estimator.init( h, abegin, aend );
399 estimator.eval( abegin, aend, resultsIterator );
405 trace.beginBlock(
"Visualisation");
406 Quantity min = results[ 0 ];
407 Quantity max = results[ 0 ];
408 for (
unsigned int i = 1; i < results.size(); ++i )
410 if ( results[ i ] < min )
414 else if ( results[ i ] > max )
419 trace.info() <<
"Max value= "<<max<<
" min value= "<<min<<std::endl;
420 ASSERT( min <= max );
421 for (
unsigned int i = 0; i < results.size(); ++i )
425 viewer << WithQuantity(*abegin2,
"curvature", results[i]);
430 Point kCoords = K.uKCoords(K.unsigns(*abegin2));
431 outDat << kCoords[0] <<
" " << kCoords[1] <<
" " << kCoords[2] <<
" " << results[i] << std::endl;
439 typedef Z3i::Space::RealVector Quantity;
440 std::vector< Quantity > results;
441 std::back_insert_iterator< std::vector< Quantity > > resultsIterator( results );
443 if( mode.compare(
"prindir1") == 0 )
445 typedef functors::IIFirstPrincipalDirectionFunctor<Z3i::Space> MyIICurvatureFunctor;
446 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
448 MyIICurvatureFunctor functor;
449 functor.init( h, re_convolution_kernel );
451 MyIIEstimator estimator( functor );
452 estimator.attach( K, predicate );
453 estimator.setParams( re_convolution_kernel/h );
454 estimator.init( h, abegin, aend );
456 estimator.eval( abegin, aend, resultsIterator );
458 else if( mode.compare(
"prindir2") == 0 )
460 typedef functors::IISecondPrincipalDirectionFunctor<Z3i::Space> MyIICurvatureFunctor;
461 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
463 MyIICurvatureFunctor functor;
464 functor.init( h, re_convolution_kernel );
466 MyIIEstimator estimator( functor );
467 estimator.attach( K, predicate );
468 estimator.setParams( re_convolution_kernel/h );
469 estimator.init( h, abegin, aend );
471 estimator.eval( abegin, aend, resultsIterator );
474 if( mode.compare(
"normal") == 0 )
476 typedef functors::IINormalDirectionFunctor<Z3i::Space> MyIICurvatureFunctor;
477 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
479 MyIICurvatureFunctor functor;
480 functor.init( h, re_convolution_kernel );
482 MyIIEstimator estimator( functor );
483 estimator.attach( K, predicate );
484 estimator.setParams( re_convolution_kernel/h );
485 estimator.init( h, abegin, aend );
487 estimator.eval( abegin, aend, resultsIterator );
493 for (
unsigned int i = 0; i < results.size(); ++i )
495 DGtal::Dimension kDim = K.sOrthDir( *abegin2 );
496 SCell outer = K.sIndirectIncident( *abegin2, kDim);
497 if ( predicate(Z3i::Point(embedder(outer),functors::Round<>()) ))
499 outer = K.sDirectIncident( *abegin2, kDim);
502 Cell unsignedSurfel = K.uCell( K.sKCoords(*abegin2) );
506 viewer << DGtal::Color(255,255,255,255)
512 Point kCoords = K.uKCoords(K.unsigns(*abegin2));
513 outDat << kCoords[0] <<
" " << kCoords[1] <<
" " << kCoords[2] <<
" "
514 << results[i][0] <<
" " << results[i][1] <<
" " << results[i][2]
518 RealPoint center = embedder( outer );
522 if( mode.compare(
"prindir1") == 0 )
524 viewer.drawColor( AXIS_COLOR_BLUE );
526 else if( mode.compare(
"prindir2") == 0 )
528 viewer.drawColor( AXIS_COLOR_RED );
530 else if( mode.compare(
"normal") == 0 )
532 viewer.drawColor( AXIS_COLOR_GREEN );
537 center[0] - 0.5 * results[i][0],
538 center[1] - 0.5 * results[i][1],
539 center[2] - 0.5 * results[i][2]
542 center[0] + 0.5 * results[i][0],
543 center[1] + 0.5 * results[i][1],
544 center[2] + 0.5 * results[i][2]