@@ -103,6 +103,183 @@ threshold::Integer, linesMax::Integer) where T<:Union{Bool,Gray{Bool}}
103103
104104end 
105105
106+ function  hough_line_probabilistic (
107+ img:: AbstractArray{T,2} ,
108+ ρ:: Real , θ:: Range ,
109+ threshold:: Integer , lineLength:: Integer , lineGap:: Integer , linesMax:: Integer ) where  T<: Union{Bool,Gray{Bool}} 
110+   
111+     ρ >  0  ||  error (" Discrete step size must be positive"  )
112+     indsy, indsx =  indices (img)
113+     ρinv =  1  /  ρ
114+     numangle =  length (θ)
115+     numrho =  round (Int,(2 (length (indsx) +  length (indsy)) +  1 )* ρinv) 
116+     constadd =  (numrho- 1 )/ 2 
117+     accumulator_matrix =  zeros (Int, numangle +  2 , numrho +  2 )
118+     h, w =  size (img)
119+     mask =  zeros (Bool, h, w)
120+ 
121+     # Pre-Computed sines and cosines in tables
122+     sinθ, cosθ =  sin .(θ).* ρinv, cos .(θ).* ρinv
123+     nzloc =  Vector {Tuple{Int64,Int64}} (0 )
124+     lines =  Vector {Tuple{Int64, Int64, Int64, Int64}} (0 )
125+ 
126+     for  pix in  CartesianRange (size (img))
127+         pix1 =  (pix[1 ], pix[2 ])
128+         if (img[pix])
129+             push! (nzloc, pix1)
130+             mask[pix] =  true 
131+         else 
132+             mask[pix] =  false 
133+         end 
134+     end 
135+ 
136+     count_ =  size (nzloc)[1 ]+ 1 
137+     while (count_> 1 )
138+         count_-= 1 
139+         good_line =  false 
140+         idx =  rand (1 : count_)
141+         max_val =  threshold- 1 
142+         max_n =  1 
143+         point =  nzloc[idx]
144+         line_end =  [[0 ,0 ],[0 ,0 ]]
145+         i =  point[1 ]- 1     
146+         j =  point[2 ]- 1 
147+         x0, y0, dx0, dy0, xflag =  0 , 0 , 0 , 0 , 0 
148+         const  shift =  16 
149+ 
150+         nzloc[idx] =  nzloc[count_]
151+ 
152+         if (! (mask[point[1 ], point[2 ]]))
153+             continue 
154+         end 
155+         
156+         for  n in  0 : numangle- 1 
157+                 dist =  round (Int, point[2 ]* cosθ[n+ 1 ] +  point[1 ]* sinθ[n+ 1 ])
158+                 dist +=  constadd
159+                 dist =  Int64 (dist)
160+                 accumulator_matrix[n+ 1  , dist +  1 ] +=  1 
161+                 val =  accumulator_matrix[n+ 1  , dist +  1 ]
162+                 if (max_val <  val)
163+                     max_val =  val
164+                     max_n =  n+ 1 
165+                 end     
166+         end 
167+ 
168+         if (max_val <  threshold)
169+             continue 
170+         end 
171+         
172+         a =  - sinθ[max_n]
173+         b =  cosθ[max_n]
174+         x0 =  j
175+         y0 =  i
176+         good_line =  false 
177+ 
178+         if (abs (a) >  abs (b))
179+             xflag =  1 
180+             dx0 =  a >  0  ?  1  :  - 1 
181+             dy0 =  round (b* (1  <<  shift)/ abs (a))
182+             y0 =  (y0 <<  shift) +  (1  <<  (shift- 1 ))
183+         else 
184+             xflag =  0 
185+             dy0 =  b >  0  ?  1  :  - 1 
186+             dx0 =  round ( a* (1  <<  shift)/ abs (b) );
187+             x0 =  (x0 <<  shift) +  (1  <<  (shift- 1 ));    
188+         end    
189+ 
190+         for  k =  1 : 2 
191+             gap =  0 
192+             x =  x0
193+             y =  y0
194+             dx =  dx0
195+             dy =  dy0
196+ 
197+             if  k> 1 
198+                 dx =  - dx
199+                 dy =  - dy
200+             end 
201+             
202+             while (true )
203+                 i1 =  0 
204+                 j1 =  0 
205+                 if (xflag== 1 )
206+                     j1 =  x
207+                     i1 =  y>> shift
208+                 else 
209+                     j1 =  x>> shift
210+                     i1 =  y
211+                 end 
212+                 
213+                 if ( j1 <  0  ||  j1 >=  w ||  i1 <  0  ||  i1 >=  h )
214+                     break ;
215+                 end 
216+                 gap+= 1 
217+                 if (mask[i1+ 1 , j1+ 1 ])
218+                     gap =  0 
219+                     line_end[k][1 ] =  i1+ 1 
220+                     line_end[k][2 ] =  j1+ 1 
221+                     
222+                 elseif (gap >  lineGap)
223+                     break 
224+                 end 
225+                 x =  Int64 (x+ dx)
226+                 y =  Int64 (y+ dy)
227+             end 
228+         end 
229+         
230+         good_line =  abs (line_end[2 ][1 ] -  line_end[1 ][1 ]) >=  lineLength ||  abs (line_end[2 ][2 ] -  line_end[1 ][2 ]) >=  lineLength              
231+ 
232+         for  k =  1 : 2 
233+             x =  x0
234+             y =  y0
235+             dx =  dx0
236+             dy =  dy0
237+ 
238+             if  k> 1 
239+                 dx =  - dx
240+                 dy =  - dy
241+             end 
242+ 
243+             while (true )
244+                 i1, j1 =  0 ,0 
245+ 
246+                 if  (xflag== 1 )
247+                     j1 =  x
248+                     i1 =  y >>  shift
249+                 else 
250+                     j1 =  x >> shift
251+                     i1 =  y
252+                 end 
253+                 if (mask[i1+ 1 , j1+ 1 ])
254+                     if (good_line)
255+                         for  n =  0 : numangle- 1 
256+                             r =  round ((j1+ 1 )* cosθ[n+ 1 ] +  (i1+ 1 )* sinθ[n+ 1 ])
257+                             r =  Int64 (r+ constadd)
258+                             accumulator_matrix[n+ 1 , r+ 1 ]-= 1 
259+                             mask[i1+ 1 , j1+ 1 ] =  false 
260+                         end 
261+                     end 
262+                 end 
263+                 
264+                 if ((i1+ 1 ) ==  line_end[k][1 ] &&  (j1+ 1 ) ==  line_end[k][2 ])
265+                     break 
266+                 end 
267+                 x =  Int64 (x+ dx)
268+                 y =  Int64 (y+ dy)              
269+             end 
270+ 
271+         end     
272+         if (good_line)
273+             push! (lines, (line_end[1 ][1 ], line_end[1 ][2 ], line_end[2 ][1 ], line_end[2 ][2 ]))
274+ 
275+             if (size (lines)[1 ] >=  linesMax)
276+                 return  lines
277+             end 
278+         end          
279+     end 
280+     return  lines        
281+ end 
282+ 
106283""" 
107284``` 
108285circle_centers, circle_radius = hough_circle_gradient(img_edges, img_phase, scale, min_dist, vote_thres, min_radius:max_radius)   
0 commit comments