31 #include "DGtal/base/Common.h"
34 #include <boost/program_options/options_description.hpp>
35 #include <boost/program_options/parsers.hpp>
36 #include <boost/program_options/variables_map.hpp>
39 #include "DGtal/io/readers/GenericReader.h"
40 #include "DGtal/images/ImageSelector.h"
41 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
42 #include "DGtal/images/IntervalForegroundPredicate.h"
43 #include "DGtal/topology/SurfelAdjacency.h"
44 #include "DGtal/topology/helpers/Surfaces.h"
45 #include "DGtal/topology/LightImplicitDigitalSurface.h"
46 #include <DGtal/topology/SetOfSurfels.h>
48 #include "DGtal/images/ImageHelper.h"
49 #include "DGtal/topology/DigitalSurface.h"
50 #include "DGtal/graph/DepthFirstVisitor.h"
51 #include "DGtal/graph/GraphVisitorRange.h"
54 #include "DGtal/geometry/surfaces/estimation/IIGeometricFunctors.h"
55 #include "DGtal/geometry/surfaces/estimation/IntegralInvariantVolumeEstimator.h"
56 #include "DGtal/geometry/surfaces/estimation/IntegralInvariantCovarianceEstimator.h"
59 #include "DGtal/io/boards/Board3D.h"
60 #include "DGtal/io/colormaps/GradientColorMap.h"
62 #ifdef WITH_VISU3D_QGLVIEWER
63 #include "DGtal/io/viewers/Viewer3D.h"
66 using namespace DGtal;
160 const Color AXIS_COLOR_RED( 200, 20, 20, 255 );
161 const Color AXIS_COLOR_GREEN( 20, 200, 20, 255 );
162 const Color AXIS_COLOR_BLUE( 20, 20, 200, 255 );
163 const double AXIS_LINESIZE = 0.05;
171 void missingParam( std::string param )
173 trace.error() <<
" Parameter: " << param <<
" is required.";
174 trace.info() << std::endl;
177 namespace po = boost::program_options;
179 int main(
int argc,
char** argv )
182 po::options_description general_opt(
"Allowed options are");
183 general_opt.add_options()
184 (
"help,h",
"display this message")
185 (
"input,i", po::value< std::string >(),
".vol file")
186 (
"radius,r", po::value< double >(),
"Kernel radius for IntegralInvariant" )
187 (
"threshold,t", po::value< unsigned int >()->default_value(8),
"Min size of SCell boundary of an object" )
188 (
"minImageThreshold,l", po::value< int >()->default_value(0),
"set the minimal image threshold to define the image object (object defined by the voxel with intensity belonging to ]minImageThreshold, maxImageThreshold ] )." )
189 (
"maxImageThreshold,u", po::value< int >()->default_value(255),
"set the minimal image threshold to define the image object (object defined by the voxel with intensity belonging to ]minImageThreshold, maxImageThreshold] )." )
190 (
"mode,m", po::value< std::string >()->default_value(
"mean"),
"type of output : mean, gaussian, k1, k2, prindir1, prindir2 or normal (default mean)")
191 (
"exportOBJ,o", po::value< std::string >(),
"Export the scene to specified OBJ/MTL filename (extensions added)." )
192 (
"exportDAT,d", po::value<std::string>(),
"Export resulting curvature (for mean, gaussian, k1 or k2 mode) in a simple data file each line representing a surfel. ")
193 (
"exportOnly",
"Used to only export the result without the 3d Visualisation (usefull for scripts)." )
194 (
"imageScale,s", po::value<std::vector<double> >()->multitoken(),
"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. ")
195 (
"normalization,n",
"When exporting to OBJ, performs a normalization so that the geometry fits in [-1/2,1/2]^3") ;
198 po::variables_map vm;
201 po::store( po::parse_command_line( argc, argv, general_opt ), vm );
203 catch(
const std::exception & ex )
206 trace.error() <<
" Error checking program options: " << ex.what() << std::endl;
208 bool neededArgsGiven=
true;
210 if (parseOK && !(vm.count(
"input"))){
211 missingParam(
"--input");
212 neededArgsGiven=
false;
214 if (parseOK && !(vm.count(
"radius"))){
215 missingParam(
"--radius");
216 neededArgsGiven=
false;
219 bool normalization =
false;
220 if (parseOK && vm.count(
"normalization"))
221 normalization =
true;
225 mode = vm[
"mode"].as< std::string >();
226 if ( parseOK && ( mode.compare(
"gaussian") != 0 ) && ( mode.compare(
"mean") != 0 ) &&
227 ( mode.compare(
"k1") != 0 ) && ( mode.compare(
"k2") != 0 ) &&
228 ( mode.compare(
"prindir1") != 0 ) && ( mode.compare(
"prindir2") != 0 ) && ( mode.compare(
"normal") != 0 ))
231 trace.error() <<
" The selected mode ("<<mode <<
") is not defined."<<std::endl;
234 #ifndef WITH_VISU3D_QGLVIEWER
235 bool enable_visu =
false;
237 bool enable_visu = !vm.count(
"exportOnly");
239 bool enable_obj = vm.count(
"exportOBJ");
240 bool enable_dat = vm.count(
"exportDAT");
242 if( !enable_visu && !enable_obj && !enable_dat )
244 #ifndef WITH_VISU3D_QGLVIEWER
245 trace.error() <<
"You should specify what you want to export with --export and/or --exportDat." << std::endl;
247 trace.error() <<
"You should specify what you want to export with --export and/or --exportDat, or remove --exportOnly." << std::endl;
249 neededArgsGiven =
false;
252 if(!neededArgsGiven || !parseOK || vm.count(
"help") || argc <= 1 )
254 trace.info()<<
"Visualisation of 3d curvature from .vol file using curvature from Integral Invariant" <<std::endl
255 << general_opt <<
"\n"
256 <<
"Basic usage: "<<std::endl
257 <<
"\t3dCurvatureViewer -i file.vol --radius 5 --mode mean"<<std::endl
259 <<
"Below are the different available modes: " << std::endl
260 <<
"\t - \"mean\" for the mean curvature" << std::endl
261 <<
"\t - \"gaussian\" for the Gaussian curvature" << std::endl
262 <<
"\t - \"k1\" for the first principal curvature" << std::endl
263 <<
"\t - \"k2\" for the second principal curvature" << std::endl
264 <<
"\t - \"prindir1\" for the first principal curvature direction" << std::endl
265 <<
"\t - \"prindir2\" for the second principal curvature direction" << std::endl
266 <<
"\t - \"normal\" for the normal vector" << std::endl
270 unsigned int threshold = vm[
"threshold"].as<
unsigned int >();
271 int minImageThreshold = vm[
"minImageThreshold"].as<
int >();
272 int maxImageThreshold = vm[
"maxImageThreshold"].as<
int >();
276 std::string export_obj_filename;
277 std::string export_dat_filename;
281 export_obj_filename = vm[
"exportOBJ"].as< std::string >();
282 if( export_obj_filename.find(
".obj") == std::string::npos )
284 std::ostringstream oss;
285 oss << export_obj_filename <<
".obj" << std::endl;
286 export_obj_filename = oss.str();
293 export_dat_filename = vm[
"exportDAT"].as<std::string>();
296 double re_convolution_kernel = vm[
"radius"].as<
double >();
299 std::vector< double > aGridSizeReSample;
300 if( vm.count(
"imageScale" ))
302 std::vector< double> vectScale = vm[
"imageScale"].as<std::vector<double > >();
303 if( vectScale.size() != 3 )
305 trace.error() <<
"The grid size should contains 3 elements" << std::endl;
310 aGridSizeReSample.push_back(1.0/vectScale.at(0));
311 aGridSizeReSample.push_back(1.0/vectScale.at(1));
312 aGridSizeReSample.push_back(1.0/vectScale.at(2));
317 aGridSizeReSample.push_back(1.0);
318 aGridSizeReSample.push_back(1.0);
319 aGridSizeReSample.push_back(1.0);
325 typedef Z3i::Space::RealPoint RealPoint;
326 typedef Z3i::Point Point;
327 typedef ImageSelector< Z3i::Domain, int>::Type Image;
328 typedef DGtal::functors::BasicDomainSubSampler< HyperRectDomain<SpaceND<3, int> >,
329 DGtal::int32_t,
double > ReSampler;
330 typedef DGtal::ConstImageAdapter<Image, Image::Domain, ReSampler,
331 Image::Value, DGtal::functors::Identity > SamplerImageAdapter;
332 typedef IntervalForegroundPredicate< SamplerImageAdapter > ImagePredicate;
333 typedef BinaryPointPredicate<DomainPredicate<Image::Domain>, ImagePredicate, AndBoolFct2 > Predicate;
334 typedef Z3i::KSpace KSpace;
335 typedef KSpace::SCell SCell;
336 typedef KSpace::Cell Cell;
338 trace.beginBlock(
"Loading the file");
339 std::string filename = vm[
"input"].as< std::string >();
340 Image image = GenericReader<Image>::import( filename );
342 PointVector<3,int> shiftVector3D( 0 ,0, 0 );
343 DGtal::functors::BasicDomainSubSampler< HyperRectDomain< SpaceND< 3, int > >,
344 DGtal::int32_t,
double > reSampler(image.domain(),
345 aGridSizeReSample, shiftVector3D);
346 const functors::Identity identityFunctor{};
347 SamplerImageAdapter sampledImage ( image, reSampler.getSubSampledDomain(), reSampler, identityFunctor );
348 ImagePredicate predicateIMG = ImagePredicate( sampledImage, minImageThreshold, maxImageThreshold );
349 DomainPredicate<Z3i::Domain> domainPredicate( sampledImage.domain() );
351 Predicate predicate(domainPredicate, predicateIMG, andF );
354 Z3i::Domain domain = sampledImage.domain();
356 bool space_ok = K.init( domain.lowerBound()-Z3i::Domain::Point::diagonal(),
357 domain.upperBound()+Z3i::Domain::Point::diagonal(), true );
360 trace.error() <<
"Error in the Khalimsky space construction."<<std::endl;
363 CanonicSCellEmbedder< KSpace > embedder( K );
364 SurfelAdjacency< Z3i::KSpace::dimension > Sadj(
true );
370 typedef KSpace::SurfelSet SurfelSet;
371 typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
372 typedef DigitalSurface< MySetOfSurfels > MyDigitalSurface;
374 trace.beginBlock(
"Extracting surfaces");
375 std::vector< std::vector<SCell > > vectConnectedSCell;
376 Surfaces<KSpace>::extractAllConnectedSCell(vectConnectedSCell,K, Sadj, predicate,
false);
377 std::ofstream outDat;
380 trace.info() <<
"Exporting curvature as dat file: "<< export_dat_filename <<std::endl;
381 outDat.open( export_dat_filename.c_str() );
382 outDat <<
"# data exported from 3dCurvatureViewer implementing the II curvature estimator (Coeurjolly, D.; Lachaud, J.O; Levallois, J., (2013). Integral based Curvature"
383 <<
" Estimators in Digital Geometry. DGCI 2013.) " << std::endl;
384 outDat <<
"# format: surfel coordinates (in Khalimsky space) curvature: "<< mode << std::endl;
387 trace.info()<<
"Number of components= "<<vectConnectedSCell.size()<<std::endl;
390 if( vectConnectedSCell.size() == 0 )
392 trace.error()<<
"No surface component exists. Please check the vol file threshold parameter.";
393 trace.info()<<std::endl;
397 #ifdef WITH_VISU3D_QGLVIEWER
398 QApplication application( argc, argv );
399 typedef Viewer3D<Z3i::Space, Z3i::KSpace> Viewer;
401 typedef Board3D<Z3i::Space, Z3i::KSpace> Board;
403 #ifdef WITH_VISU3D_QGLVIEWER
408 #ifdef WITH_VISU3D_QGLVIEWER
415 for(
unsigned int i = 0; i<vectConnectedSCell.size(); ++i )
417 if( vectConnectedSCell[i].size() <= threshold )
422 MySetOfSurfels aSet(K, Sadj);
424 for( std::vector<SCell>::const_iterator it = vectConnectedSCell.at(i).begin();
425 it != vectConnectedSCell.at(i).end();
428 aSet.surfelSet().insert( *it);
431 MyDigitalSurface digSurf( aSet );
434 typedef DepthFirstVisitor<MyDigitalSurface> Visitor;
435 typedef GraphVisitorRange< Visitor > VisitorRange;
436 typedef VisitorRange::ConstIterator SurfelConstIterator;
437 VisitorRange range(
new Visitor( digSurf, *digSurf.begin() ) );
438 SurfelConstIterator abegin = range.begin();
439 SurfelConstIterator aend = range.end();
441 VisitorRange range2(
new Visitor( digSurf, *digSurf.begin() ) );
442 SurfelConstIterator abegin2 = range2.begin();
444 trace.beginBlock(
"Curvature computation on a component");
445 if( ( mode.compare(
"gaussian") == 0 ) || ( mode.compare(
"mean") == 0 )
446 || ( mode.compare(
"k1") == 0 ) || ( mode.compare(
"k2") == 0 ))
448 typedef double Quantity;
449 std::vector< Quantity > results;
450 std::back_insert_iterator< std::vector< Quantity > > resultsIterator( results );
451 if ( mode.compare(
"mean") == 0 )
453 typedef functors::IIMeanCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
454 typedef IntegralInvariantVolumeEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
456 MyIICurvatureFunctor functor;
457 functor.init( h, re_convolution_kernel );
459 MyIIEstimator estimator( functor );
460 estimator.attach( K, predicate );
461 estimator.setParams( re_convolution_kernel/h );
462 estimator.init( h, abegin, aend );
464 estimator.eval( abegin, aend, resultsIterator );
466 else if ( mode.compare(
"gaussian") == 0 )
468 typedef functors::IIGaussianCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
469 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
471 MyIICurvatureFunctor functor;
472 functor.init( h, re_convolution_kernel );
474 MyIIEstimator estimator( functor ); estimator.attach( K,
475 predicate ); estimator.setParams( re_convolution_kernel/h );
476 estimator.init( h, abegin, aend );
478 estimator.eval( abegin, aend, resultsIterator );
480 else if ( mode.compare(
"k1") == 0 )
482 typedef functors::IIFirstPrincipalCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
483 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
485 MyIICurvatureFunctor functor;
486 functor.init( h, re_convolution_kernel );
488 MyIIEstimator estimator( functor );
489 estimator.attach( K, predicate );
490 estimator.setParams( re_convolution_kernel/h );
491 estimator.init( h, abegin, aend );
493 estimator.eval( abegin, aend, resultsIterator );
495 else if ( mode.compare(
"k2") == 0 )
497 typedef functors::IISecondPrincipalCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
498 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
500 MyIICurvatureFunctor functor;
501 functor.init( h, re_convolution_kernel );
503 MyIIEstimator estimator( functor );
504 estimator.attach( K, predicate );
505 estimator.setParams( re_convolution_kernel/h );
506 estimator.init( h, abegin, aend );
508 estimator.eval( abegin, aend, resultsIterator );
514 trace.beginBlock(
"Visualisation");
515 Quantity min = results[ 0 ];
516 Quantity max = results[ 0 ];
517 for (
unsigned int i = 1; i < results.size(); ++i )
519 if ( results[ i ] < min )
523 else if ( results[ i ] > max )
528 trace.info() <<
"Max value= "<<max<<
" min value= "<<min<<std::endl;
529 ASSERT( min <= max );
530 typedef GradientColorMap< Quantity > Gradient;
531 Gradient cmap_grad( min, (max==min)? max+1: max );
532 cmap_grad.addColor( Color( 50, 50, 255 ) );
533 cmap_grad.addColor( Color( 255, 0, 0 ) );
534 cmap_grad.addColor( Color( 255, 255, 10 ) );
536 #ifdef WITH_VISU3D_QGLVIEWER
539 viewer << SetMode3D((*abegin2).className(),
"Basic" );
544 board << SetMode3D((K.unsigns(*abegin2)).className(),
"Basic" );
548 for (
unsigned int i = 0; i < results.size(); ++i )
550 #ifdef WITH_VISU3D_QGLVIEWER
553 viewer << CustomColors3D( Color::Black, cmap_grad( results[ i ] ));
560 board << CustomColors3D( Color::Black, cmap_grad( results[ i ] ));
561 board << K.unsigns(*abegin2);
566 Point kCoords = K.uKCoords(K.unsigns(*abegin2));
567 outDat << kCoords[0] <<
" " << kCoords[1] <<
" " << kCoords[2] <<
" " << results[i] << std::endl;
575 typedef Z3i::Space::RealVector Quantity;
576 std::vector< Quantity > results;
577 std::back_insert_iterator< std::vector< Quantity > > resultsIterator( results );
579 if( mode.compare(
"prindir1") == 0 )
581 typedef functors::IIFirstPrincipalDirectionFunctor<Z3i::Space> MyIICurvatureFunctor;
582 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
584 MyIICurvatureFunctor functor;
585 functor.init( h, re_convolution_kernel );
587 MyIIEstimator estimator( functor );
588 estimator.attach( K, predicate );
589 estimator.setParams( re_convolution_kernel/h );
590 estimator.init( h, abegin, aend );
592 estimator.eval( abegin, aend, resultsIterator );
594 else if( mode.compare(
"prindir2") == 0 )
596 typedef functors::IISecondPrincipalDirectionFunctor<Z3i::Space> MyIICurvatureFunctor;
597 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
599 MyIICurvatureFunctor functor;
600 functor.init( h, re_convolution_kernel );
602 MyIIEstimator estimator( functor );
603 estimator.attach( K, predicate );
604 estimator.setParams( re_convolution_kernel/h );
605 estimator.init( h, abegin, aend );
607 estimator.eval( abegin, aend, resultsIterator );
610 if( mode.compare(
"normal") == 0 )
612 typedef functors::IINormalDirectionFunctor<Z3i::Space> MyIICurvatureFunctor;
613 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
615 MyIICurvatureFunctor functor;
616 functor.init( h, re_convolution_kernel );
618 MyIIEstimator estimator( functor );
619 estimator.attach( K, predicate );
620 estimator.setParams( re_convolution_kernel/h );
621 estimator.init( h, abegin, aend );
623 estimator.eval( abegin, aend, resultsIterator );
630 #ifdef WITH_VISU3D_QGLVIEWER
633 viewer << SetMode3D(K.uCell( K.sKCoords(*abegin2) ).className(),
"Basic" );
639 board << SetMode3D(K.uCell( K.sKCoords(*abegin2) ).className(),
"Basic" );
642 for (
unsigned int i = 0; i < results.size(); ++i )
644 DGtal::Dimension kDim = K.sOrthDir( *abegin2 );
645 SCell outer = K.sIndirectIncident( *abegin2, kDim);
646 if ( predicate(embedder(outer)) )
648 outer = K.sDirectIncident( *abegin2, kDim);
651 Cell unsignedSurfel = K.uCell( K.sKCoords(*abegin2) );
653 #ifdef WITH_VISU3D_QGLVIEWER
656 viewer << CustomColors3D( DGtal::Color(255,255,255,255),
657 DGtal::Color(255,255,255,255))
664 board << CustomColors3D( DGtal::Color(255,255,255,255),
665 DGtal::Color(255,255,255,255))
671 Point kCoords = K.uKCoords(K.unsigns(*abegin2));
672 outDat << kCoords[0] <<
" " << kCoords[1] <<
" " << kCoords[2] <<
" "
673 << results[i][0] <<
" " << results[i][1] <<
" " << results[i][2]
677 RealPoint center = embedder( outer );
679 #ifdef WITH_VISU3D_QGLVIEWER
682 if( mode.compare(
"prindir1") == 0 )
684 viewer.setLineColor( AXIS_COLOR_BLUE );
686 else if( mode.compare(
"prindir2") == 0 )
688 viewer.setLineColor( AXIS_COLOR_RED );
690 else if( mode.compare(
"normal") == 0 )
692 viewer.setLineColor( AXIS_COLOR_GREEN );
698 center[0] - 0.5 * results[i][0],
699 center[1] - 0.5 * results[i][1],
700 center[2] - 0.5 * results[i][2]
703 center[0] + 0.5 * results[i][0],
704 center[1] + 0.5 * results[i][1],
705 center[2] + 0.5 * results[i][2]
713 if( mode.compare(
"prindir1") == 0 )
715 board.setFillColor( AXIS_COLOR_BLUE );
717 else if( mode.compare(
"prindir2") == 0 )
719 board.setFillColor( AXIS_COLOR_RED );
721 else if( mode.compare(
"normal") == 0 )
723 board.setFillColor( AXIS_COLOR_GREEN );
728 center[0] - 0.5 * results[i][0],
729 center[1] - 0.5 * results[i][1],
730 center[2] - 0.5 * results[i][2]),
732 center[0] + 0.5 * results[i][0],
733 center[1] + 0.5 * results[i][1],
734 center[2] + 0.5 * results[i][2]),
744 #ifdef WITH_VISU3D_QGLVIEWER
747 viewer << Viewer3D<>::updateDisplay;
752 trace.info()<<
"Exporting object: " << export_obj_filename <<
" ...";
753 board.saveOBJ(export_obj_filename,normalization);
754 trace.info() <<
"[done]" << std::endl;
761 #ifdef WITH_VISU3D_QGLVIEWER
764 return application.exec();