URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2008-01-21 Matthieu Garrigues <garrigues(a)lrde.epita.fr>
Add accu::rank.
* mln/accu/rank.hh: New, accumulator for the rank filters.
* mln/accu/rank_bool.hh: New. Specialisation of accu::rank<bool>.
* tests/accu/rank.cc: New.
---
mln/accu/rank.hh | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++
mln/accu/rank_bool.hh | 133 +++++++++++++++++++++++++++++++++++++
tests/accu/rank.cc | 55 +++++++++++++++
3 files changed, 364 insertions(+)
Index: trunk/milena/tests/accu/rank.cc
===================================================================
--- trunk/milena/tests/accu/rank.cc (revision 0)
+++ trunk/milena/tests/accu/rank.cc (revision 1675)
@@ -0,0 +1,55 @@
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+//
+// 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.
+
+/*! \file tests/accu/rank.cc
+ *
+ * \brief Tests on mln::accu::rank.
+ */
+
+#include <mln/accu/rank.hh>
+
+int main()
+{
+ using namespace mln;
+
+ accu::rank_<int> accu(3, 5);
+
+ accu.take(3);
+ accu.take(1);
+ accu.take(4);
+ accu.take(5);
+ accu.take(2);
+ mln_assertion(accu.to_result() == 4);
+
+ accu::rank_<bool> accu_bool(1, 5);
+ accu_bool.take(true);
+ accu_bool.take(true);
+ accu_bool.take(true);
+ accu_bool.take(true);
+ accu_bool.take(false);
+ mln_assertion(accu_bool == true);
+}
Index: trunk/milena/mln/accu/rank.hh
===================================================================
--- trunk/milena/mln/accu/rank.hh (revision 0)
+++ trunk/milena/mln/accu/rank.hh (revision 1675)
@@ -0,0 +1,176 @@
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+//
+// 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_ACCU_RANK_HH
+# define MLN_ACCU_RANK_HH
+
+/*! \file mln/accu/rank.hh
+ *
+ * \brief Define an rank accumulator.
+ */
+
+# include <vector>
+# include <mln/accu/internal/base.hh>
+# include <mln/core/concept/meta_accumulator.hh>
+# include <mln/trait/value_.hh>
+# include <mln/util/pix.hh>
+# include <mln/core/inplace.hh>
+
+namespace mln
+{
+
+ namespace accu
+ {
+
+
+ /*! \brief Generic rank accumulator class.
+ *
+ * The parameter \c T is the type of values.
+ */
+ template <typename T>
+ struct rank_ : public mln::accu::internal::base_< T, rank_<T> >
+ {
+ typedef T argument;
+ typedef T result;
+
+ rank_(unsigned k, unsigned n);
+
+ void init();
+ void take_as_init(const argument& t);
+ void take(const argument& t);
+ void take(const rank_<T>& other);
+ void sort();
+
+ T to_result() const;
+
+ protected:
+
+ std::vector<T> elts_;
+ bool is_sorted_;
+ unsigned k_; // 0 <= k_ < n
+ unsigned n_;
+ };
+
+
+ template <typename I> struct rank_< util::pix<I> >;
+
+
+ /*!
+ * \brief Meta accumulator for rank.
+ */
+ struct rank : public Meta_Accumulator< rank >
+ {
+ template <typename T>
+ struct with
+ {
+ typedef rank_<T> ret;
+ };
+ };
+
+
+
+
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename T>
+ inline
+ rank_<T>::rank_(unsigned k, unsigned n)
+ : k_(k),
+ n_(n),
+ is_sorted_(false)
+ {
+ mln_assertion(k_ < n_);
+ init();
+ }
+
+ template <typename T>
+ inline
+ void
+ rank_<T>::init()
+ {
+ }
+
+ template <typename T>
+ inline
+ void rank_<T>::take_as_init(const argument& t)
+ {
+ elts_.push_back(t);
+ is_sorted_ = false;
+ }
+
+ template <typename T>
+ inline
+ void rank_<T>::take(const argument& t)
+ {
+ elts_.push_back(t);
+ is_sorted_ = false;
+ }
+
+ template <typename T>
+ inline
+ void
+ rank_<T>::take(const rank_<T>& other)
+ {
+ elts_.insert(elts_.end(),
+ other.elts_.begin(),
+ other.elts_.end());
+ is_sorted_ = false;
+ }
+
+ template <typename T>
+ inline
+ T
+ rank_<T>::to_result() const
+ {
+ mln_assertion(n_ == elts_.size());
+ inplace(*this).sort();
+ return elts_[k_];
+ }
+
+ template <typename T>
+ inline
+ void
+ rank_<T>::sort()
+ {
+ if (!is_sorted_)
+ {
+ is_sorted_ = true;
+ std::sort(elts_.begin(), elts_.end());
+ }
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::accu
+
+} // end of namespace mln
+
+#include <mln/accu/rank_bool.hh>
+
+#endif // ! MLN_ACCU_RANK_HH
Index: trunk/milena/mln/accu/rank_bool.hh
===================================================================
--- trunk/milena/mln/accu/rank_bool.hh (revision 0)
+++ trunk/milena/mln/accu/rank_bool.hh (revision 1675)
@@ -0,0 +1,133 @@
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+//
+// 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_ACCU_RANK_BOOL_HH
+# define MLN_ACCU_RANK_BOOL_HH
+
+/*! \file mln/accu/rank_bool.hh
+ *
+ * \brief Define an rank accumulator.
+ */
+
+# include <vector>
+# include <mln/accu/internal/base.hh>
+# include <mln/core/concept/meta_accumulator.hh>
+# include <mln/trait/value_.hh>
+# include <mln/util/pix.hh>
+# include <mln/core/inplace.hh>
+
+namespace mln
+{
+
+ namespace accu
+ {
+
+ // Fwd declaration.
+ template <typename T> struct rank_;
+
+ /*! \brief rank accumulator class for boolean.
+ *
+ */
+ template <>
+ struct rank_<bool> : public mln::accu::internal::base_< bool, rank_<bool> >
+ {
+ typedef bool argument;
+ typedef bool result;
+
+ rank_(unsigned k, unsigned n);
+
+ void init();
+ void take_as_init(const argument& t);
+ void take(const argument& t);
+ void take(const rank_<bool>& other);
+
+ bool to_result() const;
+
+ protected:
+ unsigned nfalse_;
+ unsigned k_; // 0 <= k_ < n
+ unsigned n_;
+ };
+
+# ifndef MLN_INCLUDE_ONLY
+
+ inline
+ rank_<bool>::rank_(unsigned k, unsigned n)
+ : k_(k),
+ n_(n),
+ nfalse_(0)
+ {
+ mln_assertion(k_ < n_);
+ init();
+ }
+
+
+ inline
+ void
+ rank_<bool>::init()
+ {
+ }
+
+
+ inline
+ void rank_<bool>::take_as_init(const argument& t)
+ {
+ nfalse_ += !t;
+ }
+
+
+ inline
+ void rank_<bool>::take(const argument& t)
+ {
+ nfalse_ += !t;
+ }
+
+
+ inline
+ void
+ rank_<bool>::take(const rank_<bool>& other)
+ {
+ nfalse_ += other.nfalse_;
+ }
+
+
+ inline
+ bool
+ rank_<bool>::to_result() const
+ {
+ mln_assertion(nfalse_ <= n_);
+ return k_ >= nfalse_;
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::accu
+
+} // end of namespace mln
+
+
+#endif // ! MLN_ACCU_RANK_BOOL_HH
URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2008-01-18 Guillaume Duhamel <guillaume.duhamel(a)lrde.epita.fr>
Add tools for display in random color a labeled image.
* mln/draw/label.hh: New function which creates a color image from
a labeled image.
* tests/draw/label.cc: New test for that.
---
mln/draw/label.hh | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++
tests/draw/label.cc | 59 +++++++++++++++++++++++++
2 files changed, 180 insertions(+)
Index: trunk/milena/tests/draw/label.cc
===================================================================
--- trunk/milena/tests/draw/label.cc (revision 0)
+++ trunk/milena/tests/draw/label.cc (revision 1674)
@@ -0,0 +1,59 @@
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+//
+// 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.
+
+/*! \file tests/draw/label.cc
+ *
+ * \brief Tests on mln::draw::label.
+ */
+
+
+#include <mln/core/image2d.hh>
+#include <mln/debug/iota.hh>
+#include <mln/value/rgb8.hh>
+#include <mln/io/ppm/save.hh>
+#include <mln/draw/label.hh>
+#include <mln/literal/all.hh>
+
+#include <mln/core/image2d.hh>
+#include <mln/io/pbm/load.hh>
+#include <mln/core/neighb2d.hh>
+#include <mln/labeling/blobs.hh>
+#include <mln/display/save_and_show.hh>
+
+#include "tests/data.hh"
+
+
+int
+main()
+{
+ using namespace mln;
+ image2d<bool> input = io::pbm::load(MLN_IMG_DIR "/picasso.pbm");
+ unsigned n;
+ image2d<unsigned> ima = labeling::blobs(input, c4(), n);
+ image2d<value::rgb8> out = draw::label (ima, literal::black);
+ display::save_and_show(out, "display", 1);
+}
Index: trunk/milena/mln/draw/label.hh
===================================================================
--- trunk/milena/mln/draw/label.hh (revision 0)
+++ trunk/milena/mln/draw/label.hh (revision 1674)
@@ -0,0 +1,121 @@
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+//
+// 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_DRAW_LABEL_HH
+# define MLN_DRAW_LABEL_HH
+
+/*! \file mln/draw/label.hh
+ *
+ * \brief Draw a random color image from a label image.
+ */
+
+# include <mln/core/clone.hh>
+# include <mln/core/concept/image.hh>
+# include <mln/pw/image.hh>
+# include <mln/pw/cst.hh>
+
+# include <mln/trait/image_from_mesh.hh>
+# include <mln/core/image_if_value.hh>
+# include <mln/value/rgb8.hh>
+# include <mln/level/fill.hh>
+# include <mln/level/paste.hh>
+# include <mln/core/p_set.hh>
+# include <mln/metal/is_not.hh>
+# include <mln/core/image_if_value.hh>
+# include <mln/debug/println.hh>
+
+namespace mln
+{
+
+ namespace draw
+ {
+
+ /*! Draw a new image from a data (int, unsigned, int_u8, etc...) label image.
+ *
+ * \param[in] input The input image.
+ * \param[in] background The value to fill background output.
+ *
+ * \return A rgb8 image.
+ *
+ * \pre \p input has to be initialized.
+ *
+ */
+ template <typename I>
+ typename trait::image_from_mesh < mln_mesh(I), value::rgb8 >::ret
+ label(Image<I>& input, const value::rgb8& background);
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename I>
+ typename trait::image_from_mesh < mln_mesh(I), value::rgb8 >::ret
+ label(Image<I>& input, const value::rgb8& background)
+ {
+ typedef mln_value(I) V;
+ typedef mln_ch_value(I, value::rgb8) O;
+ typedef const mln::pset_if<mln_pset(I), mln::fun::eq_p2b_expr_<mln::pw::value_<I>, mln::pw::cst_<V> > > F;
+
+ I in = exact(input);
+ mln_precondition(in.has_data());
+ I ref = clone(in);
+ O out (in.domain ());
+
+ level::fill(out, background);
+ mln_piter(I) p (out.domain());
+ for_all (p)
+ {
+ if (ref(p) == 0)
+ continue;
+ value::rgb8 color;
+ while (1)
+ {
+ int r = rand() % 256;
+ int g = rand() % 256;
+ int b = rand() % 256;
+ if (r + g + b < 50)
+ continue;
+ color = value::rgb8(r, g, b);
+ break;
+ }
+ V val = ref(p);
+ image_if_value<I> ima_if = ref | val;
+ mln_piter(F) l (ima_if.domain());
+ for_all (l)
+ out(l) = color;
+ level::fill(ima_if, 0);
+ }
+ return out;
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::draw
+
+} // end of namespace mln
+
+
+#endif // ! MLN_DRAW_LINE_HH
URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2008-01-14 Matthieu Garrigues <garrigues(a)lrde.epita.fr>
Begin a process to extract arrays.
* sandbox/garrigues/factures/array_global.cc: New.
* sandbox/garrigues/factures/facture.pgm: An receipt image.
* sandbox/garrigues/factures: New.
---
array_global.cc | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 101 insertions(+)
Index: trunk/milena/sandbox/garrigues/factures/array_global.cc
===================================================================
--- trunk/milena/sandbox/garrigues/factures/array_global.cc (revision 0)
+++ trunk/milena/sandbox/garrigues/factures/array_global.cc (revision 1661)
@@ -0,0 +1,101 @@
+// Copyright (C) 2008 EPITA Research and Development Laboratory
+//
+// 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.
+
+#include <mln/core/image2d.hh>
+#include <mln/io/pgm/all.hh>
+#include <mln/io/pbm/all.hh>
+#include <mln/geom/resize.hh>
+#include <mln/display/all.hh>
+#include <mln/value/int_u8.hh>
+#include <mln/morpho/closing.hh>
+#include <mln/win/rectangle2d.hh>
+
+#include <mln/pw/all.hh>
+#include <mln/core/inplace.hh>
+#include <mln/level/stretch.hh>
+#include <mln/labeling/level.hh>
+#include <mln/core/neighb2d.hh>
+
+#include <mln/accu/all.hh>
+
+int main()
+{
+ using namespace mln;
+ using namespace mln;
+
+ typedef image2d<bool> ima2d_bool;
+ typedef image2d<unsigned> ima2d_unsigned;
+
+ image2d<value::int_u8> in;
+
+ io::pgm::load(in , "facture.pgm");
+
+ // Resize 50%.
+ image2d<value::int_u8> small = in; //geom::resize(in, 0.5);
+
+ // Binarisation.
+ ima2d_bool bin(small.domain());
+ level::paste(inplace(pw::value(small) > pw::cst(50) | small.domain()), bin);
+
+ // Labeling.
+ unsigned nlabels;
+ image2d<unsigned> labels = labeling::level(bin, true, c4(), nlabels);
+
+ // Get the caracteristics of the connected components.
+ std::vector< accu::pair_< accu::bbox<point2d>, accu::count_<point2d> > > caracteristics(nlabels);
+ mln_fwd_piter_(ima2d_unsigned) p(labels.domain());
+ for_all(p)
+ caracteristics[labels(p)].take(p);
+
+ // Filter the connected components.
+ std::vector<bool> is_array(nlabels);
+ for (int i = 0; i < nlabels; i++)
+ {
+ box_<point2d> b = caracteristics[i].to_result().first;
+ unsigned n = caracteristics[i].to_result().second;
+ float ratio;
+ if (b.pmax() == b.pmin() || n < 10)
+ {
+ is_array[i] = false;
+ continue;
+ }
+
+ if (0 != (b.pmax()[1] - b.pmin()[1]))
+ ratio = float(b.pmax()[0] - b.pmin()[0]) / (b.pmax()[1] - b.pmin()[1]);
+ else
+ ratio = float(b.pmax()[1] - b.pmin()[1]) / (b.pmax()[0] - b.pmin()[0]);
+
+ int area = (b.pmax()[0] - b.pmin()[0]) * (b.pmax()[1] - b.pmin()[1]);
+ is_array[i] = ratio < 0.1 || ratio > 10 || (float(n) / area) < 0.2;
+ }
+
+ // Clean the image.
+ for_all(p)
+ bin(p) = is_array[labels(p)];
+
+ io::pbm::save(bin, "array.pbm");
+}
Index: trunk/milena/sandbox/garrigues/factures/facture.pgm
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes on: trunk/milena/sandbox/garrigues/factures/facture.pgm
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2008-01-17 Matthieu Garrigues <garrigues(a)lrde.epita.fr>
Highlight hlives and vlines in extract_array.
* sandbox/garrigues/factures/extract_array_highlight.cc: New.
Highlight hlives and vlines in extract_array.
* sandbox/garrigues/factures/array_global.cc: Rename as...
* sandbox/garrigues/factures/extract_array.cc: this.
* mln/geom/resize.hh: Use mln_sum type.
---
mln/geom/resize.hh | 2
sandbox/garrigues/factures/extract_array.cc | 114 ++++++++++++
sandbox/garrigues/factures/extract_array_highlight.cc | 160 ++++++++++++++++++
3 files changed, 275 insertions(+), 1 deletion(-)
Index: trunk/milena/mln/geom/resize.hh
===================================================================
--- trunk/milena/mln/geom/resize.hh (revision 1668)
+++ trunk/milena/mln/geom/resize.hh (revision 1669)
@@ -101,7 +101,7 @@
resize_2d_(const I& input, const float ratio)
{
trace::entering("mln::geom::impl::resize_2d_");
- typedef mln_value(I) V;
+ typedef mln_sum(mln_value(I)) V;
std::size_t rows = input.bbox().len(0);
std::size_t cols = input.bbox().len(1);
Index: trunk/milena/sandbox/garrigues/factures/extract_array_highlight.cc
===================================================================
--- trunk/milena/sandbox/garrigues/factures/extract_array_highlight.cc (revision 0)
+++ trunk/milena/sandbox/garrigues/factures/extract_array_highlight.cc (revision 1669)
@@ -0,0 +1,160 @@
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
+//
+// 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.
+
+#include <mln/core/image2d.hh>
+#include <mln/io/pgm/all.hh>
+#include <mln/io/pbm/all.hh>
+#include <mln/geom/resize.hh>
+#include <mln/display/all.hh>
+#include <mln/value/int_u8.hh>
+#include <mln/morpho/opening.hh>
+#include <mln/win/hline2d.hh>
+#include <mln/win/vline2d.hh>
+#include <mln/win/disk2d.hh>
+
+#include <mln/pw/all.hh>
+#include <mln/core/inplace.hh>
+#include <mln/level/stretch.hh>
+#include <mln/level/median.hh>
+#include <mln/morpho/gradient.hh>
+
+#include <mln/labeling/level.hh>
+#include <mln/literal/all.hh>
+
+#include <mln/core/neighb2d.hh>
+
+#include <mln/accu/all.hh>
+
+#include <mln/draw/box.hh>
+
+using namespace mln;
+typedef image2d<bool> ima2d_bool;
+typedef image2d<unsigned> ima2d_unsigned;
+
+image2d<bool>
+filter_arrays(image2d<bool> in)
+{
+ // Labeling.
+ unsigned nlabels;
+ image2d<unsigned> labels = labeling::level(in, true, c4(), nlabels);
+
+ // Get the caracteristics of the connected components.
+ std::vector< accu::pair_< accu::bbox<point2d>, accu::count_<point2d> > > caracteristics(nlabels + 1);
+ mln_fwd_piter_(ima2d_unsigned) p(labels.domain());
+ for_all(p)
+ caracteristics[labels(p)].take(p);
+
+ // Filter each connected components.
+ std::vector<bool> is_array(nlabels);
+ for (int i = 0; i < nlabels; i++)
+ {
+ box_<point2d> b = caracteristics[i].to_result().first;
+ unsigned n = caracteristics[i].to_result().second;
+ float ratio;
+ if (b.pmax() == b.pmin() || n < 10)
+ {
+ is_array[i] = false;
+ continue;
+ }
+
+ if (0 != (b.pmax()[1] - b.pmin()[1]))
+ ratio = float(b.pmax()[0] - b.pmin()[0]) / (b.pmax()[1] - b.pmin()[1]);
+ else
+ ratio = float(b.pmax()[1] - b.pmin()[1]) / (b.pmax()[0] - b.pmin()[0]);
+
+ int area = (b.pmax()[0] - b.pmin()[0]) * (b.pmax()[1] - b.pmin()[1]);
+ is_array[i] = ratio < 0.1 || ratio > 10 || (float(n) / area) < 0.2;
+ }
+
+ // Clean the image.
+ image2d<bool> output(in.domain());
+ for_all(p)
+ output(p) = is_array[labels(p)];
+
+ return output;
+}
+
+image2d<value::rgb8>
+highlight_vlines(image2d<bool> in)
+{
+
+ image2d<bool> tmp;
+ tmp = filter_arrays(in);
+
+ // Opening.
+ tmp = morpho::opening(tmp, win::vline2d(5));
+
+ image2d<value::rgb8> output(tmp.domain());
+ mln_fwd_piter_(ima2d_unsigned) p(tmp.domain());
+ for_all(p)
+ if (tmp(p))
+ output(p) = literal::red;
+ else
+ output(p) = literal::black;
+
+ return output;
+}
+
+
+image2d<value::rgb8>
+highlight_hlines(image2d<bool> in)
+{
+
+ image2d<bool> tmp;
+ tmp = filter_arrays(in);
+ // Opening.
+ tmp = morpho::opening(tmp, win::hline2d(5));
+
+ image2d<value::rgb8> output(tmp.domain());
+ mln_fwd_piter_(ima2d_unsigned) p(tmp.domain());
+ for_all(p)
+ if (tmp(p))
+ output(p) = literal::green;
+ else
+ output(p) = literal::black;
+
+ return output;
+}
+
+
+int main()
+{
+
+ image2d<value::int_u8> in;
+
+ io::pgm::load(in , "facture.pgm");
+
+ // Binarisation.
+ ima2d_bool bin(in.domain());
+ level::paste(inplace(pw::value(in) > pw::cst(50) | in.domain()), bin);
+
+ image2d<value::rgb8> output_h = highlight_hlines(bin);
+ image2d<value::rgb8> output_v = highlight_vlines(bin);
+
+ image2d<value::rgb8> sum = output_v + output_h;
+ io::ppm::save(sum, "array.ppm");
+}
Index: trunk/milena/sandbox/garrigues/factures/extract_array.cc
===================================================================
--- trunk/milena/sandbox/garrigues/factures/extract_array.cc (revision 0)
+++ trunk/milena/sandbox/garrigues/factures/extract_array.cc (revision 1669)
@@ -0,0 +1,114 @@
+// Copyright (C) 2008 EPITA Research and Development Laboratory
+//
+// 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.
+
+#include <mln/core/image2d.hh>
+#include <mln/io/pgm/all.hh>
+#include <mln/io/pbm/all.hh>
+#include <mln/geom/resize.hh>
+#include <mln/display/all.hh>
+#include <mln/value/int_u8.hh>
+#include <mln/morpho/closing.hh>
+#include <mln/win/rectangle2d.hh>
+
+#include <mln/pw/all.hh>
+#include <mln/core/inplace.hh>
+#include <mln/level/stretch.hh>
+#include <mln/labeling/level.hh>
+#include <mln/literal/all.hh>
+
+#include <mln/core/neighb2d.hh>
+
+#include <mln/accu/all.hh>
+
+#include <mln/draw/box.hh>
+
+int main()
+{
+ using namespace mln;
+ using namespace mln;
+
+ typedef image2d<bool> ima2d_bool;
+ typedef image2d<unsigned> ima2d_unsigned;
+
+ image2d<value::int_u8> in;
+
+ io::pgm::load(in , "facture.pgm");
+
+ // Resize 50%.
+ image2d<value::int_u8> small = in; //geom::resize(in, 0.5);
+
+ // Binarisation.
+ ima2d_bool bin(small.domain());
+ level::paste(inplace(pw::value(small) > pw::cst(50) | small.domain()), bin);
+
+ // Labeling.
+ unsigned nlabels;
+ image2d<unsigned> labels = labeling::level(bin, true, c4(), nlabels);
+
+ // Get the caracteristics of the connected components.
+ std::vector< accu::pair_< accu::bbox<point2d>, accu::count_<point2d> > > caracteristics(nlabels);
+ mln_fwd_piter_(ima2d_unsigned) p(labels.domain());
+ for_all(p)
+ caracteristics[labels(p)].take(p);
+
+ // Filter each connected components.
+ std::vector<bool> is_array(nlabels);
+ for (int i = 0; i < nlabels; i++)
+ {
+ box_<point2d> b = caracteristics[i].to_result().first;
+ unsigned n = caracteristics[i].to_result().second;
+ float ratio;
+ if (b.pmax() == b.pmin() || n < 10)
+ {
+ is_array[i] = false;
+ continue;
+ }
+
+ if (0 != (b.pmax()[1] - b.pmin()[1]))
+ ratio = float(b.pmax()[0] - b.pmin()[0]) / (b.pmax()[1] - b.pmin()[1]);
+ else
+ ratio = float(b.pmax()[1] - b.pmin()[1]) / (b.pmax()[0] - b.pmin()[0]);
+
+ int area = (b.pmax()[0] - b.pmin()[0]) * (b.pmax()[1] - b.pmin()[1]);
+ is_array[i] = ratio < 0.14 || ratio > 7 || (float(n) / area) < 0.2;
+ }
+
+ // Clean the image.
+ image2d<value::rgb8> output(small.domain());
+ for_all(p)
+ if (is_array[labels(p)])
+ output(p) = literal::white;
+ else
+ output(p) = literal::black;
+
+ // Draw the bounding boxes.
+ for (int i = 0; i < nlabels; i++)
+ if (is_array[i])
+ draw::box(output, inplace(caracteristics[i].to_result().first), inplace(value::rgb8(literal::green)));
+
+ io::ppm::save(output, "array.ppm");
+}