|
| 1 | + |
| 2 | +""" |
| 3 | +``` |
| 4 | +lines = hough_transform_standard(image, ρ, θ, threshold, linesMax) |
| 5 | +``` |
| 6 | +
|
| 7 | +Returns an vector of tuples corresponding to the tuples of (r,t) where r and t are parameters for normal form of line: |
| 8 | + x*cos(t) + y*sin(t) = r |
| 9 | +
|
| 10 | +r = length of perpendicular from (1,1) to the line |
| 11 | +t = angle between perpendicular from (1,1) to the line and x-axis |
| 12 | +
|
| 13 | +The lines are generated by applying hough transform on the image. |
| 14 | +
|
| 15 | +Parameters: |
| 16 | + image = Image to be transformed (eltype should be `Bool`) |
| 17 | + ρ = Discrete step size for perpendicular length of line |
| 18 | + θ = List of angles for which the transform is computed |
| 19 | + threshold = No of points to pass through line for considering it valid |
| 20 | + linesMax = Maximum no of lines to return |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | +function hough_transform_standard{T<:Union{Bool,Gray{Bool}}}( |
| 25 | + img::AbstractArray{T,2}, |
| 26 | + ρ::Number, θ::Range, |
| 27 | + threshold::Integer, linesMax::Integer) |
| 28 | + |
| 29 | + |
| 30 | + #function to compute local maximum lines with values > threshold and return a vector containing them |
| 31 | + function findlocalmaxima(accumulator_matrix::Array{Integer,2},threshold::Integer) |
| 32 | + validLines = Vector{CartesianIndex}(0) |
| 33 | + for val in CartesianRange(size(accumulator_matrix)) |
| 34 | + if accumulator_matrix[val] > threshold && |
| 35 | + accumulator_matrix[val] > accumulator_matrix[val[1],val[2] - 1] && |
| 36 | + accumulator_matrix[val] >= accumulator_matrix[val[1],val[2] + 1] && |
| 37 | + accumulator_matrix[val] > accumulator_matrix[val[1] - 1,val[2]] && |
| 38 | + accumulator_matrix[val] >= accumulator_matrix[val[1] + 1,val[2]] |
| 39 | + push!(validLines,val) |
| 40 | + end |
| 41 | + end |
| 42 | + validLines |
| 43 | + end |
| 44 | + |
| 45 | + ρ > 0 || error("Discrete step size must be positive") |
| 46 | + |
| 47 | + height, width = size(img) |
| 48 | + ρinv = 1 / ρ |
| 49 | + numangle = length(θ) |
| 50 | + numrho = round(Integer,(2(width + height) + 1)*ρinv) |
| 51 | + |
| 52 | + accumulator_matrix = zeros(Integer, numangle + 2, numrho + 2) |
| 53 | + |
| 54 | + #Pre-Computed sines and cosines in tables |
| 55 | + sinθ, cosθ = sin.(θ).*ρinv, cos.(θ).*ρinv |
| 56 | + |
| 57 | + #Hough Transform implementation |
| 58 | + constadd = round(Integer,(numrho -1)/2) |
| 59 | + for pix in CartesianRange(size(img)) |
| 60 | + if img[pix] |
| 61 | + for i in 1:numangle |
| 62 | + dist = round(Integer, pix[1] * sinθ[i] + pix[2] * cosθ[i]) |
| 63 | + dist += constadd |
| 64 | + accumulator_matrix[i + 1, dist + 1] += 1 |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + #Finding local maximum lines |
| 70 | + validLines = findlocalmaxima(accumulator_matrix, threshold) |
| 71 | + |
| 72 | + #Sorting by value in accumulator_matrix |
| 73 | + sort!(validLines, by = (x)->accumulator_matrix[x], rev = true) |
| 74 | + |
| 75 | + linesMax = min(linesMax, length(validLines)) |
| 76 | + |
| 77 | + lines = Vector{Tuple{Number,Number}}(0) |
| 78 | + |
| 79 | + #Getting lines with Maximum value in accumulator_matrix && size(lines) < linesMax |
| 80 | + for l in 1:linesMax |
| 81 | + lrho = ((validLines[l][2]-1) - (numrho - 1)*0.5)*ρ |
| 82 | + langle = θ[validLines[l][1]-1] |
| 83 | + push!(lines,(lrho,langle)) |
| 84 | + end |
| 85 | + |
| 86 | + lines |
| 87 | + |
| 88 | +end |
0 commit comments