Commit 213b67f7 authored by Edwin Carlinet's avatar Edwin Carlinet
Browse files

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.

git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@3477 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent 412967e0
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.
2009-03-04 Vivien Delmon <vivien.delmon@lrde.epita.fr>
Two algorithms implemented during my internship in GE.
......
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 @@ $(REF): $(OBJS_REF) $(SRC_REF)
$(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
......@@ -62,7 +62,7 @@ namespace mln {
{
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));
......
// 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_ */
#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>
inline
void
sample(const T& t, const A& a, int echo)
/// Structure to simplify filtering using min tree.
template <typename I>
struct treefilter : Object< treefilter<I> >
{
A aa;
initialize(aa, a);
data::fill(aa, false); // 'aa' is false /a priori/.
typedef p_array< mln_site(I) > S;
typedef morpho::tree::data<I,S> tree_t;
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);
/// 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));
if (echo) io::pbm::save(aa, "before.pbm");
if (echo > 1) debug::println("aa (before)", aa);
/// Get the min tree performed.
tree_t& tree() { return tree_; };
back_propagate_subbranch(t, aa, true);
if (echo > 1) debug::println("aa (After subbranch propagation)", aa);
back_propagate_level(t, aa);
/// Get the boolean image got after filtering.
mln_ch_value(I, bool)& img() {return img_; };
if (echo > 1) debug::println("aa (Final)", aa);
io::pbm::save(aa, "out.pbm");
}
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_));
} // mln
img_ = duplicate((pw::cst(lambda1) < pw::value(a) &&
pw::value(a) < pw::cst(lambda2))
| a.domain());
using namespace mln;
int echo = 0;
debug::println("attribut", a);
}
template <typename I, typename A>
inline
void
create_tree_and_compute(Image<I>& f_, Accumulator<A> a_, float lambda, float lambda2 = mln_max(float))
{
using value::int_u8;
// template <typename T>
// inline
// float
// find_treshold(const T& t)
// {
// mln_bkd_piter(T) = p(t.domain());
I f = exact(f_);
// for_all(p)
// if (t.is_a_node(p))
// {
// if
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());
template <typename I, typename A>
void filtercheck(const Image<I>& img, const Meta_Accumulator<A>& a)
{
mln_VAR(a, morpho::tree::compute_attribute_image(a_, t));
using value::label_8;
label_8 n;
util::array<unsigned int> counts;
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++)
{
std::cout << "counts[" << i << "]: " << counts[i]
<< std::endl;
}
}
} // end of namespace mln
if (echo > 1)
{
debug::println("parent imagee", t.parent_image());
debug::println("a", a);
debug::println("a | nodes", a | t.nodes());
}
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);
}
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);
lambda1 = atof(argv[3]);
lambda2 = (argc == 5) ? atof(argv[4]) : mln_max(float);
// 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;
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;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment