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"
38 #include <boost/pending/disjoint_sets.hpp>
43 using namespace DGtal;
91 template <
typename Rank,
typename Parent,
typename Image>
92 void CCCounter(Rank& r, Parent& p,
const Image& elements,
const unsigned int connectivity)
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)
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);
108 for (
typename Image::Domain::ConstIterator e = elements.domain().begin();
109 e != elements.domain().end(); ++e )
111 if ( elements.domain().isInside( *e + decx ) &&
112 ( elements( *e ) == elements( *e + decx ) ) )
113 dsets.union_set( *e, *e + decx );
115 if ( elements.domain().isInside( *e + decy ) &&
116 ( elements( *e ) == elements( *e + decy ) ) )
117 dsets.union_set( *e, *e + decy );
119 if ( elements.domain().isInside( *e + decz ) &&
120 ( elements( *e ) == elements( *e + decz ) ) )
121 dsets.union_set( *e, *e + decz );
123 if ( connectivity > 6 )
125 if ( elements.domain().isInside( *e + decx + decy ) &&
126 ( elements( *e ) == elements( *e + decx + decy ) ) )
127 dsets.union_set( *e, *e + decx + decy );
129 if ( elements.domain().isInside( *e + decx + decz ) &&
130 ( elements( *e ) == elements( *e + decx + decz ) ) )
131 dsets.union_set( *e, *e + decx + decz );
133 if ( elements.domain().isInside( *e + decy + decz ) &&
134 ( elements( *e ) == elements( *e + decy + decz ) ) )
135 dsets.union_set( *e, *e + decy + decz );
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 );
144 std::cout <<
"Number of disjoint "<<connectivity<<
"-components = "
145 <<dsets.count_sets(elements.domain().begin(),
146 elements.domain().end())
151 int main(
int argc,
char** argv )
156 std::string inputFileName;
157 unsigned int connectivity {6};
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)." )
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}));
166 app.get_formatter()->column_width(40);
167 CLI11_PARSE(app, argc, argv);
171 typedef ImageSelector<Domain, unsigned char>::Type Image;
172 Image image = VolReader<Image>::importVol( inputFileName );
174 trace.info() <<
"Image loaded: "<<image<< std::endl;
176 typedef std::map<Point,std::size_t> rank_t;
177 typedef std::map<Point,Point> parent_t;
181 boost::associative_property_map<rank_t> rank_pmap(rank_map);
182 boost::associative_property_map<parent_t> parent_pmap(parent_map);
184 CCCounter(rank_pmap, parent_pmap, image, connectivity);