SMIL  0.9.1
DHoughTransform.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _D_HOUGH_TRANSFORM_HPP
31 #define _D_HOUGH_TRANSFORM_HPP
32 
33 #include "Core/include/private/DImage.hxx"
34 
35 namespace smil
36 {
47  template <class T1, class T2>
48  RES_T houghLines(Image<T1> &imIn, double thetaRes, double rhoRes, Image<T2> &imOut)
49  {
50  UINT wIn = imIn.getWidth();
51  UINT hIn = imIn.getHeight();
52 
53  double rhoMax = sqrt(wIn*wIn + hIn*hIn);
54 
55  UINT wOut = thetaRes * 180;
56  UINT hOut = rhoRes * rhoMax;
57 
58 
59  ImageFreezer freeze(imOut);
60  imOut.setSize(wOut, hOut);
61  fill(imOut, T2(0));
62 
63  typename Image<T1>::sliceType linesIn = imIn.getSlices()[0];
64  typename Image<T1>::lineType lIn;
65  typename Image<T2>::sliceType linesOut = imOut.getSlices()[0];
66 
67  double thetaStep = PI / double(wOut);
68  double rhoStep = rhoMax / double(hOut);
69  double theta;
70  UINT rho;
71 
72  for (UINT j=0;j<imIn.getHeight();j++)
73  {
74  lIn = linesIn[j];
75  for (UINT i=0;i<imIn.getWidth();i++)
76  {
77  if (lIn[i]!=0)
78  {
79  for (UINT t=0;t<wOut;t++)
80  {
81  theta = t*thetaStep;
82  rho = (i*cos(theta) + j*sin(theta)) / rhoStep;
83  if (rho<hOut)
84  linesOut[rho][t] += 1;
85  }
86 
87  }
88  }
89  }
90  return RES_OK;
91  }
92 
97  template <class T1, class T2>
98  RES_T houghCircles(Image<T1> &imIn, double xyResol, double rhoResol, Image<T2> &imOut)
99  {
100  UINT wIn = imIn.getWidth();
101  UINT hIn = imIn.getHeight();
102 
103  double rhoMax = sqrt(wIn*wIn + hIn*hIn);
104 
105  UINT wOut = xyResol * wIn;
106  UINT hOut = xyResol * hIn;
107  UINT dOut = rhoResol * rhoMax;
108 
109  ImageFreezer freeze(imOut);
110  imOut.setSize(wOut, hOut, dOut);
111  fill(imOut, T2(0));
112 
113  typename Image<T1>::sliceType linesIn = imIn.getSlices()[0];
114  typename Image<T1>::lineType lIn;
115  typename Image<T2>::volType slicesOut = imOut.getSlices();
116 
117  UINT rho;
118 
119  for (UINT j=0;j<imIn.getHeight();j++)
120  {
121  lIn = linesIn[j];
122  for (UINT i=0;i<imIn.getWidth();i++)
123  {
124  if (lIn[i]!=T1(0))
125  {
126  for (UINT j2=0;j2<hOut;j2++)
127  for (UINT i2=0;i2<wOut;i2++)
128  if (i!=i2 && j!=j2)
129  {
130  rho = sqrt(double((i*xyResol-i2)*(i*xyResol-i2)+(j*xyResol-j2)*(j*xyResol-j2)))/xyResol*rhoResol;
131  if (rho<dOut)
132  slicesOut[rho][j2][i2] += 1;
133  }
134 
135  }
136  }
137  }
138  return RES_OK;
139  }
140 
145  template <class T1, class T2>
146  RES_T houghCircles(Image<T1> &imIn, double resol, Image<T2> &imOut, Image<T2> &imRadiiOut)
147  {
148  UINT wIn = imIn.getWidth();
149  UINT hIn = imIn.getHeight();
150 
151  // Commente - rhoMax non utilise
152  // double rhoMax = sqrt(wIn*wIn + hIn*hIn);
153 
154  UINT wOut = resol * wIn;
155  UINT hOut = resol * hIn;
156 
157  ImageFreezer freeze(imRadiiOut);
158 
159  Image<double> imCentersOut(wOut, hOut);
160  imRadiiOut.setSize(wOut, hOut);
161 
162  fill(imCentersOut, 0.);
163  fill(imRadiiOut, T2(0));
164 
165  typename Image<T1>::sliceType linesIn = imIn.getSlices()[0];
166  typename Image<T1>::lineType lIn;
167  typename Image<double>::sliceType linesOut1 = imCentersOut.getSlices()[0];
168  typename Image<T2>::sliceType linesOut2 = imRadiiOut.getSlices()[0];
169 
170  double rho;
171  UINT nonZeroPts = 0;
172 
173  for (UINT j=0;j<imIn.getHeight();j++)
174  {
175  lIn = linesIn[j];
176  for (UINT i=0;i<imIn.getWidth();i++)
177  {
178  if (lIn[i]!=T1(0))
179  {
180  nonZeroPts++;
181  for (UINT j2=0;j2<hOut;j2++)
182  for (UINT i2=0;i2<wOut;i2++)
183  if (i!=i2 && j!=j2)
184  {
185  rho = sqrt(double((i*resol-i2)*(i*resol-i2)+(j*resol-j2)*(j*resol-j2)));
186  if (rho > 100 && rho<500)
187  {
188  linesOut1[j2][i2] += 1;
189  if (linesOut1[j2][i2]>nonZeroPts && linesOut2[j2][i2]<T2(rho))
190  linesOut2[j2][i2] = T2(rho);
191  }
192  }
193 
194  }
195  }
196  }
197  stretchHist(imCentersOut, imOut);
198  return RES_OK;
199  }
200 
202 } // namespace smil
203 
204 #endif // _D_HOUGH_TRANSFORM_HPP
205 
206 
Author Vincent Morard Date : 7 march 2011 See : Morard V. and Dokladal P. and Decenciere E...
Definition: DApplyThreshold.hpp:36
Definition: DBaseImage.h:235
RES_T houghCircles(Image< T1 > &imIn, double xyResol, double rhoResol, Image< T2 > &imOut)
Hough Circles.
Definition: DHoughTransform.hpp:98
virtual RES_T setSize(size_t w, size_t h, size_t d=1, bool doAllocate=true)
Set the size of image.
Definition: DImage.hxx:319
RES_T fill(Image< T > &imOut, const T &value)
Fill an image with a given value.
Definition: DImageArith.hpp:62
volType getSlices() const
Get an array containing the start offset of each slice.
Definition: DImage.hpp:118
size_t getHeight() const
Get image height.
Definition: DBaseImage.h:91
RES_T houghLines(Image< T1 > &imIn, double thetaRes, double rhoRes, Image< T2 > &imOut)
Hough Lines.
Definition: DHoughTransform.hpp:48
size_t getWidth() const
Get image width.
Definition: DBaseImage.h:87
RES_T stretchHist(const Image< T1 > &imIn, T1 inMinVal, T1 inMaxVal, Image< T2 > &imOut, T2 outMinVal=numeric_limits< T2 >::min(), T2 outMaxVal=numeric_limits< T2 >::max())
Stretch histogram.
Definition: DImageHistogram.hpp:214