33 #include <boost/program_options/options_description.hpp>
34 #include <boost/program_options/parsers.hpp>
35 #include <boost/program_options/variables_map.hpp>
36 #include <DGtal/kernel/sets/SetPredicate.h>
37 #include <DGtal/io/readers/VolReader.h>
38 #include <DGtal/images/ImageSelector.h>
39 #include <DGtal/images/SimpleThresholdForegroundPredicate.h>
40 #include <DGtal/images/ImageLinearCellEmbedder.h>
41 #include <DGtal/shapes/Shapes.h>
42 #include <DGtal/kernel/CanonicEmbedder.h>
43 #include <DGtal/helpers/StdDefs.h>
44 #include <DGtal/topology/helpers/Surfaces.h>
45 #include <DGtal/topology/DigitalSurface.h>
46 #include <DGtal/topology/SetOfSurfels.h>
47 #include <DGtal/geometry/volumes/KanungoNoise.h>
55 using namespace DGtal;
116 int main(
int argc,
char** argv )
122 std::string inputFileName;
123 std::string outputFileName {
"marching-cubes.off"};
125 double threshold {1.0};
126 unsigned int intAdjacency = 0;
128 app.description(
"Outputs the isosurface of value <threshold> of the volume <fileName.vol> as an OFF file <output.off>. The <adjacency> (0/1) allows to choose between interior (6,18) and exterior (18,6) adjacency.");
129 app.add_option(
"-i,--input,1", inputFileName,
"the volume file (.vol)." )
131 ->check(CLI::ExistingFile);
132 app.add_option(
"--threshold,-t",threshold,
"the value that defines the isosurface in the image (an integer between 0 and 255).",
true);
133 app.add_option(
"--adjacency,-a",intAdjacency,
"0: interior adjacency, 1: exterior adjacency",
true);
134 app.add_option(
"-o,--output,2", outputFileName,
"the output OFF file that represents the geometry of the isosurface",
true );
135 auto noiseOpt = app.add_option(
"--noise,-n", noise,
"Kanungo noise level in ]0,1[. Note that only the largest connected component is considered and that no specific embedder is used.");
137 app.get_formatter()->column_width(40);
138 CLI11_PARSE(app, argc, argv);
144 if (noiseOpt->count() > 0 )
151 trace.beginBlock(
"Reading vol file into an image." );
152 typedef ImageSelector < Domain, int>::Type Image;
153 Image image = VolReader<Image>::importVol(inputFileName);
155 typedef functors::SimpleThresholdForegroundPredicate<Image> ThresholdedImage;
156 ThresholdedImage thresholdedImage( image, threshold );
162 trace.beginBlock(
"Construct the Khalimsky space from the image domain." );
165 ks.init( image.domain().lowerBound(), image.domain().upperBound(),
true );
168 trace.error() <<
"Error in the Khamisky space construction."<<std::endl;
175 typedef SurfelAdjacency<KSpace::dimension> MySurfelAdjacency;
176 MySurfelAdjacency surfAdj( intAdjacency );
180 trace.beginBlock(
"Extracting boundary by scanning the space. " );
181 typedef KSpace::SurfelSet SurfelSet;
182 typedef SetOfSurfels< KSpace, SurfelSet > MySetOfSurfels;
183 typedef DigitalSurface< MySetOfSurfels > MyDigitalSurface;
184 MySetOfSurfels theSetOfSurfels( ks, surfAdj );
188 trace.info()<<
"Adding some noise."<<std::endl;
189 KanungoNoise<ThresholdedImage, Z3i::Domain> kanungo(thresholdedImage, image.domain(), noise);
190 std::vector< std::vector<SCell > > vectConnectedSCell;
191 trace.info()<<
"Extracting all connected components."<<std::endl;
192 Surfaces<KSpace>::extractAllConnectedSCell( vectConnectedSCell, ks, surfAdj,
194 if( vectConnectedSCell.size() == 0 )
196 trace.error()<<
"No surface component exists. Please check the vol file --min and --max parameters." << std::endl;
200 trace.info()<<vectConnectedSCell.size()<<
" components."<<std::endl;
202 trace.info()<<
"Extracting the largest one."<<std::endl;
203 int cc_max_size_idx = -1;
204 auto it_max = std::max_element( vectConnectedSCell.begin(), vectConnectedSCell.end(),
205 [] (std::vector<SCell >& v1, std::vector<SCell >& v2)
206 { return v1.size() < v2.size(); } );
207 cc_max_size_idx = std::distance( vectConnectedSCell.begin(), it_max );
208 theSetOfSurfels.surfelSet().insert( vectConnectedSCell[ cc_max_size_idx ].begin(),
209 vectConnectedSCell[ cc_max_size_idx ].end() );
212 Surfaces<KSpace>::sMakeBoundary(
213 theSetOfSurfels.surfelSet(), ks, thresholdedImage,
214 image.domain().lowerBound(), image.domain().upperBound() );
216 MyDigitalSurface digSurf( theSetOfSurfels );
217 trace.info() <<
"Digital surface has " << digSurf.size() <<
" surfels."
224 trace.beginBlock(
"Making OFF surface. " );
226 typedef CanonicEmbedder< Space > MyEmbedder;
228 typedef ImageLinearCellEmbedder< KSpace, Image, MyEmbedder > CellEmbedder;
229 CellEmbedder cellEmbedder;
230 MyEmbedder trivialEmbedder;
236 Image largerImage( Domain( image.domain().lowerBound() - KSpace::Vector::diagonal(2),
237 image.domain().upperBound() + KSpace::Vector::diagonal(2)));
238 for(
auto p: image.domain())
239 largerImage.setValue( p, image(p));
241 std::ofstream out( outputFileName.c_str() );
245 digSurf.exportSurfaceAs3DOFF( out );
248 trace.error()<<
"IO error while opening the output file"<<std::endl;
254 cellEmbedder.init( ks, image, trivialEmbedder,
255 ( (
double) threshold ) + 0.5 );
258 digSurf.exportEmbeddedSurfaceAs3DOFF( out, cellEmbedder );
261 trace.error()<<
"IO error while opening the output file"<<std::endl;