$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
font_color.hh
1 // Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef SCRIBO_ESTIM_FONT_COLOR_HH
27 # define SCRIBO_ESTIM_FONT_COLOR_HH
28 
32 
33 # include <mln/core/image/image2d.hh>
34 # include <mln/core/alias/neighb2d.hh>
35 # include <mln/value/int_u8.hh>
36 # include <mln/labeling/compute.hh>
37 # include <mln/accu/stat/mean.hh>
38 # include <mln/accu/stat/median_h.hh>
39 # include <mln/value/int_u12.hh>
40 # include <mln/value/rgb8.hh>
41 
42 # include <scribo/estim/internal/compute_skeleton.hh>
43 
44 
45 namespace scribo
46 {
47 
48  namespace estim
49  {
50 
51  using namespace mln;
52 
54  //
55  template <typename I, typename J>
56  mln_value(I)
57  font_color(const Image<I>& text_ima, const Image<J>& bin_text_ima);
58 
59 
60 # ifndef MLN_INCLUDE_ONLY
61 
62  namespace impl
63  {
64 
65  template <typename I, typename J>
66  mln_value(I)
67  font_color_rgb(const Image<I>& text_ima_,
68  const Image<J>& bin_text_ima_)
69  {
70  mln_trace("scribo::estim::impl::font_color_rgb");
71 
72  typedef mln_value(I) V1;
73  typedef mln_value(J) V2;
74  //mlc_is(V1, value::rgb)::check();
75  mlc_is(V2, bool)::check();
76 
77  const I& text_ima = exact(text_ima_);
78  const J& bin_text_ima = exact(bin_text_ima_);
79 
80  mln_concrete(J) skel = internal::compute_skeleton(bin_text_ima);
81 
82  value::int_u8 nlabels = 0;
83  mln_ch_value(I,value::int_u8)
84  lbl = labeling::blobs(skel, c8(), nlabels);
85 
86  mln::util::array<algebra::vec<3u, float> > res =
87  labeling::compute(accu::meta::stat::mean(), text_ima, lbl, nlabels);
88 
89  accu::stat::median_h<value::int_u12> m_red;
90  accu::stat::median_h<value::int_u12> m_green;
91  accu::stat::median_h<value::int_u12> m_blue;
92  m_red.init();
93  m_green.init();
94  m_blue.init();
95 
96  mln_value(I) med_rgb;
97 
98  for (unsigned int i = 1; i <= nlabels; ++i)
99  {
100  m_red.take(res[i][0]);
101  m_green.take(res[i][1]);
102  m_blue.take(res[i][2]);
103  }
104 
105  med_rgb.red() = m_red.to_result();
106  med_rgb.green() = m_green.to_result();
107  med_rgb.blue() = m_blue.to_result();
108 
109  return med_rgb;
110  }
111 
112 
113  template <typename I, typename J>
114  mln_value(I)
115  font_color_grayscale(const Image<I>& text_ima_,
116  const Image<J>& bin_text_ima_)
117  {
118  mln_trace("scribo::estim::impl::font_color_grayscale");
119 
120  typedef mln_value(I) V1;
121  typedef mln_value(J) V2;
122  //mlc_is(V1, value::rgb)::check();
123  mlc_is(V2, bool)::check();
124 
125  const I& text_ima = exact(text_ima_);
126  const J& bin_text_ima = exact(bin_text_ima_);
127 
128  mln_concrete(J) skel = internal::compute_skeleton(bin_text_ima);
129 
130  value::int_u8 nlabels = 0;
131  mln_ch_value(J,value::int_u8) lbl = labeling::blobs(skel, c8(), nlabels);
132 
133  mln::util::array<algebra::vec<3u, float> > res =
134  labeling::compute(accu::meta::stat::mean(), text_ima, lbl, nlabels);
135 
136  accu::stat::median_h<value::int_u12> m_val;
137  m_val.init();
138 
139  mln_value(I) med_val;
140  for (unsigned int i = 1; i <= nlabels; ++i)
141  m_val.take(res[i][0]);
142 
143  med_val = m_val.to_result();
144 
145  return med_val;
146  }
147 
148  } // end of namespace scribo::estim::impl
149 
150 
151  // DISPATCH
152 
153  namespace internal
154  {
155 
156  template <typename I, typename J>
157  mln_value(I)
158  font_color_dispatch(const value::rgb8&,
159  const Image<I>& text_ima, const Image<J>& bin_text_ima)
160  {
161  return impl::font_color_rgb(exact(text_ima), exact(bin_text_ima));
162  }
163 
164 
165  template <typename V, typename I, typename J>
166  mln_value(I)
167  font_color_dispatch(const V&,
168  const Image<I>& text_ima, const Image<J>& bin_text_ima)
169  {
170  return impl::font_color_grayscale(exact(text_ima), exact(bin_text_ima));
171  }
172 
173 
174  template <typename I, typename J>
175  mln_value(I)
176  font_color_dispatch(const Image<I>& text_ima, const Image<J>& bin_text_ima)
177  {
178  return font_color_dispatch(mln_value(I)(), text_ima, bin_text_ima);
179  }
180 
181  } // end of namespace scribo::estim::internal
182 
183 
184  // FACADE
185 
186  template <typename I, typename J>
187  mln_value(I)
188  font_color(const Image<I>& text_ima, const Image<J>& bin_text_ima)
189  {
190  mln_trace("scribo::estim::font_color");
191 
192  mln_value(I) output = internal::font_color_dispatch(text_ima, bin_text_ima);
193 
194  return output;
195  }
196 
197 # endif // ! MLN_INCLUDE_ONLY
198 
199  } // end of namespace scribo::estim
200 
201 } // end of namespace scribo
202 
203 # endif // ! SCRIBO_ESTIM_FONT_COLOR_HH