$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
font_boldness.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_BOLDNESS_HH
27 # define SCRIBO_ESTIM_FONT_BOLDNESS_HH
28 
32 
33 # include <mln/core/image/image2d.hh>
34 # include <mln/core/alias/neighb2d.hh>
35 
36 # include <mln/labeling/blobs.hh>
37 # include <mln/accu/stat/mean.hh>
38 # include <mln/value/next.hh>
39 
40 # include <scribo/core/macros.hh>
41 # include <scribo/core/def/lbl_type.hh>
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>
56  float
57  font_boldness(const Image<I>& text_ima);
58 
59 
60 # ifndef MLN_INCLUDE_ONLY
61 
62  namespace internal
63  {
64 
65 
66  template <typename I, typename J>
67  float
68  boldness_from_lbl(const Image<I>& ima_lbl_,
69  const Image<J>& skel_)
70  {
71  mln_trace("scribo::estim::internal::boldness_from_lbl");
72 
73  const I& ima_lbl = exact(ima_lbl_);
74  const J& skel = exact(skel_);
75  mln_precondition(ima_lbl.is_valid());
76  mln_precondition(skel.is_valid());
77 
78  mln_piter(I) p(ima_lbl.domain());
79  int skel_p = 0, ima_p = 0;
80 
81  for_all(p)
82  {
83  if (skel(p))
84  ++skel_p;
85 
86  if (ima_lbl(p))
87  ++ima_p;
88  }
89 
90  float res = ima_p / (float) skel_p;
91 
92  return res;
93  }
94 
95 
96  // FIXME: this version is invalid!!! It supposes that image and
97  // skeleton labeling associate the same id to a component and
98  // its corresponding skeleton. This is wrong!
99  template <typename I, typename J>
100  float
101  boldness_from_lbl(const Image<I>& ima_lbl_,
102  const mln_value(I)& nlabels,
103  const Image<J>& skel_lbl_)
104  {
105  mln_trace("scribo::estim::internal::boldness_from_lbl");
106 
107  const I& ima_lbl = exact(ima_lbl_);
108  const J& skel_lbl = exact(skel_lbl_);
109  mln_precondition(ima_lbl.is_valid());
110  mln_precondition(skel_lbl.is_valid());
111 
112  mln_piter(I) p(ima_lbl.domain());
113  std::vector<int> skel_p(value::next(nlabels), 0);
114  std::vector<int> ima_p(value::next(nlabels), 0);
115 
116  for_all(p)
117  {
118  int val = skel_lbl(p);
119 
120  if (val)
121  ++skel_p[val];
122 
123  val = ima_lbl(p);
124 
125  if (val)
126  ++ima_p[val];
127  }
128 
130  for_all_ncomponents(i, nlabels)
131  mean.take(ima_p[i] / skel_p[i]);
132 
133  // double variance = 0.;
134  // for (unsigned int i = 0; i < nlabels; ++i)
135  // variance += ((ima_p[i] / skel_p[i]) - mean.to_result()) * ((ima_p[i] / skel_p[i]) - mean.to_result());
136 
137  // variance /= nlabels;
138 
139  return mean.to_result();
140  }
141 
142  } // end of namespace scribo::estim::internal
143 
144 
145  // IMPLEMENTATIONS
146 
147  namespace impl
148  {
149 
150  namespace generic
151  {
152 
153  template <typename I>
154  float
155  font_boldness(const Image<I>& ima_)
156  {
157  mln_trace("scribo::estim::impl::generic::font_boldness");
158 
159  const I& ima = exact(ima_);
160  mln_precondition(ima.is_valid());
161  mlc_is(mln_value(I), bool)::check();
162 
163  typedef scribo::def::lbl_type V;
164  typedef mln_ch_value(I,V) L;
165 
166  mln_concrete(I) skel = internal::compute_skeleton(ima);
167 
168  V nslabels;
169  L slbl = labeling::blobs(skel, c8(), nslabels);
170 
171  V nlabels;
172  L lbl = labeling::blobs(ima, c8(), nlabels);
173 
174  float
175  output = internal::boldness_from_lbl(lbl, nlabels, slbl);
176 
177  return output;
178  }
179 
180  } // end of namespace scribo::estim::generic::impl
181 
182  } // end of namespace scribo::estim::impl
183 
184 
185 
186  // FACADE
187 
188  template <typename I>
189  float
190  font_boldness(const Image<I>& ima)
191  {
192  mln_trace("scribo::estim::font_boldness");
193 
194  mln_precondition(exact(ima).is_valid());
195  mlc_is(mln_value(I), bool)::check();
196 
197  float output = impl::generic::font_boldness(ima);
198 
199  return output;
200  }
201 
202 # endif // ! MLN_INCLUDE_ONLY
203 
204  } // end of namespace scribo::estim
205 
206 } // end of namespace scribo
207 
208 # endif // ! SCRIBO_ESTIM_FONT_BOLDNESS_HH
209 
210 
211