milena r3446: Exo2: Perform attribute calculus on trees

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2009-02-28 Edwin Carlinet <carlinet@lrde.epita.fr> Exo2: Perform attribute calculus on trees. * sandbox/theo/rush/exo2/Makefile: New. * sandbox/theo/rush/exo2/exo2.cc: New. * sandbox/theo/rush/exo2/propagate.hh: New. * sandbox/theo/rush/exo2: New. * sandbox/theo/rush: New. --- Makefile | 32 ++++++++++++++++++ exo2.cc | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ propagate.hh | 77 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 210 insertions(+) Index: trunk/milena/sandbox/theo/rush/exo2/propagate.hh =================================================================== --- trunk/milena/sandbox/theo/rush/exo2/propagate.hh (revision 0) +++ trunk/milena/sandbox/theo/rush/exo2/propagate.hh (revision 3446) @@ -0,0 +1,77 @@ +// Copyright (C) 2008 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 MLN_MORPHO_TREE_PROPAGATE_HH_ +# define MLN_MORPHO_TREE_PROPAGATE_HH_ + +/// \file mln/morpho/tree/propagate.hh +/// +/// Functions to propagate a node value in the tree. + +#include <mln/morpho/tree/data.hh> + +namespace mln { + namespace morpho { + namespace tree { + + + /// The representative point propagates its value to + /// its non-representative children nodes. + template <typename T, typename A> + void + back_propagate_level(const T& t, A& a) + { + mln_fwd_piter(T) p(t.domain()); + for_all(p) + if (! t.is_a_node(p)) + { + mln_assertion(t.is_a_node(t.parent(p))); + a(p) = a(t.parent(p)); + } + } + + /// The representative point having the right + /// value propagates to the nodes of its sub-branch. + template <typename T, typename A> + void + back_propagate_subbranch(const T& t, A& a, mln_value(A) v) + { + mln_bkd_piter(T) p(t.domain()); + for_all(p) + 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)); + } + } + } // end of namespace mln::morpho::tree + } // end of namespace mln::morpho +} // end of namespace mln + +#endif /* !MLN_MORPHO_TREE_PROPAGATE_HH_ */ + + Index: trunk/milena/sandbox/theo/rush/exo2/exo2.cc =================================================================== --- trunk/milena/sandbox/theo/rush/exo2/exo2.cc (revision 0) +++ trunk/milena/sandbox/theo/rush/exo2/exo2.cc (revision 3446) @@ -0,0 +1,101 @@ +#include <mln/core/var.hh> +#include <mln/core/image/image2d.hh> +#include <mln/core/alias/neighb2d.hh> +#include <mln/core/routine/duplicate.hh> +#include <mln/pw/all.hh> + +#include <mln/value/int_u8.hh> +#include <mln/value/label_8.hh> + +#include <mln/io/pgm/load.hh> +#include <mln/io/pbm/save.hh> + +#include <mln/level/sort_psites.hh> + +#include <mln/morpho/tree/data.hh> +#include <mln/morpho/tree/compute_attribute_image.hh> + +#include <mln/morpho/attribute/card.hh> +#include <mln/morpho/attribute/sharpness.hh> + +#include "propagate.hh" + + +namespace mln +{ + template <typename I> + struct treefilter + { + typedef p_array< mln_site(I) > S; + typedef morpho::tree::data<I,S> tree_t; + + template <typename A> + treefilter(Image<I>& f_, + Accumulator<A> a_, + double lambda1 = mln_min(double), + double lambda2 = mln_max(double)); + + tree_t& tree() { return tree_; }; + 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 + 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()) + { + mln_VAR(a, morpho::tree::compute_attribute_image(a_, tree_)); + + img_ = duplicate((pw::cst(lambda1) < pw::value(a) && + pw::value(a) < pw::cst(lambda2)) | a.domain()); + } + +} // end of namespace mln + + + +void usage(char* argv[]) +{ + std::cerr << "usage: " << argv[0] << " input.pgm lambda1 lamda2" + << std::endl; + abort(); +} + + +int main(int argc, char* argv[]) +{ + using namespace mln; + using value::int_u8; + + typedef image2d<int_u8> I; + + float lambda1; + float lambda2; + I input; + + if (argc < 3) + usage(argv); + + io::pgm::load(input, argv[1]); + lambda1 = atof(argv[2]); + lambda2 = (argc == 4) ? atof(argv[3]) : mln_max(float); + + treefilter<I> f(input, morpho::attribute::sharpness<I>(), lambda1, lambda2); + + back_propagate_subbranch(f.tree(), f.img() ,true); + back_propagate_level(f.tree(), f.img()); + + io::pbm::save(f.img(), "out.pbm"); +} Index: trunk/milena/sandbox/theo/rush/exo2/Makefile =================================================================== --- trunk/milena/sandbox/theo/rush/exo2/Makefile (revision 0) +++ trunk/milena/sandbox/theo/rush/exo2/Makefile (revision 3446) @@ -0,0 +1,32 @@ +TARGET=exo2 +SRC=exo2.cc +OBJS=${SRC:.cc=.o} + +OLENADIR=../../../../.. +MILENADIR=$(OLENADIR)/milena + +CXXFLAGS=-I$(MILENADIR) -I./ + +CXXFLAGS += -DNDEBUG -O1 + +CXX=g++ +LD=g++ +LDFLAGS= + +all: clean $(TARGET) + +$(TARGET): $(OBJS) $(SRC) + $(LD) $(LDFLAGS) -o $@ $(OBJS) + + +$(REF): $(OBJS_REF) $(SRC_REF) + $(LD) $(LDFLAGS) -o $@ $(OBJS_REF) + +%.o: %.cc + $(CXX) $(CXXFLAGS) -c $< + +%.o: %.hh + $(CXX) $(CXXFLAGS) -c $< + +clean: + rm -f *.o $(REF) $(TARGET)
participants (1)
-
Edwin Carlinet