https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox
Index: ChangeLog
from Ugo Jardonnet <ugo.jardonnet(a)lrde.epita.fr>
N components extraction based on value iterator.
* jardonnet/n_cmpt/n_cmpt2.hh: Make use of value iterator.
* jardonnet/n_cmpt/n_cmpt2.cc: Update.
* jardonnet/n_cmpt/Makefile: Add rules.
* jardonnet/n_cmpt/n_cmpt.hh: Minor fix.
Makefile | 5 +++
n_cmpt.hh | 2 -
n_cmpt2.cc | 8 +++---
n_cmpt2.hh | 77 +++++++++++++++++++++++++++++++++++++++++--------------------
4 files changed, 61 insertions(+), 31 deletions(-)
Index: jardonnet/n_cmpt/n_cmpt2.hh
--- jardonnet/n_cmpt/n_cmpt2.hh (revision 2975)
+++ jardonnet/n_cmpt/n_cmpt2.hh (working copy)
@@ -30,6 +30,11 @@
# include <mln/level/fill.hh>
# include <mln/core/site_set/p_vaccess.hh>
+# include <mln/level/sort_psites.hh>
+# include <mln/accu/volume.hh>
+# include <mln/morpho/tree/data.hh>
+# include <mln/morpho/tree/compute_attribute_image.hh>
+# include <mln/value/int_u.hh>
namespace mln
{
@@ -43,54 +48,76 @@
# ifndef MLN_INCLUDE_ONLY
+ template <typename I>
+ inline
+ mln_psite(I)
+ find_root(I& parent,
+ const mln_psite(I)& x)
+ {
+ if (parent(x) == x)
+ return x;
+ else
+ return parent(x) = find_root(parent, parent(x));
+ }
- template <typename I, typename N, typename O>
- void n_cmpt(const Image<I>& input_,
+ template <typename I, typename N>
+ I n_cmpt(const Image<I>& input_,
const Neighborhood<N>& nbh_,
- unsigned limit,
- Image<O>& output_)
+ unsigned limit)
{
const I& input = exact(input_);
const N& nbh = exact(nbh_);
- O& output = exact(output_);
-
+ typedef I O;
+ O output(input.domain());
mln_precondition(output.domain() == input.domain());
- image2d<unsigned> v_ima = level::compute_volume(input);
+ // compute volume image
+ typedef p_array<mln_psite(I)> S;
+ typedef accu::volume<I> A;
+ S sp = level::sort_psites_increasing(input);
+ morpho::tree::data<I,S> t(input, sp, c4());
+ image2d<unsigned> volume = morpho::tree::compute_attribute_image(A(), t);
// Local type
typedef mln_psite(I) P;
- typedef accu::volume<I> A;
- typedef p_vaccess<unsigned, p_array<point2d> > PV;
// Auxiliary data.
mln_ch_value(O, bool) deja_vu;
mln_ch_value(O, P) parent;
- PV arr;
- fill(arr, input);
+ initialize(deja_vu, input);
+ initialize(parent, input);
+ bool last = false;
+ unsigned n_cmpt = input.domain().nsites();
+
+ // Construct iterable value set
+ typedef p_array<P> Arr;
+// typedef p_vaccess<value::int_u<10>, Arr> SV;
+// SV s = convert::to<SV>(volume);
+ for
+ Arr s = convert::to<Arr>(volume);
- mln_fwd_piter(PV) v(arr);
+ mln_piter(Arr) last_v(s.nsites());
+ mln_piter(Arr) v(s.nsites());
for_all(v)
{
std::cout << "lambda=" << v << "-------"
<< std::endl;
+
// init
mln_ch_value(O, A) data;
initialize(data, input);
mln::level::fill(deja_vu, false);
{
- mln_fwd_piter(S) p(s);
+ mln_fwd_piter(S) p(sp);
for_all(p)
parent(p) = p;
}
// process
- mln_fwd_piter(S) p(s); // s required.
+ mln_fwd_piter(Arr) p(s(v));
mln_niter(N) n(nbh, p);
for_all(p)
{
- // Make set.
- data(p).take_as_init(make::pix(input, p)); // FIXME: algebraic so p!
- //data(p).take_as_init(1);
+ data(p).take_as_init(make::pix(input, p));
for_all(n)
{
if (input.domain().has(n) && deja_vu(n))
@@ -99,15 +126,14 @@
P r = find_root(parent, n);
if (r != p)
{
- //std::cout << data(p).to_result() << std::endl;
- if (input(r) == input(p) || (data(p).to_result() <= lambda))
+ if (input(r) == input(p) || (data(p).to_result() <=
(value::int_u<10>(v))))
// Either a flat zone or the component of r is still growing.
{
if(--n_cmpt < limit)
{
std::cout << "find less than " << limit <<
" cmpts" << std::endl;
std::cout << "Using last lambda" << std::endl;
- v--;
+ v = last_v;
last = true;
goto end;
}
@@ -115,9 +141,7 @@
parent(r) = p;
}
else
- {
- data(p).set_value(lambda);
- }
+ data(p).set_value(value::int_u<10>(v));
}
}
}
@@ -127,17 +151,20 @@
goto step2;
end:
n_cmpt = input.domain().nsites();
+ last_v = v;
}
-
+ step2:
// second pass
{
- mln_bkd_piter(S) p(s);
+ mln_bkd_piter(S) p(sp);
for_all(p)
if (parent(p) == p) // p is root.
output(p) = input(p);
else
output(p) = output(parent(p));
}
+
+ return output;
}
# endif // ! MLN_INCLUDE_ONLY
Index: jardonnet/n_cmpt/n_cmpt2.cc
--- jardonnet/n_cmpt/n_cmpt2.cc (revision 2975)
+++ jardonnet/n_cmpt/n_cmpt2.cc (working copy)
@@ -7,7 +7,6 @@
#include <mln/io/pgm/load.hh>
#include <mln/io/pgm/save.hh>
-
#include "n_cmpt2.hh"
using namespace mln;
@@ -15,7 +14,7 @@
bool usage(int argc, char ** argv)
{
- if (argc != 2)
+ if (argc != 3)
{
std::cout << argv[0] << " ima.pgm n_cmpt" << std::endl;
return false;
@@ -31,7 +30,8 @@
image2d<int_u8> ima;
io::pgm::load(ima, argv[1]);
- unsigned n_cmpt = atoi(argv[2]);
+ unsigned limit = atoi(argv[2]);
- n_cmpt(ima, c4(), n_cmpt, out);
+ io::pgm::save(n_cmpt(ima, c4(), limit),
+ "out.pgm");
}
Index: jardonnet/n_cmpt/Makefile
--- jardonnet/n_cmpt/Makefile (revision 2975)
+++ jardonnet/n_cmpt/Makefile (working copy)
@@ -5,4 +5,7 @@
g++ -I../../.. -Wall -W -Wextra n_cmpt2.cc -DNDEBUG -O1 -o n_cmpt2
debug: n_cmpt.hh n_cmpt.cc
- g++ -I../../.. -Wall -W -Wextra n_cmpt.cc -g -g3 -DNDEBUG -O1 -o n_cmpt
+ g++ -I../../.. -Wall -W -Wextra n_cmpt.cc -g -g3 -DNDEBUG -o n_cmpt
+
+debug2: n_cmpt2.hh n_cmpt2.cc
+ g++ -I../../.. -Wall -W -Wextra n_cmpt2.cc -g -g3 -DNDEBUG -o n_cmpt2
\ No newline at end of file
Index: jardonnet/n_cmpt/n_cmpt.hh
--- jardonnet/n_cmpt/n_cmpt.hh (revision 2975)
+++ jardonnet/n_cmpt/n_cmpt.hh (working copy)
@@ -86,7 +86,7 @@
mln_ch_value(O, bool) deja_vu;
mln_ch_value(O, P) parent;
- int n_cmpt = input.domain().nsites();
+ unsigned n_cmpt = input.domain().nsites();
std::cout << "Number of sites = " << n_cmpt <<
std::endl;