-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathhoughtransform.jl
260 lines (206 loc) · 8.48 KB
/
houghtransform.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using Images
"""
```
lines = hough_transform_standard(
img_edges::AbstractMatrix;
stepsize=1,
angles=range(0,stop=pi,length=minimum(size(img))),
vote_threshold=minimum(size(img)) / stepsize -1,
max_linecount=typemax(Int))
```
Returns a vector of tuples corresponding to the tuples of (r,t)
where r and t are parameters for normal form of line:
`x * cos(t) + y * sin(t) = r`
- `r` = length of perpendicular from (1,1) to the line
- `t` = angle between perpendicular from (1,1) to the line and x-axis
The lines are generated by applying hough transform on the image.
Parameters:
- `img_edges` = Image to be transformed (eltype should be `Bool`)
- `stepsize` = Discrete step size for perpendicular length of line
- `angles` = List of angles for which the transform is computed
- `vote_threshold` = Accumulator threshold for line detection
- `max_linecount` = Maximum no of lines to return
# Example
```julia
julia> using ImageFeatures
julia> img = fill(false,5,5); img[3,:] .= true; img
5×5 Array{Bool,2}:
false false false false false
false false false false false
true true true true true
false false false false false
false false false false false
julia> hough_transform_standard(img)
1-element Array{Tuple{Float64,Float64},1}:
(3.0, 1.5707963267948966)
```
"""
function hough_transform_standard(
img_edges::AbstractMatrix{Bool};
stepsize=1,
angles=range(0,stop=pi,length=minimum(size(img_edges))),
vote_threshold=minimum(size(img_edges)) / stepsize -1,
max_linecount=typemax(Int))
stepsize > 0 || error("Discrete step size must be positive")
ρ = stepsize
θ = angles
#function to compute local maximum lines with values > threshold and return a vector containing them
function findlocalmaxima!(validLines::AbstractVector{CartesianIndex{2}}, accumulator_matrix::Array{Int,2}, threshold)
for val in CartesianIndices(size(accumulator_matrix))
if accumulator_matrix[val] > threshold &&
accumulator_matrix[val] > accumulator_matrix[val[1],val[2] - 1] &&
accumulator_matrix[val] >= accumulator_matrix[val[1],val[2] + 1] &&
accumulator_matrix[val] > accumulator_matrix[val[1] - 1,val[2]] &&
accumulator_matrix[val] >= accumulator_matrix[val[1] + 1,val[2]]
push!(validLines,val)
end
end
end
indsy, indsx = axes(img_edges)
ρinv = 1 / ρ
numangle = length(θ)
numrho = round(Int,(2(length(indsx) + length(indsy)) + 1)*ρinv)
accumulator_matrix = zeros(Int, numangle + 2, numrho + 2)
#Pre-Computed sines and cosines in tables
sinθ, cosθ = sin.(θ).*ρinv, cos.(θ).*ρinv
#Hough Transform implementation
constadd = round(Int,(numrho -1)/2)
for pix in CartesianIndices(size(img_edges))
if img_edges[pix]
for i in 1:numangle
dist = round(Int, pix[1] * sinθ[i] + pix[2] * cosθ[i])
dist += constadd
accumulator_matrix[i + 1, dist + 1] += 1
end
end
end
#Finding local maximum lines
validLines = Vector{CartesianIndex{2}}(undef, 0)
findlocalmaxima!(validLines, accumulator_matrix, vote_threshold)
#Sorting by value in accumulator_matrix
@noinline sort_by_votes(validLines, accumulator_matrix) = sort!(validLines, lt = (a,b)-> accumulator_matrix[a]>accumulator_matrix[b])
sort_by_votes(validLines, accumulator_matrix)
max_linecount = min(max_linecount, length(validLines))
lines = Vector{Tuple{Float64,Float64}}(undef, 0)
#Getting lines with Maximum value in accumulator_matrix && size(lines) < max_linecount
for l in 1:max_linecount
lrho = ((validLines[l][2]-1) - (numrho-1)*0.5)*ρ
langle = θ[validLines[l][1]-1]
push!(lines,(lrho,langle))
end
lines
end
"""
```
circle_centers, circle_radius = hough_circle_gradient(img_edges, img_phase, radii; scale=1, min_dist=minimum(radii), vote_threshold)
```
Returns two vectors, corresponding to circle centers and radius.
The circles are generated using a hough transform variant in which a non-zero point only votes for circle
centers perpendicular to the local gradient. In case of concentric circles, only the largest circle is detected.
Parameters:
- `img_edges` = edges of the image
- `img_phase` = phase of the gradient image
- `radii` = circle radius range
- `scale` = relative accumulator resolution factor
- `min_dist` = minimum distance between detected circle centers
- `vote_threshold` = accumulator threshold for circle detection
[`canny`](@ref) and [`phase`](@ref) can be used for obtaining img_edges and img_phase respectively.
# Example
```julia
julia> using Images, ImageFeatures, FileIO, ImageView
julia> img = load(download("http://docs.opencv.org/3.1.0/water_coins.jpg"));
julia> img = Gray.(img);
julia> img_edges = canny(img, (Percentile(99), Percentile(80)));
julia> dx, dy=imgradients(img, KernelFactors.ando5);
julia> img_phase = phase(dx, dy);
julia> centers, radii = hough_circle_gradient(img_edges, img_phase, 20:30);
julia> img_demo = Float64.(img_edges); for c in centers img_demo[c] = 2; end
julia> imshow(img_demo)
```
"""
function hough_circle_gradient(
img_edges::AbstractArray{Bool,2},
img_phase::AbstractArray{<:Number,2},
radii::AbstractRange{<:Integer};
scale::Number=1,
min_dist::Number=minimum(radii),
vote_threshold::Number=minimum(radii)*min(scale, length(radii)))
rows,cols=size(img_edges)
non_zeros=CartesianIndex{2}[]
centers=CartesianIndex{2}[]
circle_centers=CartesianIndex{2}[]
circle_radius=Int[]
accumulator_matrix=zeros(Int, Int(floor(rows/scale))+1, Int(floor(cols/scale))+1)
function vote!(accumulator_matrix, x, y)
fx = Int(floor(x))
fy = Int(floor(y))
for i in fx:fx+1
for j in fy:fy+1
if checkbounds(Bool, accumulator_matrix, i, j)
@inbounds accumulator_matrix[i, j] += 1
end
end
end
end
for j in axes(img_edges, 2)
for i in axes(img_edges, 1)
if img_edges[i,j]
sinθ = -cos(img_phase[i,j]);
cosθ = sin(img_phase[i,j]);
for r in radii
x=(i+r*sinθ)/scale
y=(j+r*cosθ)/scale
vote!(accumulator_matrix, x, y)
x=(i-r*sinθ)/scale
y=(j-r*cosθ)/scale
vote!(accumulator_matrix, x, y)
end
push!(non_zeros, CartesianIndex{2}(i,j));
end
end
end
for i in findlocalmaxima(accumulator_matrix)
if accumulator_matrix[i]>vote_threshold
push!(centers, i);
end
end
@noinline sort_by_votes(centers, accumulator_matrix) = sort!(centers, lt=(a, b) -> accumulator_matrix[a]>accumulator_matrix[b])
sort_by_votes(centers, accumulator_matrix)
dist(a, b) = sqrt(sum(abs2, (a-b).I))
f = CartesianIndex(map(r->first(r), axes(accumulator_matrix)))
l = CartesianIndex(map(r->last(r), axes(accumulator_matrix)))
radius_accumulator=Vector{Int}(undef, Int(floor(dist(f,l)/scale)+1))
for center in centers
center=(center-1*_oneunit(center))*scale
fill!(radius_accumulator, 0)
too_close=false
for circle_center in circle_centers
if dist(center, circle_center)< min_dist
too_close=true
break
end
end
if too_close
continue;
end
for point in non_zeros
r=Int(floor(dist(center, point)/scale))
if radii.start/scale<=r<=radii.stop/scale
radius_accumulator[r+1]+=1
end
end
voters, radius = findmax(radius_accumulator)
radius=(radius-1)*scale;
if voters>vote_threshold
push!(circle_centers, center)
push!(circle_radius, radius)
end
end
return circle_centers, circle_radius
end
@deprecate hough_circle_gradient(
img_edges, img_phase,
scale, min_dist,
vote_threshold, radii) hough_circle_gradient(img_edges, img_phase, radii; scale=scale, min_dist=min_dist, vote_threshold=vote_threshold)
@deprecate hough_transform_standard(image, ρ, θ, threshold, linesMax) hough_transform_standard(
image, stepsize=ρ, angles=θ, vote_threshold=threshold, max_linecount=linesMax)