Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
hough.hh
1 // Copyright (C) 2009 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 MLN_TRANSFORM_HOUGH_HH
27 # define MLN_TRANSFORM_HOUGH_HH
28 
32 
33 
34 # include <mln/core/image/image2d.hh>
35 # include <mln/data/fill.hh>
36 
37 # include <mln/geom/nrows.hh>
38 # include <mln/geom/ncols.hh>
39 # include <mln/geom/min_col.hh>
40 # include <mln/geom/min_row.hh>
41 # include <mln/geom/bbox.hh>
42 
43 # include <mln/opt/at.hh>
44 
45 # include <mln/math/sin.hh>
46 # include <mln/math/cos.hh>
47 # include <mln/math/pi.hh>
48 
49 # include <mln/make/box2d.hh>
50 
51 # include <mln/value/int_u8.hh>
52 
53 
54 namespace mln
55 {
56 
57  namespace transform
58  {
59 
71  //
72  template <typename I>
73  image2d<float>
74  hough(const Image<I>& input_);
75 
76 
77 
78 # ifndef MLN_INCLUDE_ONLY
79 
80 
81  namespace internal
82  {
83 
84 
86  double to_radians(double angle)
87  {
88  return angle * math::pi / 180.0f;
89  }
90 
91 
92  } // end of namespace mln::transform::internal
93 
94 
95 
96  template <typename I>
97  image2d<float>
98  hough(const Image<I>& input_)
99  {
100  trace::entering("mln::transform::hough");
101 
102  const I& input = exact(input_);
103  mlc_equal(mln_value(I), bool)::check();
104  mln_precondition(input.is_valid());
105 
106  def::coord
107  minrow = geom::min_row(input),
108  mincol = geom::min_col(input);
109  unsigned
110  ncols = geom::ncols(input),
111  nrows = geom::nrows(input);
112  int maxRho = (int)(sqrt((ncols * nrows)
113  + (ncols * nrows))
114  + 0.5);
115 
116  image2d<float> accu(360, 2*maxRho);
117  data::fill(accu, 0.f);
118 
119  mln_piter(image2d<int>) p(input.domain());
120  for_all(p)
121  if (input(p))
122  for (int angle = 0 ; angle < 360 ; ++angle)
123  {
124  double
125  theta = internal::to_radians(angle),
126  rho = (p.row() - minrow) * math::cos(theta)
127  + (p.col() - mincol) * math::sin(theta);
128  int
129  indexAngle = (int) (angle),
130  indexRho = (int)(rho + maxRho + 0.5);
131 
132  ++opt::at(accu, indexAngle, indexRho);
133  }
134 
135  trace::exiting("mln::transform::hough");
136  return accu;
137  }
138 
139 
140 # endif // ! MLN_INCLUDE_ONLY
141 
142  } // end of namespace mln::transform
143 
144 } // end of namespace mln
145 
146 
147 #endif // ! MLN_TRANSFORM_HOUGH_HH