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/volumes/KanungoNoise.h"
56#include "DGtal/geometry/surfaces/estimation/IIGeometricFunctors.h"
57#include "DGtal/geometry/surfaces/estimation/IntegralInvariantVolumeEstimator.h"
58#include "DGtal/geometry/surfaces/estimation/IntegralInvariantCovarianceEstimator.h"
61#include "DGtal/io/viewers/PolyscopeViewer.h"
64using namespace functors;
142const Color AXIS_COLOR_RED( 200, 20, 20, 255 );
143const Color AXIS_COLOR_GREEN( 20, 200, 20, 255 );
144const Color AXIS_COLOR_BLUE( 20, 20, 200, 255 );
145const double AXIS_LINESIZE = 0.05;
154void missingParam( std::string param )
156 trace.error() <<
" Parameter: " << param <<
" is required.";
157 trace.info() << std::endl;
161int main(
int argc,
char** argv )
165 std::string inputFileName;
166 double re_convolution_kernel;
167 double noiseLevel {0.5};
168 unsigned int threshold {8};
169 int minImageThreshold {0};
170 int maxImageThreshold {255};
171 std::string mode {
"mean"};
172 std::string export_dat_filename;
173 bool exportOnly {
false};
174 std::vector< double> vectScale;
175 bool normalization {
false};
178 app.description(
"Visualisation of 3d curvature from .vol file using curvature from Integral Invariant\nBasic usage:\n \t3dCurvatureViewerNoise file.vol --radius 5 --mode mean --noise 0.5 \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");
182 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." )
184 ->check(CLI::ExistingFile);
186 app.add_option(
"--radius,-r", re_convolution_kernel,
"Kernel radius for IntegralInvariant" )
188 app.add_option(
"--noise,-k", noiseLevel,
"Level of Kanungo noise ]0;1[");
189 app.add_option(
"--threshold,-t", threshold,
"Min size of SCell boundary of an object");
190 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 ] ).");
191 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] ).");
192 app.add_option(
"--mode,-m", mode,
"type of output : mean, gaussian, k1, k2, prindir1, prindir2 or normal(default mean)")
193 -> check(CLI::IsMember({
"mean",
"gaussian",
"k1",
"k2",
"prindir1",
"prindir2",
"normal" }));
194 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." );
195 app.add_flag(
"--exportOnly", exportOnly,
"Used to only export the result without the 3d Visualisation (usefull for scripts).");
197 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.")
200 app.add_option(
"--normalization,-n",normalization,
"When exporting to OBJ, performs a normalization so that the geometry fits in [-1/2,1/2]^3") ;
202 app.get_formatter()->column_width(40);
203 CLI11_PARSE(app, argc, argv);
206 bool neededArgsGiven=
true;
208 if( noiseLevel < 0.0 || noiseLevel > 1.0 )
210 trace.error() <<
"The noise level should be in the interval: ]0, 1["<< std::endl;
213 bool enable_visu = !exportOnly;
214 bool enable_dat = export_dat_filename !=
"";
216 if( !enable_visu && !enable_dat )
218 trace.error() <<
"You should specify what you want to export with --export and/or --exportDat, or remove --exportOnly." << std::endl;
219 neededArgsGiven =
false;
224 std::vector< double > aGridSizeReSample;
225 if( vectScale.size() == 3)
227 aGridSizeReSample.push_back(1.0/vectScale.at(0));
228 aGridSizeReSample.push_back(1.0/vectScale.at(1));
229 aGridSizeReSample.push_back(1.0/vectScale.at(2));
234 aGridSizeReSample.push_back(1.0);
235 aGridSizeReSample.push_back(1.0);
236 aGridSizeReSample.push_back(1.0);
242 typedef Z3i::Space::RealPoint RealPoint;
243 typedef Z3i::Point Point;
244 typedef ImageSelector< Z3i::Domain, int>::Type Image;
245 typedef DGtal::functors::BasicDomainSubSampler< HyperRectDomain<SpaceND<3, int> >,
246 DGtal::int32_t,
double > ReSampler;
247 typedef DGtal::ConstImageAdapter<Image, Image::Domain, ReSampler,
248 Image::Value, DGtal::functors::Identity > SamplerImageAdapter;
249 typedef IntervalForegroundPredicate< SamplerImageAdapter > ImagePredicate;
250 typedef KanungoNoise< ImagePredicate, Z3i::Domain > KanungoPredicate;
251 typedef BinaryPointPredicate<DomainPredicate<Image::Domain>, KanungoPredicate, AndBoolFct2 > Predicate;
252 typedef Z3i::KSpace KSpace;
253 typedef KSpace::SCell SCell;
254 typedef KSpace::Cell Cell;
256 trace.beginBlock(
"Loading the file");
258 Image image = GenericReader<Image>::import( inputFileName );
260 PointVector<3,int> shiftVector3D( 0 ,0, 0 );
261 DGtal::functors::BasicDomainSubSampler< HyperRectDomain< SpaceND< 3, int > >,
262 DGtal::int32_t,
double > reSampler(image.domain(),
263 aGridSizeReSample, shiftVector3D);
264 const functors::Identity identityFunctor{};
265 SamplerImageAdapter sampledImage ( image, reSampler.getSubSampledDomain(), reSampler, identityFunctor );
266 ImagePredicate predicateIMG = ImagePredicate( sampledImage, minImageThreshold, maxImageThreshold );
267 KanungoPredicate noisifiedPredicateIMG( predicateIMG, sampledImage.domain(), noiseLevel );
268 DomainPredicate<Z3i::Domain> domainPredicate( sampledImage.domain() );
270 Predicate predicate(domainPredicate, noisifiedPredicateIMG, andF );
273 Z3i::Domain domain = sampledImage.domain();
275 bool space_ok = K.init( domain.lowerBound()-Z3i::Domain::Point::diagonal(),
276 domain.upperBound()+Z3i::Domain::Point::diagonal(),
true );
279 trace.error() <<
"Error in the Khalimsky space construction."<<std::endl;
282 CanonicSCellEmbedder< KSpace > embedder( K );
283 SurfelAdjacency< Z3i::KSpace::dimension > Sadj(
true );
289 typedef KSpace::SurfelSet SurfelSet;
290 typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
291 typedef DigitalSurface< MySetOfSurfels > MyDigitalSurface;
295 trace.beginBlock(
"Extracting surfaces");
296 std::vector< std::vector<SCell > > vectConnectedSCell;
297 Surfaces<KSpace>::extractAllConnectedSCell(vectConnectedSCell,K, Sadj, predicate,
false);
298 std::ofstream outDat;
301 trace.info() <<
"Exporting curvature as dat file: "<< export_dat_filename <<std::endl;
302 outDat.open( export_dat_filename.c_str() );
303 outDat <<
"# data exported from 3dCurvatureViewer implementing the II curvature estimator (Coeurjolly, D.; Lachaud, J.O; Levallois, J., (2013). Integral based Curvature"
304 <<
" Estimators in Digital Geometry. DGCI 2013.) " << std::endl;
305 outDat <<
"# format: surfel coordinates (in Khalimsky space) curvature: "<< mode << std::endl;
308 trace.info()<<
"Number of components= "<<vectConnectedSCell.size()<<std::endl;
311 if( vectConnectedSCell.size() == 0 )
313 trace.error()<<
"No surface component exists. Please check the vol file threshold parameter.";
314 trace.info()<<std::endl;
318 typedef PolyscopeViewer<Z3i::Space, Z3i::KSpace> Viewer;
320 viewer.allowReuseList =
true;
323 unsigned int max_size = 0;
324 for(
unsigned int ii = 0; ii<vectConnectedSCell.size(); ++ii )
326 if( vectConnectedSCell[ii].size() <= threshold )
330 if( vectConnectedSCell[ii].size() > max_size )
332 max_size = vectConnectedSCell[ii].size();
337 MySetOfSurfels aSet(K, Sadj);
339 for( std::vector<SCell>::const_iterator it = vectConnectedSCell.at(i).begin();
340 it != vectConnectedSCell.at(i).end();
343 aSet.surfelSet().insert( *it);
346 MyDigitalSurface digSurf( aSet );
349 typedef DepthFirstVisitor<MyDigitalSurface> Visitor;
350 typedef GraphVisitorRange< Visitor > VisitorRange;
351 typedef VisitorRange::ConstIterator SurfelConstIterator;
352 VisitorRange range(
new Visitor( digSurf, *digSurf.begin() ) );
353 SurfelConstIterator abegin = range.begin();
354 SurfelConstIterator aend = range.end();
356 VisitorRange range2(
new Visitor( digSurf, *digSurf.begin() ) );
357 SurfelConstIterator abegin2 = range2.begin();
359 trace.beginBlock(
"Curvature computation on a component");
360 if( ( mode.compare(
"gaussian") == 0 ) || ( mode.compare(
"mean") == 0 )
361 || ( mode.compare(
"k1") == 0 ) || ( mode.compare(
"k2") == 0 ))
363 typedef double Quantity;
364 std::vector< Quantity > results;
365 std::back_insert_iterator< std::vector< Quantity > > resultsIterator( results );
366 if ( mode.compare(
"mean") == 0 )
368 typedef functors::IIMeanCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
369 typedef IntegralInvariantVolumeEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
371 MyIICurvatureFunctor functor;
372 functor.init( h, re_convolution_kernel );
374 MyIIEstimator estimator( functor );
375 estimator.attach( K, predicate );
376 estimator.setParams( re_convolution_kernel/h );
377 estimator.init( h, abegin, aend );
379 estimator.eval( abegin, aend, resultsIterator );
381 else if ( mode.compare(
"gaussian") == 0 )
383 typedef functors::IIGaussianCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
384 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
386 MyIICurvatureFunctor functor;
387 functor.init( h, re_convolution_kernel );
389 MyIIEstimator estimator( functor ); estimator.attach( K,
390 predicate ); estimator.setParams( re_convolution_kernel/h );
391 estimator.init( h, abegin, aend );
393 estimator.eval( abegin, aend, resultsIterator );
395 else if ( mode.compare(
"k1") == 0 )
397 typedef functors::IIFirstPrincipalCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
398 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
400 MyIICurvatureFunctor functor;
401 functor.init( h, re_convolution_kernel );
403 MyIIEstimator estimator( functor );
404 estimator.attach( K, predicate );
405 estimator.setParams( re_convolution_kernel/h );
406 estimator.init( h, abegin, aend );
408 estimator.eval( abegin, aend, resultsIterator );
410 else if ( mode.compare(
"k2") == 0 )
412 typedef functors::IISecondPrincipalCurvature3DFunctor<Z3i::Space> MyIICurvatureFunctor;
413 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
415 MyIICurvatureFunctor functor;
416 functor.init( h, re_convolution_kernel );
418 MyIIEstimator estimator( functor );
419 estimator.attach( K, predicate );
420 estimator.setParams( re_convolution_kernel/h );
421 estimator.init( h, abegin, aend );
423 estimator.eval( abegin, aend, resultsIterator );
429 trace.beginBlock(
"Visualisation");
430 Quantity min = results[ 0 ];
431 Quantity max = results[ 0 ];
432 for (
unsigned int i = 1; i < results.size(); ++i )
434 if ( results[ i ] < min )
438 else if ( results[ i ] > max )
443 trace.info() <<
"Max value= "<<max<<
" min value= "<<min<<std::endl;
444 ASSERT( min <= max );
446 for (
unsigned int i = 0; i < results.size(); ++i )
450 viewer << WithQuantity(*abegin2,
"curvature", results[i]);
455 Point kCoords = K.uKCoords(K.unsigns(*abegin2));
456 outDat << kCoords[0] <<
" " << kCoords[1] <<
" " << kCoords[2] <<
" " << results[i] << std::endl;
464 typedef Z3i::Space::RealVector Quantity;
465 std::vector< Quantity > results;
466 std::back_insert_iterator< std::vector< Quantity > > resultsIterator( results );
468 if( mode.compare(
"prindir1") == 0 )
470 typedef functors::IIFirstPrincipalDirectionFunctor<Z3i::Space> MyIICurvatureFunctor;
471 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
473 MyIICurvatureFunctor functor;
474 functor.init( h, re_convolution_kernel );
476 MyIIEstimator estimator( functor );
477 estimator.attach( K, predicate );
478 estimator.setParams( re_convolution_kernel/h );
479 estimator.init( h, abegin, aend );
481 estimator.eval( abegin, aend, resultsIterator );
483 else if( mode.compare(
"prindir2") == 0 )
485 typedef functors::IISecondPrincipalDirectionFunctor<Z3i::Space> MyIICurvatureFunctor;
486 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
488 MyIICurvatureFunctor functor;
489 functor.init( h, re_convolution_kernel );
491 MyIIEstimator estimator( functor );
492 estimator.attach( K, predicate );
493 estimator.setParams( re_convolution_kernel/h );
494 estimator.init( h, abegin, aend );
496 estimator.eval( abegin, aend, resultsIterator );
497 }
else if( mode.compare(
"normal") == 0 )
499 typedef functors::IINormalDirectionFunctor<Z3i::Space> MyIICurvatureFunctor;
500 typedef IntegralInvariantCovarianceEstimator<Z3i::KSpace, Predicate, MyIICurvatureFunctor> MyIIEstimator;
502 MyIICurvatureFunctor functor;
503 functor.init( h, re_convolution_kernel );
505 MyIIEstimator estimator( functor );
506 estimator.attach( K, predicate );
507 estimator.setParams( re_convolution_kernel/h );
508 estimator.init( h, abegin, aend );
510 estimator.eval( abegin, aend, resultsIterator );
514 for (
unsigned int i = 0; i < results.size(); ++i )
516 DGtal::Dimension kDim = K.sOrthDir( *abegin2 );
517 SCell outer = K.sIndirectIncident( *abegin2, kDim);
518 if ( predicate(Z3i::Point(embedder(outer), functors::Round<>()) ))
520 outer = K.sDirectIncident( *abegin2, kDim);
523 Cell unsignedSurfel = K.uCell( K.sKCoords(*abegin2) );
527 viewer << DGtal::Color(255,255,255,255)
533 Point kCoords = K.uKCoords(K.unsigns(*abegin2));
534 outDat << kCoords[0] <<
" " << kCoords[1] <<
" " << kCoords[2] <<
" "
535 << results[i][0] <<
" " << results[i][1] <<
" " << results[i][2]
539 RealPoint center = embedder( outer );
543 if( mode.compare(
"prindir1") == 0 )
545 viewer.drawColor( AXIS_COLOR_BLUE );
547 else if( mode.compare(
"prindir2") == 0 )
549 viewer.drawColor( AXIS_COLOR_RED );
551 else if( mode.compare(
"normal") == 0 )
553 viewer.drawColor( AXIS_COLOR_GREEN );
558 center[0] - 0.5 * results[i][0],
559 center[1] - 0.5 * results[i][1],
560 center[2] - 0.5 * results[i][2]
563 center[0] + 0.5 * results[i][0],
564 center[1] + 0.5 * results[i][1],
565 center[2] + 0.5 * results[i][2]