DGtalTools  1.5.beta
volCComponentCounter.cpp
1 
30 #include <iostream>
31 
32 #include "DGtal/base/Common.h"
33 #include "DGtal/io/readers/VolReader.h"
34 #include "DGtal/io/Color.h"
35 #include "DGtal/images/ImageSelector.h"
36 #include "DGtal/helpers/StdDefs.h"
37 
38 #include <boost/pending/disjoint_sets.hpp>
39 
40 #include "CLI11.hpp"
41 
42 using namespace std;
43 using namespace DGtal;
44 using namespace Z3i;
45 
46 
91 template <typename Rank, typename Parent, typename Image>
92 void CCCounter(Rank& r, Parent& p, const Image& elements, const unsigned int connectivity)
93 {
94 
95  boost::disjoint_sets<Rank,Parent> dsets(r, p);
96  trace.beginBlock("Initial disjoint sets construction");
97  for(typename Image::Domain::ConstIterator e = elements.domain().begin();
98  e != elements.domain().end(); ++e)
99  dsets.make_set(*e);
100  trace.endBlock();
101 
102  trace.beginBlock("Merging neighboring sets");
103  typename Image::Point decx(1,0,0);
104  typename Image::Point decy(0,1,0);
105  typename Image::Point decz(0,0,1);
106 
107  //Merging process
108  for ( typename Image::Domain::ConstIterator e = elements.domain().begin();
109  e != elements.domain().end(); ++e )
110  {
111  if ( elements.domain().isInside( *e + decx ) &&
112  ( elements( *e ) == elements( *e + decx ) ) )
113  dsets.union_set( *e, *e + decx );
114 
115  if ( elements.domain().isInside( *e + decy ) &&
116  ( elements( *e ) == elements( *e + decy ) ) )
117  dsets.union_set( *e, *e + decy );
118 
119  if ( elements.domain().isInside( *e + decz ) &&
120  ( elements( *e ) == elements( *e + decz ) ) )
121  dsets.union_set( *e, *e + decz );
122 
123  if ( connectivity > 6 )
124  {
125  if ( elements.domain().isInside( *e + decx + decy ) &&
126  ( elements( *e ) == elements( *e + decx + decy ) ) )
127  dsets.union_set( *e, *e + decx + decy );
128 
129  if ( elements.domain().isInside( *e + decx + decz ) &&
130  ( elements( *e ) == elements( *e + decx + decz ) ) )
131  dsets.union_set( *e, *e + decx + decz );
132 
133  if ( elements.domain().isInside( *e + decy + decz ) &&
134  ( elements( *e ) == elements( *e + decy + decz ) ) )
135  dsets.union_set( *e, *e + decy + decz );
136 
137  if ( connectivity == 26 )
138  if ( elements.domain().isInside( *e + decy + decz + decx ) &&
139  ( elements( *e ) == elements( *e + decy + decz + decx ) ) )
140  dsets.union_set( *e, *e + decy + decz + decx );
141  }
142  }
143  trace.endBlock();
144  std::cout << "Number of disjoint "<<connectivity<<"-components = "
145  <<dsets.count_sets(elements.domain().begin(),
146  elements.domain().end())
147  << std::endl;
148 }
149 
150 
151 int main( int argc, char** argv )
152 {
153 
154  // parse command line using CLI ----------------------------------------------
155  CLI::App app;
156  std::string inputFileName;
157  unsigned int connectivity {6};
158 
159  app.description("Count the number of connected component (same values) in a volume (Vol) file image\n\n Example : \n \t volCComponentCounter $DGtal/examples/samples/Al.100.vol\n");
160  app.add_option("-i,--input,1", inputFileName, "volume file (.vol)." )
161  ->required()
162  ->check(CLI::ExistingFile);
163  app.add_option("--connectivity,-c",connectivity,"object connectivity (6,18,26) (default: 6 ).", true)
164  -> check(CLI::IsMember({6, 18, 26}));
165 
166  app.get_formatter()->column_width(40);
167  CLI11_PARSE(app, argc, argv);
168  // END parse command line using CLI ----------------------------------------------
169 
170 
172  Image image = VolReader<Image>::importVol( inputFileName );
173 
174  trace.info() << "Image loaded: "<<image<< std::endl;
175 
176  typedef std::map<Point,std::size_t> rank_t; // => order on Element
177  typedef std::map<Point,Point> parent_t;
178  rank_t rank_map;
179  parent_t parent_map;
180 
181  boost::associative_property_map<rank_t> rank_pmap(rank_map);
182  boost::associative_property_map<parent_t> parent_pmap(parent_map);
183 
184  CCCounter(rank_pmap, parent_pmap, image, connectivity);
185 
186  return 0;
187 }
int main(int argc, char **argv)
std::vector< Value >::const_iterator ConstIterator
const Domain & domain() const
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
Trace trace(traceWriterTerm)