$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
deskew_crop.hh
1 // Copyright (C) 2010, 2011, 2013 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_PREPROCESSING_DESKEW_CROP_HH
28 # define SCRIBO_PREPROCESSING_DESKEW_CROP_HH
29 
33 
34 # include <scribo/preprocessing/deskew.hh>
35 
36 namespace scribo
37 {
38 
39  namespace preprocessing
40  {
41 
58  template <typename I, typename J>
59  mln_concrete(I)
60  deskew(const Image<I>& crop_gl, const Image<I>& input_gl);
61 
62 # ifndef MLN_INCLUDE_ONLY
63 
64  namespace internal
65  {
66 
67 
95 // double
96 // perform_deskew_crop(const image2d<bool>& input_bin,
97 // const image2d<value::int_u8>& input_gray,
98 // int threshold,
99 // int length)
100 // {
101 // Hough hough(input_bin.ncols(), input_bin.nrows());
102 // int
103 // mid = length >> 1,
104 // max = 0;
105 // double pos = 0;
106 
107 // for (int i = 1; i < input_bin.nrows() - 1; ++i)
108 // {
109 // int
110 // acc = 0,
111 // begin = 0,
112 // end = length;
113 
114 // for (int j = 0; j < length; ++j)
115 // acc += opt::at(input_bin, i, j);
116 
117 // for (int j = mid + 1; end < input_bin.ncols(); ++j, ++end, ++begin)
118 // {
119 // acc += opt::at(input_bin, i, end) - opt::at(input_bin, i, begin);
120 
121 // if (acc > threshold && !opt::at(input_bin, i + 1, j) &&
122 // !opt::at(input_bin, i + 1, j - 1) && !opt::at(input_bin, i + 1, j + 1))
123 // {
124 // double gy = opt::at(input_gray, i - 1, j - 1) + 2 * opt::at(input_gray, i - 1, j) + opt::at(input_gray, i - 1, j + 1);
125 // gy += - opt::at(input_gray, i + 1, j - 1) - 2 * opt::at(input_gray, i + 1, j) - opt::at(input_gray, i + 1, j + 1);
126 
127 // double gx = opt::at(input_gray, i - 1, j - 1) + 2 * opt::at(input_gray, i, j - 1) + opt::at(input_gray, i + 1, j - 1);
128 // gx += - opt::at(input_gray, i - 1, j + 1) - 2 * opt::at(input_gray, i, j + 1) - opt::at(input_gray, i + 1, j + 1);
129 
130 // if (abs(gx) + abs(gy) >= 255)
131 // vote(j, i, deskew, &max, &pos);
132 // }
133 // }
134 // }
135 
136 // return 90 - (pos + 650) / 10.0f;
137 // }
138 
139 
140  } // end of namespace scribo::preprocessing::internal
141 
142 
143  // Facade
144 
145 
146  template <typename I>
147  mln_concrete(I)
148  deskew(const Image<I>& crop_gl_, const Image<I>& input_gl_)
149  {
150  const I& crop_gl = exact(crop_gl_);
151  const I& input_gl = exact(input_gl_);
152 
153  mln_trace("scribo::preprocessing::deskew_crop");
154  mln_assertion(crop_gl.is_valid());
155  mln_assertion(input_gl.is_valid());
156  mlc_is(mln_domain(I), box2d)::check();
157  mlc_is_not(mln_value(I), bool)::check();
158  mlc_is_not_a(mln_value(I), value::Vectorial)::check();
159 
160  double angle = internal::perform_deskew(crop_gl);
161 
162  mln_concrete(I) output = crop_gl;
163 
164  // FIXME: trick to make this routine faster for really small
165  // angles (no impact on the results)
166  if (angle > 0.5 || angle < -0.5)
167  output = geom::rotate(crop_gl, - angle,
168  //mln_max(mln_value(I)),
169  extend(input_gl, mln_max(mln_value(I))),
170  make::box2d(crop_gl.nrows(),
171  crop_gl.ncols()));
172 
173  return output;
174  }
175 
176 # endif // ! MLN_INCLUDE_ONLY
177 
178 
179  } // end of namespace scribo::preprocessing
180 
181 } // end of namespace scribo
182 
183 
184 # endif // SCRIBO_PREPROCESSING_DESKEW_CROP_HH