first_pass_functor.hh 4.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena 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 Olena.  If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project 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 SCRIBO_BINARIZATION_INTERNAL_FIRST_PASS_FUNCTOR_HH
# define SCRIBO_BINARIZATION_INTERNAL_FIRST_PASS_FUNCTOR_HH

Guillaume Lazzara's avatar
Guillaume Lazzara committed
29
30
31
# include <mln/core/image/image2d.hh>
# include <mln/core/alias/neighb2d.hh>
# include <mln/extension/fill.hh>
32
# include <mln/value/int_u8.hh>
Guillaume Lazzara's avatar
Guillaume Lazzara committed
33
# include <mln/data/fill.hh>
34

35
# include <scribo/binarization/sauvola_threshold_image.hh>
36
37
38
39
40
41
42
43
44
45
46
47
48


// #include <mln/border/adjust.hh>

namespace scribo
{

  namespace binarization
  {

    namespace internal
    {

49
50
51
52
53
54
55
# ifdef SCRIBO_SAUVOLA_DEBUG
      // Global debug images.
      extern image2d<value::int_u8> debug_k;
      extern image2d<float> debug_s_n;
      extern image2d<float> debug_k_l;
# endif // ! SCRIBO_SAUVOLA_DEBUG

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
      using namespace mln;


      unsigned my_find_root(image2d<unsigned>& parent, unsigned x)
      {
	if (parent.element(x) == x)
	  return x;
	return parent.element(x) = my_find_root(parent,
						parent.element(x));
      }


      template <typename I>
      struct first_pass_functor
      {
	const I& input;
	mln_fwd_pixter(const I) pxl;
	double res;
	image2d<unsigned> parent;
	image2d<unsigned> card;
	image2d<bool> msk;
	image2d<value::int_u8> t_sub;

	unsigned n_nbhs;
	util::array<int> dp;

82
	double K_;
83

84
	first_pass_functor(const I& input, double K)
85
	  : input(input),
86
87
	    pxl(input),
	    K_(K)
88
89
90
91
92
93
94
	{
	  res = 0;
	  pxl.start();

	  initialize(t_sub, input);
	  initialize(parent, input);
	  initialize(msk, input);
95
96
97
98
99
100
101

# ifdef SCRIBO_SAUVOLA_DEBUG
	  initialize(debug_k, input);
	  initialize(debug_s_n, input);
	  initialize(debug_k_l, input);
# endif // ! SCRIBO_SAUVOLA_DEBUG

Guillaume Lazzara's avatar
Guillaume Lazzara committed
102
	  mln::extension::fill(msk, false);
103
104
105
106
107
108
109
110
111
112
113
114
115
116

	  initialize(card, input);
	  data::fill(card, 1);

	  dp = negative_offsets_wrt(input, c4());
	  n_nbhs = dp.nelements();
	}

	void exec(double mean, double stddev)
	{
 	  mln_precondition(pxl.is_valid());

	  unsigned p = pxl.offset();

117
# ifdef SCRIBO_SAUVOLA_DEBUG
118
119
120
121
122
123
124
125
126
          value::int_u8 t_p;
	  convert::from_to(
	    sauvola_threshold_formula(mean, stddev,
				      K_,
				      SCRIBO_DEFAULT_SAUVOLA_R,
				      debug_k.element(p),
				      debug_s_n.element(p),
				      debug_k_l.element(p)),
	    t_p);
127
# else
128
          value::int_u8 t_p;
Guillaume Lazzara's avatar
Guillaume Lazzara committed
129
130
131
	  mln::convert::from_to(sauvola_threshold_formula(mean, stddev,
							  K_,
							  SCRIBO_DEFAULT_SAUVOLA_R),
132
			   t_p);
133
# endif // SCRIBO_SAUVOLA_DEBUG
134

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

	  msk.element(p) = input.element(p) < t_p;
	  t_sub.element(p) = t_p;
	  if (! msk.element(p))
	  {
	    pxl.next();
	    return;
	  }
	  parent.element(p) = p;
	  for (unsigned i = 0; i < n_nbhs; ++i)
	  {
	    unsigned n = p + dp[i];
	    if (! msk.element(n))
	      continue;
	    unsigned r = my_find_root(parent, n);
	    if (r != p)
	    {
	      parent.element(r) = p;
	      card.element(p) += card.element(r);
	    }
	  }

 	  pxl.next(); // next pixel
	}

	void finalize()
	{
 	  mln_assertion(! pxl.is_valid());
	}
      };


    } // end of namespace scribo::binarization::internal

  } // end of namespace scribo::binarization

} // end of namespace scribo

#endif // SCRIBO_BINARIZATION_INTERNAL_FIRST_PASS_FUNCTOR_HH