52 #include <mln/value/int_u8.hh>
53 #include <mln/value/int_u16.hh>
55 #include <mln/core/routine/duplicate.hh>
57 #include <mln/core/image/image2d.hh>
59 #include <mln/morpho/line_gradient.hh>
60 #include <mln/morpho/closing/area_on_vertices.hh>
61 #include <mln/labeling/regional_minima.hh>
62 #include <mln/morpho/watershed/flooding.hh>
64 #include <mln/io/pgm/load.hh>
65 #include <mln/io/pgm/save.hh>
67 int main(
int argc,
char* argv[])
72 <<
"usage: " << argv[0] <<
" max_nregions input.pgm output.pgm"
74 std::exit(EXIT_FAILURE);
90 if (!input.is_valid())
92 std::cerr <<
"Error reading input " << argv[2] << std::endl;
117 typedef lg_ima_t::nbh_t nbh_t;
121 unsigned max_area = input.nsites();
122 unsigned nregions = mln_max(
unsigned);
123 unsigned max_nregions = atoi(argv[1]);
126 while (area < max_area && nregions > max_nregions)
129 std::cerr <<
"area = " <<
area <<
" \t"
130 <<
"nregions = " << nregions << std::endl;
144 typedef int_u16 wst_val_t;
149 std::cout <<
"nbasins = " << nbasins << std::endl;
155 const wst_val_t wshed_label = 0;
165 mln_piter_(wshed_t)
p(wshed.domain());
167 if (wshed(
p) != wshed_label)
169 wshed2d(
p.first()) = wshed(
p);
170 wshed2d(
p.second()) = wshed(
p);
174 std::vector<mln_sum_(val_t)>
sum(nbasins + 1, 0);
175 std::vector<unsigned>
nsites(nbasins + 1, 0);
176 mln_piter_(orig_ima_t) q(input.domain());
179 sum[wshed2d(q)] += input(q);
182 std::vector<float> average(nbasins + 1);
183 for (
unsigned i = 1; i <= nbasins; ++i)
187 orig_ima_t output(input.domain());
189 output(q) =
convert::to<mln_value_(orig_ima_t)>(average[wshed2d(q)]);
191 std::cout << "
area = " <<
area << " \t"
192 << "nregions = " << nregions << std::endl;
193 io::pgm::
save(output, argv[3]);