$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
global_threshold_auto.hh
1 // Copyright (C) 2009, 2010 EPITA Research and Development Laboratory
2 // (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef SCRIBO_BINARIZATION_GLOBAL_THRESHOLD_AUTO_HH
28 # define SCRIBO_BINARIZATION_GLOBAL_THRESHOLD_AUTO_HH
29 
35 
36 # include <mln/core/image/image1d.hh>
37 # include <mln/core/alias/neighb1d.hh>
38 
39 # include <mln/core/image/image2d.hh>
40 # include <mln/core/alias/neighb2d.hh>
41 
42 # include <mln/core/routine/duplicate.hh>
43 # include <mln/core/image/dmorph/image_if.hh>
44 # include <mln/pw/all.hh>
45 
46 # include <mln/histo/compute.hh>
47 # include <mln/debug/histo.hh>
48 # include <mln/convert/from_to.hh>
49 
50 # include <mln/morpho/elementary/gradient_external.hh>
51 # include <mln/morpho/closing/height.hh>
52 # include <mln/morpho/closing/volume.hh>
53 # include <mln/morpho/watershed/flooding.hh>
54 
55 # include <mln/linear/gaussian_1d.hh>
56 # include <mln/labeling/regional_minima.hh>
57 # include <mln/labeling/compute.hh>
58 # include <mln/value/label_8.hh>
59 # include <mln/accu/center.hh>
60 
61 
62 
63 namespace scribo
64 {
65 
66  namespace binarization
67  {
68 
69  using namespace mln;
70 
72 
79  template <typename I>
80  mln_ch_value(I, bool)
81  global_threshold_auto(const Image<I>& input);
82 
83 
84 
85 # ifndef MLN_INCLUDE_ONLY
86 
87  template <typename I>
88  inline
89  mln_ch_value(I, bool)
90  global_threshold_auto(const Image<I>& input_)
91  {
92  mln_trace("scribo::binarization::global_threshold_auto");
93 
94  const I& input = exact(input_);
95  mln_precondition(input.is_valid());
96 
97  const float sigma = 5; // FIXME: hard-coded!
98 
99  typedef mln_value(I) V;
100  histo::array<V> h;
101 
102  {
103  mln_concrete(I) g;
104  g = morpho::elementary::gradient_external(input, c4());
105  g = morpho::closing::height(g, c4(), 5); // FIXME: hard-coded!
106 
107  unsigned nbasins;
108  mln_ch_value(I, unsigned) w = morpho::watershed::flooding(g, c4(), nbasins);
109  h = histo::compute(input | (pw::value(w) == pw::cst(0u)));
110  }
111 
112 
113  util::array<point1d> c;
114  value::label_8 n;
115 
116  {
117  image1d<unsigned> h_, hs_;
119 
120  convert::from_to(h, h_);
121  hs_ = linear::gaussian_1d(h_, sigma, 0);
122  l = labeling::regional_minima(hs_, c2(), n);
123 
124  if (n < 3u)
125  {
126  std::cerr << "This method has not worked properly!" << std::endl;
127 
128  debug::histo(h, "tmp_h.txt");
129 
130  std::ofstream file("tmp_hs.txt");
131  mln_piter(box1d) p(h_.domain());
132  for_all(p)
133  file << p.ind() << ' ' << hs_(p) << std::endl;
134  file.close();
135 
136  std::abort();
137  }
138 
139  accu::center<point1d, point1d> a;
140  c = labeling::compute(a, l, n);
141  c[0] = point1d(0); // Force a neutral value for the non-label value (0).
142  }
143 
144  unsigned threshold;
145 
146  {
147  std::vector<int> v;
148  for (unsigned i = 0; i <= n; ++i)
149  v.push_back(c[i].ind());
150  std::sort(v.begin(), v.end());
151  threshold = v[n.prev()];
152  }
153 
154  mln_ch_value(I, bool) output;
155  output = duplicate((pw::value(input) < pw::cst(threshold)) | input.domain());
156 
157  return output;
158  }
159 
160 # endif // !MLN_INCLUDE_ONLY
161 
162  } // end of namespace scribo::binarization
163 
164 } // end of namespace scribo
165 
166 
167 #endif // ! SCRIBO_BINARIZATION_GLOBAL_THRESHOLD_AUTO_HH