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 {
43  template <class T1, class T2>
44  RES_T houghLines(Image<T1> &imIn, double thetaRes, double rhoRes, Image<T2> &imOut)
45  {
46  UINT wIn = imIn.getWidth();
47  UINT hIn = imIn.getHeight();
48 
49  double rhoMax = sqrt(wIn*wIn + hIn*hIn);
50 
51  UINT wOut = thetaRes * 180;
52  UINT hOut = rhoRes * rhoMax;
53 
54 
55  ImageFreezer freeze(imOut);
56  imOut.setSize(wOut, hOut);
57  fill(imOut, T2(0));
58 
59  typename Image<T1>::sliceType linesIn = imIn.getSlices()[0];
60  typename Image<T1>::lineType lIn;
61  typename Image<T2>::sliceType linesOut = imOut.getSlices()[0];
62 
63  double thetaStep = PI / double(wOut);
64  double rhoStep = rhoMax / double(hOut);
65  double theta;
66  UINT rho;
67 
68  for (UINT j=0;j<imIn.getHeight();j++)
69  {
70  lIn = linesIn[j];
71  for (UINT i=0;i<imIn.getWidth();i++)
72  {
73  if (lIn[i]!=0)
74  {
75  for (UINT t=0;t<wOut;t++)
76  {
77  theta = t*thetaStep;
78  rho = (i*cos(theta) + j*sin(theta)) / rhoStep;
79  if (rho<hOut)
80  linesOut[rho][t] += 1;
81  }
82 
83  }
84  }
85  }
86  return RES_OK;
87  }
88 
89  template <class T1, class T2>
90  RES_T houghCircles(Image<T1> &imIn, double xyResol, double rhoResol, Image<T2> &imOut)
91  {
92  UINT wIn = imIn.getWidth();
93  UINT hIn = imIn.getHeight();
94 
95  double rhoMax = sqrt(wIn*wIn + hIn*hIn);
96 
97  UINT wOut = xyResol * wIn;
98  UINT hOut = xyResol * hIn;
99  UINT dOut = rhoResol * rhoMax;
100 
101  ImageFreezer freeze(imOut);
102  imOut.setSize(wOut, hOut, dOut);
103  fill(imOut, T2(0));
104 
105  typename Image<T1>::sliceType linesIn = imIn.getSlices()[0];
106  typename Image<T1>::lineType lIn;
107  typename Image<T2>::volType slicesOut = imOut.getSlices();
108 
109  UINT rho;
110 
111  for (UINT j=0;j<imIn.getHeight();j++)
112  {
113  lIn = linesIn[j];
114  for (UINT i=0;i<imIn.getWidth();i++)
115  {
116  if (lIn[i]!=T1(0))
117  {
118  for (UINT j2=0;j2<hOut;j2++)
119  for (UINT i2=0;i2<wOut;i2++)
120  if (i!=i2 && j!=j2)
121  {
122  rho = sqrt(double((i*xyResol-i2)*(i*xyResol-i2)+(j*xyResol-j2)*(j*xyResol-j2)))/xyResol*rhoResol;
123  if (rho<dOut)
124  slicesOut[rho][j2][i2] += 1;
125  }
126 
127  }
128  }
129  }
130  return RES_OK;
131  }
132 
133  template <class T1, class T2>
134  RES_T houghCircles(Image<T1> &imIn, double resol, Image<T2> &imOut, Image<T2> &imRadiiOut)
135  {
136  UINT wIn = imIn.getWidth();
137  UINT hIn = imIn.getHeight();
138 
139  // Commente - rhoMax non utilise
140  // double rhoMax = sqrt(wIn*wIn + hIn*hIn);
141 
142  UINT wOut = resol * wIn;
143  UINT hOut = resol * hIn;
144 
145  ImageFreezer freeze(imRadiiOut);
146 
147  Image<double> imCentersOut(wOut, hOut);
148  imRadiiOut.setSize(wOut, hOut);
149 
150  fill(imCentersOut, 0.);
151  fill(imRadiiOut, T2(0));
152 
153  typename Image<T1>::sliceType linesIn = imIn.getSlices()[0];
154  typename Image<T1>::lineType lIn;
155  typename Image<double>::sliceType linesOut1 = imCentersOut.getSlices()[0];
156  typename Image<T2>::sliceType linesOut2 = imRadiiOut.getSlices()[0];
157 
158  double rho;
159  UINT nonZeroPts = 0;
160 
161  for (UINT j=0;j<imIn.getHeight();j++)
162  {
163  lIn = linesIn[j];
164  for (UINT i=0;i<imIn.getWidth();i++)
165  {
166  if (lIn[i]!=T1(0))
167  {
168  nonZeroPts++;
169  for (UINT j2=0;j2<hOut;j2++)
170  for (UINT i2=0;i2<wOut;i2++)
171  if (i!=i2 && j!=j2)
172  {
173  rho = sqrt(double((i*resol-i2)*(i*resol-i2)+(j*resol-j2)*(j*resol-j2)));
174  if (rho > 100 && rho<500)
175  {
176  linesOut1[j2][i2] += 1;
177  if (linesOut1[j2][i2]>nonZeroPts && linesOut2[j2][i2]<T2(rho))
178  linesOut2[j2][i2] = T2(rho);
179  }
180  }
181 
182  }
183  }
184  }
185  stretchHist(imCentersOut, imOut);
186  return RES_OK;
187  }
188 
189 
190 } // namespace smil
191 
192 #endif // _D_HOUGH_TRANSFORM_HPP
193 
194 
Definition: DColorConvert.h:38
RES_T fill(Image< T > &imOut, const T &value)
Fill an image with a given value.
Definition: DImageArith.hpp:62
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