DGtalTools  0.9.4
eulerCharacteristic.cpp
1 
28 #include <iostream>
29 #include <DGtal/base/Common.h>
30 #include <DGtal/helpers/StdDefs.h>
31 #include <DGtal/io/readers/VolReader.h>
32 #include <boost/program_options/options_description.hpp>
33 #include <boost/program_options/parsers.hpp>
34 #include <boost/program_options/variables_map.hpp>
35 #include <DGtal/images/ImageContainerBySTLVector.h>
36 #include <DGtal/images/IntervalForegroundPredicate.h>
37 
38 using namespace std;
39 using namespace DGtal;
40 using namespace Z3i;
41 
42 namespace po = boost::program_options;
43 
44 
45 
46 
92 void missingParam ( std::string param )
93 {
94  trace.error() <<" Parameter: "<<param<<" is required..";
95  trace.info() <<std::endl;
96  exit ( 1 );
97 }
98 
99 
100 int main(int argc, char**argv)
101 {
102 
103  // parse command line ----------------------------------------------
104  po::options_description general_opt ( "Allowed options are: " );
105  general_opt.add_options()
106  ( "help,h", "display this message." )
107  ( "input,i", po::value<std::string>(), "Input vol file." )
108  ("thresholdMin,m", po::value<int>()->default_value(0), "threshold min (excluded) to define binary shape" )
109  ("thresholdMax,M", po::value<int>()->default_value(255), "threshold max (included) to define binary shape" );
110  bool parseOK=true;
111  po::variables_map vm;
112 
113 
114 
115  try{
116  po::store(po::parse_command_line(argc, argv, general_opt), vm);
117  }catch(const std::exception& ex){
118  parseOK=false;
119  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
120  }
121  po::notify ( vm );
122  if (!parseOK || vm.count ( "help" ) ||argc<=1 )
123  {
124  trace.info() << "Compute the Euleur Characteristic of a vol to a 8-bit raw file. The vol file is first binarized using interval [m,M[ thresholds and the Eucler characteristic is given from the cubical complex."<<std::endl
125  << std::endl << "Basic usage: "<<std::endl
126  << "\t eulerCharacteristic --input <volFileName> -m <minlevel> -M <maxlevel> "<<std::endl
127  << general_opt << "\n";
128  return 0;
129  }
130 
131  //Parse options
132  if ( ! ( vm.count ( "input" ) ) ) missingParam ( "--input" );
133  std::string filename = vm["input"].as<std::string>();
134  int thresholdMin = vm["thresholdMin"].as<int>();
135  int thresholdMax = vm["thresholdMax"].as<int>();
136 
137  //Importing the Vol
138  trace.beginBlock("Loading the vol file");
140  MyImageC imageC = VolReader< MyImageC >::importVol ( filename );
141  trace.info()<<imageC<<std::endl;
142  trace.endBlock();
143 
144  //Constructing the cubical complex
145  trace.beginBlock("Construting the cubical complex");
146  KSpace::CellSet myCellSet;
147  KSpace ks;
148  bool space_ok = ks.init( imageC.domain().lowerBound(), imageC.domain().upperBound(), true );
149  if (!space_ok)
150  {
151  trace.error() << "Error in the Khamisky space construction."<<std::endl;
152  return 2;
153  }
154  functors::IntervalForegroundPredicate<MyImageC> interval(imageC, thresholdMin,thresholdMax);
155  for(MyImageC::Domain::ConstIterator it =imageC.domain().begin(), itend= imageC.domain().end();
156  it != itend; ++it)
157  {
158  if (interval( *it ))
159  {
160  Domain dom( 2*(*it), 2*(*it) + Point::diagonal(2));
161  for(Domain::ConstIterator itdom = dom.begin(), itdomend = dom.end(); itdom != itdomend; ++itdom)
162  myCellSet.insert( ks.uCell( *itdom) );
163  }
164  }
165  trace.info() << "Got "<< myCellSet.size()<< " cells"<<std::endl;
166  trace.endBlock();
167 
168  trace.beginBlock("Computing the characteristics");
169  std::vector<int> cells(4,0);
170 
171  for(KSpace::CellSet::const_iterator it = myCellSet.begin(), itend = myCellSet.end(); it !=itend; ++it)
172  cells[ ks.uDim(*it) ] ++;
173 
174  trace.info() << "Got "<< cells[0]<< " pointels "<<cells[1]<<" linels "<< cells[2]<<" surfels and "<<cells[3]<<" bells"<<std::endl;
175  trace.endBlock();
176 
177  trace.info() << "Volumetric Euler Characteristic = "<<cells[0] - cells[1] + cells[2] - cells[3]<<std::endl;
178 
179  return 0;
180 }
void beginBlock(const std::string &keyword="")
STL namespace.
double endBlock()
Dimension uDim(const Cell &p) const
Cell uCell(const PreCell &c) const
bool init(const Point &lower, const Point &upper, bool isClosed)
std::set< Cell > CellSet
Trace trace(traceWriterTerm)
std::ostream & info()
std::ostream & error()
typename Self::Domain Domain