r3477: Add methods to check attribute computing

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox ChangeLog: 2009-03-04 Edwin Carlinet <carlinet@lrde.epita.fr> Add methods to check attribute computing. * edwin/tree/propagate.hh, * edwin/tree/tree.cc: Fix bugs and add check function. * edwin/tree/routines.hh: Some utils about tree manipulation. --- Makefile | 14 +-- propagate.hh | 2 routines.hh | 109 +++++++++++++++++++++++++++++++ tree.cc | 207 +++++++++++++++++++++++++++++++---------------------------- 4 files changed, 227 insertions(+), 105 deletions(-) Index: trunk/milena/sandbox/edwin/tree/propagate.hh =================================================================== --- trunk/milena/sandbox/edwin/tree/propagate.hh (revision 3476) +++ trunk/milena/sandbox/edwin/tree/propagate.hh (revision 3477) @@ -62,7 +62,7 @@ { mln_bkd_piter(T) p(t.domain()); for_all(p) - if (a(t.parent(p)) == v) + if (t.is_a_node(p) && a(t.parent(p)) == v) { mln_assertion(t.is_a_node(t.parent(p))); a(p) = a(t.parent(p)); Index: trunk/milena/sandbox/edwin/tree/tree.cc =================================================================== --- trunk/milena/sandbox/edwin/tree/tree.cc (revision 3476) +++ trunk/milena/sandbox/edwin/tree/tree.cc (revision 3477) @@ -1,4 +1,3 @@ - #include <mln/core/var.hh> #include <mln/core/image/image2d.hh> #include <mln/core/alias/neighb2d.hh> @@ -12,149 +11,167 @@ #include <mln/io/pbm/save.hh> #include <mln/level/sort_psites.hh> -#include <mln/morpho/elementary/gradient.hh> #include <mln/morpho/tree/data.hh> #include <mln/morpho/tree/compute_attribute_image.hh> + #include <mln/morpho/attribute/card.hh> -#include "../attributes/occupation.hh" -#include "propagate.hh" +#include <mln/morpho/attribute/sharpness.hh> +// to check: +#include <mln/labeling/blobs.hh> +#include <mln/labeling/compute.hh> +#include <mln/accu/count.hh> #include <mln/debug/println.hh> +#include <../../theo/color/change_attributes.hh> +//----------------- + + +#include <string> + +#include "propagate.hh" +#include "routines.hh" namespace mln { - template <typename T, typename A> + /// Structure to simplify filtering using min tree. + template <typename I> + struct treefilter : Object< treefilter<I> > + { + typedef p_array< mln_site(I) > S; + typedef morpho::tree::data<I,S> tree_t; + + /// Constructor: Make the min tree based on the image \p f_, then + /// performs calculus using \p a_ attribute. To finish, it filters + /// tree's nodes which values are in [lambda1, lambda2] set. + template <typename A> + treefilter(Image<I>& f_, + Accumulator<A> a_, + double lambda1 = mln_min(double), + double lambda2 = mln_max(double)); + + /// Get the min tree performed. + tree_t& tree() { return tree_; }; + + /// Get the boolean image got after filtering. + mln_ch_value(I, bool)& img() {return img_; }; + + private: + S sorted_sites_; + tree_t tree_; + mln_ch_value(I, bool) img_; + }; + + + template <typename I> + template <typename A> inline - void - sample(const T& t, const A& a, int echo) + treefilter<I>::treefilter(Image<I>& f_, + Accumulator<A> a_, + double lambda1, + double lambda2) + : sorted_sites_(level::sort_psites_decreasing(exact(f_))), + tree_(exact(f_), sorted_sites_, c4()) { - A aa; - initialize(aa, a); - data::fill(aa, false); // 'aa' is false /a priori/. - - typedef typename T::nodes_t N; - mln_fwd_piter(N) n(t.nodes()); - for_all(n) - // We only keep "highest" nodes at 'true' => largest component. - // aa(n) = (a(n) == true && a(t.parent(n)) == false); - aa(n) = a(n); - - if (echo) io::pbm::save(aa, "before.pbm"); - if (echo > 1) debug::println("aa (before)", aa); - - back_propagate_subbranch(t, aa, true); - if (echo > 1) debug::println("aa (After subbranch propagation)", aa); - back_propagate_level(t, aa); + mln_VAR(a, morpho::tree::compute_attribute_image(a_, tree_)); - if (echo > 1) debug::println("aa (Final)", aa); - io::pbm::save(aa, "out.pbm"); + img_ = duplicate((pw::cst(lambda1) < pw::value(a) && + pw::value(a) < pw::cst(lambda2)) + | a.domain()); + debug::println("attribut", a); } +// template <typename T> +// inline +// float +// find_treshold(const T& t) +// { +// mln_bkd_piter(T) = p(t.domain()); -} // mln +// for_all(p) +// if (t.is_a_node(p)) +// { +// if -using namespace mln; -int echo = 0; +// } +// } template <typename I, typename A> -inline -void -create_tree_and_compute(Image<I>& f_, Accumulator<A> a_, float lambda, float lambda2 = mln_max(float)) + void filtercheck(const Image<I>& img, const Meta_Accumulator<A>& a) { - using value::int_u8; - - I f = exact(f_); - - typedef p_array< mln_site(I) > S; - S s = level::sort_psites_decreasing(f); - typedef morpho::tree::data<I,S> tree_t; - tree_t t(f, s, c4()); - - mln_VAR(a, morpho::tree::compute_attribute_image(a_, t)); + using value::label_8; + label_8 n; + util::array<unsigned int> counts; - if (echo > 1) + debug::println("binaire:", img); + mln_VAR(lbl, labeling::blobs(img, c4(), n)); + debug::println("blob:", lbl); + counts = labeling::compute(a, lbl, n); + for (unsigned i = 0; i < counts.nelements(); i++) { - debug::println("parent imagee", t.parent_image()); - debug::println("a", a); - debug::println("a | nodes", a | t.nodes()); + std::cout << "counts[" << i << "]: " << counts[i] + << std::endl; } - - std::cout << lambda; - image2d<bool> b = duplicate((pw::cst(lambda) < pw::value(a) && pw::value(a) < pw::cst(lambda2)) | a.domain()); - sample(t, b, echo); } +} // end of namespace mln + + void usage(char* argv[]) { - std::cerr << "usage: " << argv[0] << " input.pgm echo lambda1 lamda2" << std::endl; - std::cerr << "\techo:\t0 (none)" << std::endl - << "\t\t1 (img output)" << std::endl - << "\t\t2 (debug)" << std::endl; + std::cerr << "usage: " << argv[0] << " input.pgm accumulator lambda1 [lambda2]" + << std::endl; abort(); } + int main(int argc, char* argv[]) { + using namespace mln; using value::int_u8; - mln_VAR(nbh, c4()); + typedef image2d<int_u8> I; + + float lambda1; + float lambda2; + I input; if (argc < 4) usage(argv); - echo = std::atoi(argv[2]); - float lambda1 = atof(argv[3]); - float lambda2 = (argc == 5) ? atof(argv[4]) : mln_max(float); - - typedef image2d<int_u8> I; - - I input; io::pgm::load(input, argv[1]); - if (echo > 1) debug::println("input", input); - I f = input; - // I f = morpho::elementary::gradient(input, nbh); - if (echo > 1) debug::println("f", f); - - // test de volume - typedef image1d<int> IM; - IM img(6); - morpho::attribute::volume<IM> accu; - img.element(0) = 50; - img.element(1) = 50; - img.element(2) = 40; - img.element(3) = 40; - img.element(4) = 20; - img.element(5) = 20; + lambda1 = atof(argv[3]); + lambda2 = (argc == 5) ? atof(argv[4]) : mln_max(float); - mln_piter_(image1d<int>) p(img.domain()); + std::string s(argv[2]); + treefilter<I>* f = 0; + if (s == "card") + f = new treefilter<I>(input, morpho::attribute::card<I>(), lambda1, lambda2); + else if (s == "sharpness") + f = new treefilter<I>(input, morpho::attribute::sharpness<I>(), lambda1, lambda2); + else + usage(argv); - int tab[6] = { 50, 50, 40, 40, 20, 20 }; + back_propagate_subbranch(f->tree(), f->img() ,true); + back_propagate_level(f->tree(), f->img()); - for (int i = 0; i < 6; i++) - { - accu.take(tab[i]); - std::cout << "(" << tab[i] << "," << accu.to_result() << "):"; - } - std::cout << "Volume:" << accu.to_result() << std::endl; + filtercheck(f->img(), accu::meta::count()); - accu.init (); - for (int i = 5; i >= 0; i--) - { - accu.take(tab[i]); - std::cout << "(" << tab[i] << "," << accu.to_result() << "):"; - } - std::cout << "Volume:" << accu.to_result() << std::endl; - //create_tree_and_compute(img, morpho::attribute::volume<I2>()); - // + util::array< mln_psite_(I) > counts; + counts = morpho::tree::get_first_nodes(f->img(), f->tree()); + for (unsigned i = 0; i < counts.nelements(); i++) + std::cout << "counts[" << i << "]: " << counts[i] << std::endl; - create_tree_and_compute(f, morpho::attribute::occupation<I>(), lambda1, lambda2); + mln_VAR(a, morpho::tree::compute_attribute_image(morpho::attribute::card<I>(), f->tree())); + display_tree_attributes(f->tree(), a); + io::pbm::save(f->img(), "out.pbm"); + delete f; } Index: trunk/milena/sandbox/edwin/tree/routines.hh =================================================================== --- trunk/milena/sandbox/edwin/tree/routines.hh (revision 0) +++ trunk/milena/sandbox/edwin/tree/routines.hh (revision 3477) @@ -0,0 +1,109 @@ +// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory +// (LRDE) +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef ROUTINES_HH_ +# define ROUTINES_HH_ + +# include <mln/morpho/tree/data.hh> +# include <mln/core/concept/image.hh> +# include <mln/data/fill.hh> + +namespace mln { + namespace morpho { + namespace tree { + + template <typename I, typename T> + util::array< mln_psite(I) > + get_first_nodes(const Image<I>& img_, const T& tree) + { + I bin = exact(img_); + mln_ch_value(I, bool) deja_vu; + util::array< mln_psite(I) > fnodes; + + initialize(deja_vu, img_); + data::fill(deja_vu, false); + + bool can_break = false; + mln_bkd_piter(T) p(tree.domain()); + for_all(p) + { + if (tree.is_a_node(p) && bin(p) && bin(t.parent(p)) + { + fnodes.append(p); + } + } + + // else if (can_break) +// { +// std::cout << p << std::endl; +// break; +// } + + return fnodes; + } + + } + } + +// namespace debug { + +// template <typename T, typename I> +// void +// println(const T& t, const Image<I> f_) +// { +// //theo's code +// typedef mln_site(I) P; +// I f = exact(f_); + +// mln_ch_value(I, bool) deja_vu; +// initialize(deja_vu, f); +// data::fill(deja_vu, false); + +// typedef typename T::nodes_t nodes_t; +// mln_fwd_piter(T) p(t.nodes()); +// for_all(p) +// { +// if (deja_vu(p)) +// continue; +// P e = p; +// do +// { +// std::cout << f(e) << ':' << e << " -> "; +// deja_vu(e) = true; +// e = t.parent(e); +// } +// while (! deja_vu(e)); +// std::cout << f(e) << ':' << e << std::endl; +// } +// std::cout << std::endl; +// } +// } +} + + +#endif /* !ROUTINES_HH_ */ Index: trunk/milena/sandbox/edwin/tree/Makefile =================================================================== --- trunk/milena/sandbox/edwin/tree/Makefile (revision 3476) +++ trunk/milena/sandbox/edwin/tree/Makefile (revision 3477) @@ -1,28 +1,25 @@ -TARGET=a.out +TARGET=tree SRC=tree.cc OBJS=${SRC:.cc=.o} -OLENADIR=../../../.. +OLENADIR=$(MLN_DIR)/.. MILENADIR=$(OLENADIR)/milena CXXFLAGS=-I$(MILENADIR) -I./ -CXXFLAGS += "-DNDEBUG -O1" - +CXXFLAGS += -DNDEBUG -O1 CXX=g++ LD=g++ LDFLAGS= all: clean $(TARGET) + #chmod +x exo2.sh + #./exo2.sh $(TARGET): $(OBJS) $(SRC) $(LD) $(LDFLAGS) -o $@ $(OBJS) - -$(REF): $(OBJS_REF) $(SRC_REF) - $(LD) $(LDFLAGS) -o $@ $(OBJS_REF) - %.o: %.cc $(CXX) $(CXXFLAGS) -c $< @@ -30,4 +27,6 @@ $(CXX) $(CXXFLAGS) -c $< clean: - rm -f *.o $(REF) $(TARGET) \ No newline at end of file + rm -f *.o $(TARGET) + rm -f *.pbm + find -name "*.pgm" \! -regex ".*/affiche2?.pgm" -delete \ No newline at end of file
participants (1)
-
Edwin Carlinet