URL:
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena/sandbox
ChangeLog:
2008-10-29 Matthieu Garrigues <garrigues(a)lrde.epita.fr>
Add simple point computation to ocr preprocessing.
* garrigues/ocr/simple_point.hh: New ,Tell if a point is simple or
not. A point of an object is simple if in its c8 neiborhood, there is
exactly one connected component of the object, and only one connected
component of the background
Examples : ( | == object, - = background)
- - |
| P | Here P is simple in the c4 and c8 case.
| | |
- | -
| P | Here P is never simple.
| | |
* garrigues/ocr/simple_point.cc: New. A simple test that shows all the
simple points of a binary image.
---
simple_point.cc | 39 +++++++++++++
simple_point.hh | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 204 insertions(+)
Index: branches/cleanup-2008/milena/sandbox/garrigues/ocr/simple_point.hh
===================================================================
--- branches/cleanup-2008/milena/sandbox/garrigues/ocr/simple_point.hh (revision 0)
+++ branches/cleanup-2008/milena/sandbox/garrigues/ocr/simple_point.hh (revision 2728)
@@ -0,0 +1,165 @@
+// 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_SIMPLE_POINT_HH
+# define MLN_SIMPLE_POINT_HH
+
+/*! \file simple_point.hh
+ *
+ * \brief simple_point tell if a point is simple or not (Cf
+ * bertrand.07.chap).
+ *
+ */
+
+#include <mln/core/alias/point2d.hh>
+#include <mln/core/image/image2d.hh>
+#include <mln/core/alias/neighb2d.hh>
+
+namespace mln
+{
+
+/*! Tell if a point is simple or not. A point of an object is simple
+ * if in its c8 neiborhood, there is exactly one connected component of the
+ * object, and only one connected component of the background
+ * Examples : ( | == object, - = background)
+ *
+ * - - |
+ * | P | Here p is simple in the c4 and c8 case.
+ * | | |
+ *
+ * - | -
+ * | P | Here p is never simple.
+ * | | |
+ *
+ */
+
+ bool simple_point(const image2d<bool>& ima, const neighb2d& nbh, const
point2d& p);
+
+ unsigned nb_connexity2d(const image2d<bool>& ima, const neighb2d& nbh,
const point2d& p);
+
+# ifndef MLN_INCLUDE_ONLY
+
+ static const unsigned char nb_connexity_c8[256] =
+ {
+ 0, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1,
+ 1, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1,
+ 1, 2, 2, 2, 2, 3, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1,
+ 2, 3, 2, 2, 2, 3, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1,
+
+ 1, 2, 2, 2, 2, 3, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1,
+ 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 2, 2, 2, 2, 3, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1,
+ 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+
+ 1, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2,
+ 1, 2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1,
+ 2, 3, 3, 3, 3, 4, 3, 3, 2, 2, 2, 2, 3, 3,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 2, 2, 2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 1,
+ 1, 2, 2, 2, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1
+ };
+
+ neighb2d int_to_neighb(unsigned i)
+ {
+ if (i == 8)
+ return c8();
+ if (i == 4)
+ return c4();
+ mln_assertion(0);
+ return c4();
+ }
+
+ unsigned complement_neighb(unsigned i)
+ {
+ if (i == 8)
+ return 4;
+ if (i == 4)
+ return 8;
+ mln_assertion(0);
+ return 0;
+ }
+
+ unsigned nb_connexity2d(const image2d<bool>& ima, unsigned nbh, const
point2d& p, bool complemented)
+ {
+ unsigned res = 0;
+
+ mln_fwd_niter_(neighb2d) n(c8() , p);
+ for_all(n)
+ {
+ res = (res << 1);
+ if (ima(n) == complemented)
+ res = res | 1;
+ }
+
+ if (nbh == 8)
+ return nb_connexity_c8[res];
+ else
+ {
+ mln_assertion(nbh == 4);
+ return nb_connexity_c4[res];
+ }
+ }
+
+ bool simple_point2d(const image2d<bool>& ima, unsigned nbh, const
point2d& p)
+ {
+ mln_assertion(nbh == 4 || nbh == 8);
+
+ return nb_connexity2d(ima, nbh, p, true) == 1 &&
+ nb_connexity2d(ima, complement_neighb(nbh), p, false) == 1;
+ }
+
+# endif // MLN_INCLUDE_ONLY
+
+} // end of namespace mln
+
+#endif // ! MLN_SIMPLE_POINT_HH
Index: branches/cleanup-2008/milena/sandbox/garrigues/ocr/simple_point.cc
===================================================================
--- branches/cleanup-2008/milena/sandbox/garrigues/ocr/simple_point.cc (revision 0)
+++ branches/cleanup-2008/milena/sandbox/garrigues/ocr/simple_point.cc (revision 2728)
@@ -0,0 +1,39 @@
+# include <mln/core/image/image2d.hh>
+# include <mln/io/pbm/load.hh>
+# include <mln/io/pbm/save.hh>
+# include <mln/level/fill.hh>
+
+# include "simple_point.hh"
+
+
+void usage(char* argv[])
+{
+ std::cerr << "usage: " << argv[0] << " input.pbm
output.pbm" << std::endl;
+ abort();
+}
+
+
+
+int main(int argc, char* argv[])
+{
+ using namespace mln;
+ using value::int_u8;
+
+ if (argc != 3)
+ usage(argv);
+
+ typedef image2d<bool> I;
+ I input;
+ io::pbm::load(input, argv[1]);
+
+ I output;
+ initialize(output, input);
+ level::fill(output, false);
+
+ mln_piter_(I) p(input.domain());
+ for_all(p)
+ if (input(p) && simple_point2d(input, 8, p))
+ output(p) = true;
+
+ io::pbm::save(output, argv[2]);
+}