milena r1207: Add graph_labeling in sandbox

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2007-10-01 Guillaume Duhamel <guillaume.duhamel@lrde.epita.fr> Add graph_labeling in sandbox. * graph_labeling.hh: New make_graph : convert a labeled image into mesh_p. * graph_labeling.cc: New test for graph labeling. * graph_labeling2.cc: New test for graph labeling. Update * draw_mesh.hh, * graph.hh, * main.cc, * main_mesh_image.cc, * mesh_p.hh: Update. --- draw_mesh.hh | 6 -- graph.hh | 118 +++++++++++++++++++++++++++++++++++++++++++++-------- graph_labeling.cc | 14 ++++++ graph_labeling.hh | 98 ++++++++++++++++++++++++++++++++++++++++++++ graph_labeling2.cc | 13 +++++ main.cc | 76 +++++++++++----------------------- main_mesh_image.cc | 12 ++--- mesh_p.hh | 78 ++++++++++++++++++++--------------- 8 files changed, 305 insertions(+), 110 deletions(-) Index: trunk/milena/sandbox/duhamel/main_mesh_image.cc =================================================================== --- trunk/milena/sandbox/duhamel/main_mesh_image.cc (revision 1206) +++ trunk/milena/sandbox/duhamel/main_mesh_image.cc (revision 1207) @@ -1,11 +1,11 @@ #include <mln/core/image2d_b.hh> #include <mln/core/point2d.hh> #include <mln/debug/println.hh> -#include "graph.hh" -#include "mesh_p.hh" -#include "mesh_psite.hh" -#include "draw_mesh.hh" -#include "mesh_image.hh" +#include <mln/util/graph.hh> +#include <mln/core/mesh_p.hh> +#include <mln/core/mesh_psite.hh> +#include <mln/draw/mesh.hh> +#include <mln/core/mesh_image.hh> #include <mln/core/interpolated.hh> using namespace mln; @@ -29,7 +29,7 @@ g.add_edge (4, 5); g.add_edge (1, 4); - g.coherence (); + g.consistency (); // g.print_debug (); std::vector<point2d> v; Index: trunk/milena/sandbox/duhamel/graph_labeling.hh =================================================================== --- trunk/milena/sandbox/duhamel/graph_labeling.hh (revision 0) +++ trunk/milena/sandbox/duhamel/graph_labeling.hh (revision 1207) @@ -0,0 +1,98 @@ +#include <mln/core/image2d_b.hh> +#include <mln/core/sub_image.hh> +#include <mln/core/neighb2d.hh> +#include <mln/value/int_u8.hh> +#include <mln/io/pgm/load.hh> +#include <mln/debug/println.hh> +#include <mln/debug/println.hh> +#include <mln/util/graph.hh> +#include <mln/core/mesh_p.hh> +#include <mln/core/mesh_psite.hh> +#include <mln/draw/mesh.hh> +#include <mln/core/mesh_image.hh> +#include <algorithm> + +namespace mln +{ + template <typename I, typename N> + mesh_p<point2d>* + make_graph (Image<I>& ima_, + const Neighborhood<N>& nbh) + { + util::graph<void> gr; + std::vector<point2d> v (300); + std::map<value::int_u8, int> m; + std::map<value::int_u8, int> x; + std::map<value::int_u8, int> y; + std::map<value::int_u8, int> c; + std::vector< std::vector<value::int_u8> > vec; + unsigned cpt = 0; + I& ima = exact(ima_); + + mln_fwd_piter(I) p(ima.domain()); + mln_niter(N) n(nbh, p); + + for_all (p) + { + if (ima(p) == 0) + { + std::vector<value::int_u8> v; + for_all (n) + { + if (ima(n) != 0) + v.push_back (ima(n)); + } + vec.push_back (v); + } + else + { + if (m.find (ima(p)) == m.end ()) + { + m[ima(p)] = cpt++; + x[ima(p)] = 0; + y[ima(p)] = 0; + c[ima(p)] = 1; + } + else + { + x[ima(p)] += p[1]; + y[ima(p)] += p[0]; + ++c[ima(p)]; + } + } + } + + typename std::map<value::int_u8, int>::iterator ite = m.begin (); + for (; ite != m.end (); ++ite) + { + gr.add_node (); + int tmp_x = y[(*ite).first] / c[(*ite).first]; + int tmp_y = x[(*ite).first] / c[(*ite).first]; + v[m[(*ite).first]] = (make::point2d(tmp_x, tmp_y)); + } + typename std::vector< std::vector<value::int_u8> >::iterator it = vec.begin (); + for (; it != vec.end (); ++it) + { + unique((*it).begin (), (*it).end()); + if ((*it).size () == 2) + gr.add_edge (m[(*it)[0]], m[(*it)[1]]); + if ((*it).size () == 3) + { + gr.add_edge (m[(*it)[0]], m[(*it)[1]]); + gr.add_edge (m[(*it)[1]], m[(*it)[2]]); + gr.add_edge (m[(*it)[0]], m[(*it)[2]]); + } + if ((*it).size () == 4) + { + gr.add_edge (m[(*it)[0]], m[(*it)[1]]); + gr.add_edge (m[(*it)[0]], m[(*it)[2]]); + gr.add_edge (m[(*it)[0]], m[(*it)[3]]); + gr.add_edge (m[(*it)[1]], m[(*it)[2]]); + gr.add_edge (m[(*it)[1]], m[(*it)[3]]); + gr.add_edge (m[(*it)[2]], m[(*it)[3]]); + } + } + + return (new mesh_p<point2d>(gr, v)); + } +} Index: trunk/milena/sandbox/duhamel/graph.hh =================================================================== --- trunk/milena/sandbox/duhamel/graph.hh (revision 1206) +++ trunk/milena/sandbox/duhamel/graph.hh (revision 1207) @@ -25,14 +25,19 @@ // reasons why the executable file might be covered by the GNU General // Public License. -#ifndef MLN_GRAPH_HH -# define MLN_GRAPH_HH +#ifndef MLN_UTIL_GRAPH_HH +# define MLN_UTIL_GRAPH_HH +# include <mln/core/concept/object.hh> # include <cstddef> # include <iostream> # include <vector> -# include <map> -# include <mln/core/concept/object.hh> + +/*! \file mln/util/graph.hh + * + * \brief Definition of an instant pix. + */ + namespace mln { @@ -68,32 +73,111 @@ }; template<typename N, typename E = void> - class graph + struct graph { - public: - graph () : - nb_node_ (0), nb_link_ (0) {} - graph (unsigned nb_node, unsigned nb_link) : - nb_node_ (nb_node), nb_link_ (nb_link) {} - ~graph () {} + graph (); - // void add_node (N& elt); void add_node (void); void add_edge (unsigned n1, unsigned n2); void coherence () const; void print_debug () const; - - public: unsigned nb_node_; unsigned nb_link_; std::vector<struct s_node<N>*> nodes_; std::vector<struct s_edge<E>*> links_; }; - } // end of util -} // end of mln +# ifndef MLN_INCLUDE_ONLY + + template<typename N, typename E> + graph<N, E>::graph () + : nb_node_ (0), + nb_link_ (0), + nodes_ (0), + links_ (0) + { + } + + template<typename N, typename E> + void + graph<N, E>::add_node (void) + { + struct s_node<N>* n = new struct s_node<N>; + + nodes_.push_back (n); + ++nb_node_; + } + + template<typename N, typename E> + void + graph<N, E>::add_edge (unsigned n1, unsigned n2) + { + mln_precondition(n1 < this->nb_node_); + mln_precondition(n2 < this->nb_node_); + + struct s_edge<E>* edge; + + edge = new struct s_edge<E>; + edge->node1 = n1; + edge->node2 = n2; + links_.push_back (edge); + ++nb_link_; + nodes_[n1]->links.push_back (n2); + nodes_[n2]->links.push_back (n1); + } + + template<typename N, typename E> + void + graph<N, E>::coherence () const + { + mln_precondition(nodes_.size () == this->nb_node_); + mln_precondition(links_.size () == this->nb_link_); + typename std::vector<struct s_node <N>*>::const_iterator it = nodes_.begin (); + for (; it != nodes_.end (); ++it) + { + typename std::vector<unsigned>::const_iterator it2 = (*it)->links.begin (); + for (; it2 != (*it)->links.end (); ++it2) + mln_precondition((*it2) < nb_node_); + } + + typename std::vector<struct s_edge<E>*>::const_iterator it3 = links_.begin (); + for (; it3 != links_.end (); ++it3) + { + mln_precondition((*it3)->node1 < nb_node_); + mln_precondition((*it3)->node2 < nb_node_); + } + } + + template<typename N, typename E> + void + graph<N, E>::print_debug () const + { + std::cout << "nodes :" + << std::endl; + + typename std::vector<struct s_node<N>*>::const_iterator it = nodes_.begin (); + int i = 0; + for (; it != nodes_.end (); ++it, ++i) + { + std::cout << "node number = " + << i + << " nbh : "; + typename std::vector<unsigned>::const_iterator it2 = (*it)->links.begin (); + for (; it2 != (*it)->links.end (); ++it2) + { + std::cout << (*it2) + << " "; + } + std::cout << std::endl; + } + std::cout << std::endl; + } + +# endif // ! MLN_INCLUDE_ONLY -#include "graph.hxx" + } // end of util + +} // end of mln #endif // MLN_GRAPH_HH Index: trunk/milena/sandbox/duhamel/draw_mesh.hh =================================================================== --- trunk/milena/sandbox/duhamel/draw_mesh.hh (revision 1206) +++ trunk/milena/sandbox/duhamel/draw_mesh.hh (revision 1207) @@ -4,12 +4,10 @@ # include <mln/pw/image.hh> #include <mln/core/image2d_b.hh> # include <mln/core/point2d.hh> -//# include <mln/pw/cst.hh> # include <mln/level/fill.hh> -//# include <mln/metal/is_a.hh> # include <mln/draw/line.hh> -# include "mesh_p.hh" -# include "mesh_image.hh" +# include <mln/core/mesh_p.hh> +# include <mln/core/mesh_image.hh> namespace mln { Index: trunk/milena/sandbox/duhamel/main.cc =================================================================== --- trunk/milena/sandbox/duhamel/main.cc (revision 1206) +++ trunk/milena/sandbox/duhamel/main.cc (revision 1207) @@ -1,64 +1,38 @@ #include <mln/core/image2d_b.hh> -#include <mln/core/point2d.hh> +#include <mln/core/sub_image.hh> +#include <mln/core/neighb2d.hh> +#include <mln/value/int_u8.hh> +#include <mln/level/fill.hh> +#include <mln/level/stretch.hh> +#include <mln/io/pbm/load.hh> +#include <mln/io/pgm/save.hh> +#include <mln/io/pgm/save.hh> +#include <mln/io/pgm/save.hh> +#include <mln/core/mesh_image.hh> +# include <mln/labeling/base.hh> #include <mln/debug/println.hh> -#include "graph.hh" -#include "mesh_p.hh" -#include "mesh_psite.hh" -#include "draw_mesh.hh" -#include "mesh_image.hh" -#include <mln/core/interpolated.hh> +# include <mln/core/window2d.hh> +# include <mln/convert/to_window.hh> +# include <mln/core/concept/dpoint.hh> +# include <mln/core/concept/neighborhood.hh> +# include <mln/core/window.hh> +# include <mln/pw/image.hh> +# include <mln/pw/cst.hh> +# include <mln/metal/is_a.hh> using namespace mln; +namespace mln +{ + +} int main (void) { + image2d_b<int_u8> in = io::pbm::load("l.pgm"); util::graph<void> g; - g.add_node (); - g.add_node (); - g.add_node (); - g.add_node (); - g.add_node (); - g.add_node (); - - g.add_edge (0, 1); - g.add_edge (1, 2); - g.add_edge (2, 3); - g.add_edge (1, 3); - g.add_edge (4, 5); - g.add_edge (1, 4); - - g.coherence (); - // g.print_debug (); - - std::vector<point2d> v; - v.push_back (make::point2d (1,1)); - v.push_back (make::point2d (10,1)); - v.push_back (make::point2d (19,2)); - v.push_back (make::point2d (19,19)); - v.push_back (make::point2d (10,10)); - v.push_back (make::point2d (1,19)); - - - image2d_b<int> ima (20, 20, 1); - - mesh_p<point2d> m(g, v); - - draw::mesh (ima, m, 7, 1); - - std::vector<int> val; - - val.push_back (2); - val.push_back (3); - val.push_back (4); - val.push_back (5); - val.push_back (6); - val.push_back (7); - - mesh_image<point2d, int> im (m, val); + mln_bkd_pixter(const image2d_b<int_u8>) p(ima.input); - draw::mesh (ima, im); - debug::println (ima); } Index: trunk/milena/sandbox/duhamel/mesh_p.hh =================================================================== --- trunk/milena/sandbox/duhamel/mesh_p.hh (revision 1206) +++ trunk/milena/sandbox/duhamel/mesh_p.hh (revision 1207) @@ -5,7 +5,7 @@ # include <mln/core/internal/point_set_base.hh> # include <mln/core/internal/point_iterator_base.hh> # include <mln/accu/bbox.hh> -# include "graph.hh" +# include <mln/util/graph.hh> # include "mesh_psite.hh" namespace mln @@ -13,53 +13,27 @@ template<typename P> class mesh_p_piter_; - template<typename P> - class mesh_p : public internal::point_set_base_< P, mesh_p<P> > + struct mesh_p : public internal::point_set_base_< P, mesh_p<P> > { - public: - mesh_p () {} - mesh_p (util::graph<void>& gr, - std::vector<P>& loc) - : gr_ (gr), - loc_ (loc) - { - accu::bbox<P> a; - for (unsigned i = 0; i < loc.size(); ++i) - a.take(loc[i]); - bb_ = a.to_result(); - } + std::vector<P>& loc); /// Point_Site associated type. typedef mesh_psite<P> psite; - //FIXME /// Forward Point_Iterator associated type. typedef mesh_p_piter_<P> fwd_piter; /// Backward Point_Iterator associated type. typedef mesh_p_piter_<P> bkd_piter; - //END FIXME - std::size_t npoints() const - { - return this->gr_.nb_node_; - } + std::size_t npoints() const; /// Give the exact bounding box. - const box_<P>& bbox() const - { - return bb_; - } + const box_<P>& bbox() const; - bool has(const psite& p) const - { - for (unsigned i = 0; i < loc_.size(); ++i) - if (loc_[i] == p) - return true; - return false; - } + bool has(const psite& p) const; util::graph<void> gr_; std::vector<P> loc_; @@ -73,6 +47,7 @@ { typedef mesh_p_piter_<P> self_; typedef internal::point_iterator_base_< P, self_ > super_; + public: // Make definitions from super class available. @@ -137,7 +112,46 @@ P p_; }; +# ifndef MLN_INCLUDE_ONLY + + template<typename P> + mesh_p<P>::mesh_p (util::graph<void>& gr, + std::vector<P>& loc) + : gr_ (gr), + loc_ (loc) + { + accu::bbox<P> a; + for (unsigned i = 0; i < loc.size(); ++i) + a.take(loc[i]); + bb_ = a.to_result(); + } + + template<typename P> + std::size_t + mesh_p<P>::npoints() const + { + return this->gr_.nb_node_; + } + + template<typename P> + const box_<P>& + mesh_p<P>::bbox() const + { + return bb_; + } + + template<typename P> + bool + mesh_p<P>::has(const psite& p) const + { + for (unsigned i = 0; i < loc_.size(); ++i) + if (loc_[i] == p) + return true; + return false; + } + +# endif // ! MLN_INCLUDE_ONLY } // end of mln Index: trunk/milena/sandbox/duhamel/graph_labeling.cc =================================================================== --- trunk/milena/sandbox/duhamel/graph_labeling.cc (revision 0) +++ trunk/milena/sandbox/duhamel/graph_labeling.cc (revision 1207) @@ -0,0 +1,14 @@ +#include "graph_labeling.hh" + +int main() +{ + using namespace mln; + using value::int_u8; + + image2d_b<int_u8> ima = io::pgm::load("/lrde/beyrouth/stage/duhamel/pub/img_1.pgm"); + image2d_b<int_u8> im (ima.domain ()); + mesh_p<point2d>* m = make_graph (ima, c4()); + draw::mesh (im, *m, 7, 1); + + debug::println (im); +} Index: trunk/milena/sandbox/duhamel/graph_labeling2.cc =================================================================== --- trunk/milena/sandbox/duhamel/graph_labeling2.cc (revision 0) +++ trunk/milena/sandbox/duhamel/graph_labeling2.cc (revision 1207) @@ -0,0 +1,13 @@ +#include "graph_labeling.hh" + +int main() +{ + using namespace mln; + using value::int_u8; + + image2d_b<int_u8> ima = io::pgm::load("/lrde/beyrouth/stage/duhamel/pub/img_2.pgm"); + image2d_b<int_u8> im (ima.domain ()); + mesh_p<point2d>* m = make_graph (ima, c4()); + draw::mesh (im, *m, 7, 1); + debug::println (im | make::box2d(100, 60)); +}
participants (1)
-
Guillaume Duhamel