lines_pattern.hh 7.97 KB
Newer Older
Guillaume Lazzara's avatar
Guillaume Lazzara committed
1
2
// Copyright (C) 2009, 2010 EPITA Research and Development Laboratory
// (LRDE)
Guillaume Lazzara's avatar
Guillaume Lazzara committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// 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.

27
28
#ifndef SCRIBO_PRIMITIVE_EXTRACT_LINES_PATTERN_HH
# define SCRIBO_PRIMITIVE_EXTRACT_LINES_PATTERN_HH
Guillaume Lazzara's avatar
Guillaume Lazzara committed
29
30
31
32

/// \file
///
/// Extract lines matching a specific pattern.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
33
34
///
/// \todo Make a more generic canvas.
Guillaume Lazzara's avatar
Guillaume Lazzara committed
35
36
37
38
39

# include <mln/core/concept/image.hh>
# include <mln/core/alias/window2d.hh>
# include <mln/core/routine/duplicate.hh>

Guillaume Lazzara's avatar
Guillaume Lazzara committed
40
41
# include <mln/extension/adjust_fill.hh>

Guillaume Lazzara's avatar
Guillaume Lazzara committed
42
43
44
45
46
47
48
49
50
# include <mln/accu/transform_line.hh>
# include <mln/accu/count_value.hh>

# include <mln/data/paste.hh>


namespace scribo
{

51
  namespace primitive
Guillaume Lazzara's avatar
Guillaume Lazzara committed
52
53
  {

54
    namespace extract
Guillaume Lazzara's avatar
Guillaume Lazzara committed
55
56
57
58
59
60
61
62
63
64
65
66
67
    {

      using namespace mln;


      /// Extract lines with a specific pattern
      ///
      /// \param[in] input_ A binary image.
      /// \param[in] length The minimum line length.
      /// \param[in] dir    The direction of the lines.
      /// \param[in] win_   A window corresponding to the line pattern.
      ///
      /// \return A image with lines of direction \p dir.
68
      ///
Guillaume Lazzara's avatar
Guillaume Lazzara committed
69
70
71
72
73
74
75
76
77
      //
      template <typename I, typename W>
      mln_concrete(I)
      lines_pattern(const Image<I>& input_, unsigned length,
		    unsigned dir, const Window<W>& win_);


# ifndef MLN_INCLUDE_ONLY

Guillaume Lazzara's avatar
Guillaume Lazzara committed
78
79
80
81

      // Implementations

      namespace impl
Guillaume Lazzara's avatar
Guillaume Lazzara committed
82
83
      {

Guillaume Lazzara's avatar
Guillaume Lazzara committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
	namespace generic
	{

	  template <typename I, typename W>
	  mln_concrete(I)
	  lines_pattern(const Image<I>& input_, unsigned length,
			unsigned dir, const Window<W>& win_)
	  {
	    trace::entering("scribo::primitive::extract::impl::generic::lines_pattern");

	    const I& input = exact(input_);
	    const W& win = exact(win_);
	    mlc_is(mln_value(I), bool)::check();
	    mln_precondition(input.is_valid());

	    // Adjusting extension.
	    extension::adjust_fill(input, length / 2, 0);

	    accu::count_value<bool> accu(true);
	    mln_ch_value(I,unsigned)
	      tmp = accu::transform_line(accu, input, length, dir);

	    mln_concrete(I) output;
	    initialize(output, input);

	    mln_piter(I) p(input.domain());
	    mln_qiter(window2d) q(win, p);
	    bool is_foreground;
	    for_all(p)
	    {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
114

Guillaume Lazzara's avatar
Guillaume Lazzara committed
115
116
117
118
119
120
121
122
	      // If the foreground part of the pattern has more than 20%
	      // of background pixels, the current pixel is considered as
	      // background pixel.
	      if (length - tmp(p) > unsigned(0.2f * length) + 1)
	      {
		output(p) = false;
		continue;
	      }
Guillaume Lazzara's avatar
Guillaume Lazzara committed
123

Guillaume Lazzara's avatar
Guillaume Lazzara committed
124
125
126
127
128
129
130
131
132
133
	      // If the background parts of the pattern have exactly or
	      // less than 95% of background pixels, the current pixel is
	      // considered as part of the background.
	      is_foreground = true;
	      for_all(q)
		if ((length - tmp(q)) < unsigned(length * 0.95f) + 1)
		{
		  is_foreground = false;
		  break;
		}
Guillaume Lazzara's avatar
Guillaume Lazzara committed
134

Guillaume Lazzara's avatar
Guillaume Lazzara committed
135
136
137
138
139
140
141
142
	      output(p) = is_foreground;
	    }

	    trace::exiting("scribo::primitive::extract::impl::generic::lines_pattern");
	    return output;
	  }

	} // end of namespace scribo::primitive::extract::impl::generic
Guillaume Lazzara's avatar
Guillaume Lazzara committed
143

Guillaume Lazzara's avatar
Guillaume Lazzara committed
144
145
146
147
148
149


	template <typename I, typename W>
	mln_concrete(I)
	lines_pattern_fast(const Image<I>& input_, unsigned length,
			   unsigned dir, const Window<W>& win_)
Guillaume Lazzara's avatar
Guillaume Lazzara committed
150
	{
Guillaume Lazzara's avatar
Guillaume Lazzara committed
151
152
153
154
155
156
157
158
159
	  trace::entering("scribo::primitive::extract::impl::lines_pattern_fast");

	  const I& input = exact(input_);
	  const W& win = exact(win_);
	  mlc_is(mln_value(I), bool)::check();
	  mln_precondition(input.is_valid());

	  // Adjusting extension.
	  extension::adjust_fill(input, length / 2, 0);
160

Guillaume Lazzara's avatar
Guillaume Lazzara committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
	  accu::count_value<bool> accu(true);
	  mln_ch_value(I,unsigned)
	    tmp = accu::transform_line(accu, input, length, dir);

	  mln_concrete(I) output;
	  initialize(output, input);

	  util::array<int>
	    q_arr = offsets_wrt(output, win);

	  bool is_foreground;
	  unsigned ncols = geom::ncols(output);
	  unsigned hit_ratio = 0.2f * length + 1;
	  unsigned miss_ratio = 0.95f * length + 1;

	  mln_box_runstart_piter(I) p(output.domain());
	  for_all(p)
Guillaume Lazzara's avatar
Guillaume Lazzara committed
178
	  {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
179
180
181
182
183
	    unsigned pi = output.index_of_point(p);
	    unsigned *tmp_ptr = &tmp.element(pi);
	    unsigned *end_ptr = tmp_ptr + ncols;

	    mln_value(I) *out_ptr = &output.element(pi);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
184

Guillaume Lazzara's avatar
Guillaume Lazzara committed
185
	    for (; tmp_ptr < end_ptr; ++out_ptr, ++tmp_ptr)
186
	    {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

	      // If the foreground part of the pattern has more than 20%
	      // of background pixels, the current pixel is considered as
	      // background pixel.
	      if (length - *tmp_ptr > hit_ratio)
	      {
		*out_ptr = false;
		continue;
	      }

	      // If the background parts of the pattern have exactly or
	      // less than 95% of background pixels, the current pixel is
	      // considered as part of the background.
	      is_foreground = true;
	      for (unsigned i = 0; i < q_arr.size(); ++i)
		if ((length - *(tmp_ptr + q_arr[i])) < miss_ratio)
		{
		  is_foreground = false;
		  break;
		}

	      *out_ptr = is_foreground;
209
	    }
Guillaume Lazzara's avatar
Guillaume Lazzara committed
210
211
212
213
214
215
216
217
218
219
220
	  }

	  trace::exiting("scribo::primitive::extract::impl::lines_pattern_fast");
	  return output;
	}

      } // end of namespace scribo::primitive::extract::impl



      // Dispatch
Guillaume Lazzara's avatar
Guillaume Lazzara committed
221

Guillaume Lazzara's avatar
Guillaume Lazzara committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
      namespace internal
      {

	template <typename I, typename W>
	mln_concrete(I)
	lines_pattern_dispatch(mln::trait::image::value_storage::any,
			       mln::trait::image::value_access::any,
			       mln::trait::image::ext_domain::any,
			       const Image<I>& input, unsigned length,
			       unsigned dir, const Window<W>& win)
	{
	  return impl::generic::lines_pattern(input, length, dir, win);
	}


	template <typename I, typename W>
	mln_concrete(I)
	lines_pattern_dispatch(mln::trait::image::value_storage::one_block,
			       mln::trait::image::value_access::direct,
			       mln::trait::image::ext_domain::some,
			       const Image<I>& input, unsigned length,
			       unsigned dir, const Window<W>& win)
	{
	  return impl::lines_pattern_fast(input, length, dir, win);
Guillaume Lazzara's avatar
Guillaume Lazzara committed
246
247
	}

Guillaume Lazzara's avatar
Guillaume Lazzara committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

	template <typename I, typename W>
	mln_concrete(I)
	lines_pattern_dispatch(const Image<I>& input, unsigned length,
			       unsigned dir, const Window<W>& win)
	{
	  return lines_pattern_dispatch(mln_trait_image_value_storage(I)(),
					mln_trait_image_value_access(I)(),
					mln_trait_image_ext_domain(I)(),
					input,
					length,
					dir, win);
	}

      } // end of namespace scribo::primitive::extract::internal


      // Facade

      template <typename I, typename W>
      mln_concrete(I)
      lines_pattern(const Image<I>& input, unsigned length,
		    unsigned dir, const Window<W>& win)
      {
	trace::entering("scribo::primitive::extract::lines_pattern");

	mlc_is(mln_value(I), bool)::check();
	mln_precondition(exact(input).is_valid());
	mln_precondition(exact(win).is_valid());
	mln_precondition(length != 0);
	mln_precondition(dir == 0 || dir == 1);

	mln_concrete(I)
	  output = internal::lines_pattern_dispatch(input, length, dir, win);

Guillaume Lazzara's avatar
Guillaume Lazzara committed
283
	trace::exiting("scribo::primitive::extract::lines_pattern");
Guillaume Lazzara's avatar
Guillaume Lazzara committed
284
285
286
	return output;
      }

Guillaume Lazzara's avatar
Guillaume Lazzara committed
287

Guillaume Lazzara's avatar
Guillaume Lazzara committed
288
289
# endif // ! MLN_INCLUDE_ONLY

290
    } // end of namespace scribo::primitive::extract
Guillaume Lazzara's avatar
Guillaume Lazzara committed
291

292
  } // end of namespace scribo::primitive
Guillaume Lazzara's avatar
Guillaume Lazzara committed
293

294
} // end of namespace scribo
Guillaume Lazzara's avatar
Guillaume Lazzara committed
295
296


297
#endif // ! SCRIBO_PRIMITIVE_EXTRACT_LINES_PATTERN_HH