milena r3509: Fix bug in leaf propagation

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2009-03-10 Edwin Carlinet <carlinet@lrde.epita.fr> Fix bug in leaf propagation. * mln/morpho/tree/data.hh: Fix leaves calculus. * sandbox/edwin/tree/propagate.hh: Fix bugs in propagations. * sandbox/edwin/tree/tree.cc: Test file. --- mln/morpho/tree/data.hh | 27 +++++++++++----- sandbox/edwin/tree/propagate.hh | 17 +++++----- sandbox/edwin/tree/tree.cc | 66 +++++++++++++++++++++++++++++----------- 3 files changed, 77 insertions(+), 33 deletions(-) Index: trunk/milena/mln/morpho/tree/data.hh =================================================================== --- trunk/milena/mln/morpho/tree/data.hh (revision 3508) +++ trunk/milena/mln/morpho/tree/data.hh (revision 3509) @@ -41,6 +41,7 @@ # include <mln/core/image/sub_image.hh> # include <mln/core/site_set/p_array.hh> # include <mln/core/site_set/p_set.hh> +# include <mln/data/fill.hh> namespace mln { @@ -140,11 +141,12 @@ /// \} - /// \{ Leaves-related materials. - typedef p_array<mln_psite(I)> leaves_t; + /// \{ Nodes-related materials. + + typedef p_set<mln_psite(I)> leaves_t; - const p_set<mln_psite(I)>& leaves() const + const leaves_t& leaves() const { mln_precondition(is_valid()); return leaves_; @@ -185,8 +187,8 @@ const I& f_; const S& s_; mln_ch_value(I, mln_psite(I)) parent_; + leaves_t leaves_; p_array<mln_psite(I)> nodes_; - p_set<mln_psite(I)> leaves_; unsigned nroots_; }; @@ -210,14 +212,11 @@ // Store tree nodes. nroots_ = 0; mln_bkd_piter(S) p(s_); - mln_psite(I) old; for_all(p) { if (f_(parent_(p)) != f_(p)) { nodes_.insert(p); - if (parent_(p) != old) - leaves_.insert(old); } else if (parent_(p) == p) @@ -225,7 +224,19 @@ nodes_.insert(p); ++nroots_; } - old = p; + } + + // Store leaves. + mln_ch_value(I, bool) deja_vu; + initialize(deja_vu, f); + mln::data::fill(deja_vu, false); + + mln_fwd_piter(nodes_t) n(nodes_); + for_all(n) + { + deja_vu(parent_(n)) = true; + if (!deja_vu(n)) + leaves_.insert(n); } } Index: trunk/milena/sandbox/edwin/tree/propagate.hh =================================================================== --- trunk/milena/sandbox/edwin/tree/propagate.hh (revision 3508) +++ trunk/milena/sandbox/edwin/tree/propagate.hh (revision 3509) @@ -43,8 +43,9 @@ /// non-representative points of the same node. template <typename T, typename A> void - propagate_to_node(const T& t, A& a) + propagate_representant(const T& t, Image<A>& a_) { + A a = exact(a_); mln_fwd_piter(T) p(t.domain()); for_all(p) if (! t.is_a_node(p)) @@ -54,6 +55,8 @@ } } + namespace binary { + /// Propagate a tagged node's value to its subbranches. template <typename T, typename A> void @@ -62,7 +65,6 @@ mln_bkd_piter(T::nodes_t) n(t.nodes()); for_all(n) { - mln_assertion(t.is_a_node(n)); if (a(t.parent(n))) { mln_assertion(t.is_a_node(t.parent(n))); @@ -80,7 +82,6 @@ mln_fwd_piter(T::nodes_t) n(t.nodes()); for_all(n) { - mln_assertion(t.is_a_node(n)); if (a(t.parent(n))) { mln_assertion(t.is_a_node(t.parent(n))); @@ -97,7 +98,6 @@ mln_fwd_piter(T::nodes_t) n(t.nodes()); for_all(n) { - mln_assertion(t.is_a_node(n)); if (a(n)) { mln_assertion(t.is_a_node(t.parent(n))); @@ -109,7 +109,7 @@ /// Propagate a tagged node's value to its direct parents. template <typename T, typename A> void - propagate_to_parents(const T& t, A& a) + propagate_to_parent(const T& t, A& a) { mln_bkd_piter(T::nodes_t) n(t.nodes()); for_all(n) @@ -131,16 +131,17 @@ /// n <== l and a(l) is true. /// TODO: post-condition which checks this property. + template <typename T, typename A> void propagate_leaf_to_ancestors(const T& t, A& a) { - // 1st pass: leaves' parents are set to false. + mln_fwd_piter(T::leaves_t) l(t.leaves()); for_all(l) - t.parent(l) = 0; + a(t.parent(l)) = 0; + - // 2nd pass mln_fwd_piter(T::nodes_t) n(t.nodes()); for_all(n) { Index: trunk/milena/sandbox/edwin/tree/tree.cc =================================================================== --- trunk/milena/sandbox/edwin/tree/tree.cc (revision 3508) +++ trunk/milena/sandbox/edwin/tree/tree.cc (revision 3509) @@ -77,14 +77,18 @@ { mln_VAR(a, morpho::tree::compute_attribute_image(a_, tree_)); - display_tree_attributes(tree_, a); - find_treshold(a, tree_); + //display_tree_attributes(tree_, a); + //find_treshold(a, tree_); + + mln_fwd_piter(tree_t::nodes_t) n(tree_.nodes()); + for_all(n) + assert(tree_.is_a_leaf(tree_.parent(n)) == false); img_ = duplicate((pw::cst(lambda1) < pw::value(a) && pw::value(a) < pw::cst(lambda2)) | a.domain()); - debug::println("attribut", a); + //debug::println("attribut", a); } template <typename I, typename T> @@ -151,12 +155,12 @@ // Debug. - { - std::cout << " - " << val << ":" << pset.nsites() << " {"; - for_all(p) - std::cout << p << ","; - std::cout << "}" << std::endl; - } +// { +// std::cout << " - " << val << ":" << pset.nsites() << " {"; +// for_all(p) +// std::cout << p << ","; +// std::cout << "}" << std::endl; +// } } for (unsigned i = 0; i < f_domain.size() - 1; i++) @@ -213,8 +217,9 @@ void usage(char* argv[]) { - std::cerr << "usage: " << argv[0] << " input.pgm accumulator lambda1 [lambda2]" - << std::endl; + std::cerr << "usage: " << argv[0] << " input.pgm accumulator propagation lambda1 [lambda2]" + << "\taccu:\tcard | sharpness" << std::endl + << "\tpropa:\tto_children | to_childhood | to_parent | to_ancestors | leaf_to_ancestors" << std::endl; abort(); } @@ -228,17 +233,29 @@ float lambda1; float lambda2; - I input; + if (argc < 4) usage(argv); + I input; io::pgm::load(input, argv[1]); +// int_u8 vals[] = { 0, 1, 0, 2, 0, 1, 0, +// 1, 1, 1, 2, 1, 1, 1, +// 0, 1, 0, 2, 0, 1, 0, +// 2, 2, 2, 2, 2, 2, 2, +// 1, 1, 1, 2, 0, 1, 0, +// 1, 1, 1, 2, 1, 1, 1, +// 1, 1, 1, 2, 0, 1, 0}; +// I input = make::image2d(vals); - lambda1 = atof(argv[3]); - lambda2 = (argc == 5) ? atof(argv[4]) : mln_max(float); + + + lambda1 = atof(argv[4]); + lambda2 = (argc == 6) ? atof(argv[5]) : mln_max(float); std::string s(argv[2]); + std::string propa(argv[3]); treefilter<I>* f = 0; if (s == "card") f = new treefilter<I>(input, morpho::attribute::card<I>(), lambda1, lambda2); @@ -249,11 +266,26 @@ std::cout << "s1" << std::endl; - propagate_to_childhood(f->tree(), f->img()); + if (propa == "to_childhood") + morpho::tree::propagate_to_childhood(f->tree(), f->img()); + else if (propa == "to_children") + morpho::tree::propagate_to_children(f->tree(), f->img()); + else if (propa == "to_ancestors") + morpho::tree::propagate_to_ancestors(f->tree(), f->img()); + else if (propa == "to_parent") + morpho::tree::propagate_to_parent(f->tree(), f->img()); + else if (propa == "leaf_to_ancestors") + morpho::tree::propagate_leaf_to_ancestors(f->tree(), f->img()); + else + usage(argv); + std::cout << "s2" << std::endl; - propagate_to_node(f->tree(), f->img()); + morpho::tree::propagate_to_node(f->tree(), f->img()); std::cout << "s3" << std::endl; - filtercheck(*f, accu::meta::count()); + + + + //filtercheck(*f, accu::meta::count()); io::pbm::save(f->img(), "out.pbm");
participants (1)
-
Edwin Carlinet