hough transform

Plan for today
INF 4300 – Digital Image Analysis
HOUGH TRANSFORM
Fritz Albregtsen
F3 05.09.16
This lecture includes more details than G&W 10
10.2
2 !
• Introduction to Hough transform
05.09.2016
INF 4300
1
Introduction to HT
Using gradient information to detect lines
•
Representing a line
•
The [a,b]-representation
•
The [,]-representation
•
Algorithms
g
for detection of lines
•
Detecting circles and ellipses
F3 05.09.16
INF 4300
2
An image with linear structures
• The Hough transform (HT) can be used to detect
• Borders between the regions are
straight line segments.
– Lines
– Circles and ellipses
– Other parametric curves
•
•
•
•
•
Introduced by Paul Hough (1962)
First used to find lines in images a decade later (Duda & Hart 1972).
O first
Our
fi t goall is
i to
t find
fi d the
th location
l
ti off lines
li
in
i images.
i
This problem could be solved by e.g.
– Morphology
p
gy using
g a linear structuring
g element of a given
g
direction
• Then we would need to handle rotation, scaling, distortions etc
• These lines separate regions
with different grey levels.
• Edge detection is often used as
preprocessing to Hough
transform.
– Fitting a line (regression)
• Then we need to know which edge pixels to use as input
input,
to handle one line segment at a time
• HT can give robust detection under noise and partial occlusion.
F3 05.09.16
INF 4300
3
F3 05.09.16
INF 4300
4
Remember edge types?
Hough transform – the input
Hough-transform
• The input
p image
g must be a thresholded edge
g image.
g
• The magnitude
g
results computed
p
byy e.g.
g the Sobel
operator can be thresholded and used as input.
Step edge
F3 05.09.16
Ramp edge
Roof edge
INF 4300
5
• A thresholded edge image is the starting point for HT.
HT
• What does a Sobel filter produce?
• An approximation to the image gradient
which is a vector quantity given by:
6
• The gradient is a measure of how the function f(x
f(x,y)
y)
changes as a function of changes in the arguments x
y
and y.
• The gradient vector points in the direction of
maximum change.
• The length of this vector indicates the size of the
gradient:
 f 
 g x   x 
f ( x, y )      f 
g y   
 y 
INF 4300
INF 4300
Repetition – Edge magnitude
Repetition - Basic edge detection
F3 05.09.16
F3 05.09.16
| f |  g x2  g y2
7
F3 05.09.16
INF 4300
8
Gx, Gy, and the gradient operator
Prior to applying Hough transform:
• Horisontal edges:
– Compute gx(x,y)=Hx*f(x,y)
• Convolve with the horisontal filter kernel Hx
• Get edge magnitude from input image.
• Vertical edges:
• As always with edge detection, simple
p
filtering
g can be applied
pp
first.
lowpass
– Compute
p
gy((x,y)=H
,y) y*f(x,y)
( ,y)
• Convolve with the vertical filter kernel Hy
• Threshold gradient magnitude image.
• Compute the gradient parameters as:
G ( x, y )  g x2 ( x, y )  g y2 ( x, y )
Gradient magnitude
 g ( x, y ) 

 ( x, y )  tan 1  y
 g x ( x, y ) 
Gradient direction
Gradient-direction
• Note that thresholding of
gradient magnitude may be tricky!
• Note: from one input image, you produce four images:
INF 4300
– Assumptions of e.g. Otsu may be wrong…
– Gradient histogram is seldom bi-modal
bi modal …
– gx(x,y), gy(x,y), G(x,y), and θ(x,y).
F3 05.09.16
Input to Hough – thresholded edge image
9
F3 05.09.16
INF 4300
10
Hough transform – basic idea
Hough transform
Hough-transform
• Assume that we have performed some edge detection,
and a thresholding of the gradient magnitude image.
pixels that mayy partially
p
y describe
• We have n p
the boundaries of some objects.
• We wish to find sets of pixels
that make up straight line segments.
• Regard a point (xi; yi)
and a straight line yi = axi + b
– Many lines may pass through the point (xi,yi).
– Common
C
to
t th
them iis that
th t th
they satisfy
ti f th
the equation
ti above
b
for varying values of the parameters (a,b).
F3 05.09.16
INF 4300
11
F3 05.09.16
INF 4300
12
Hough transform – basic idea
Hough transform – basic idea
• Given
Gi
a liline in
i the
th (x,y)
( ) - plane:
l
y = ax + b
• This equation can obviously be rewritten as follows:
1
• We now consider (x ,y ) as parameters
and (a , b ) as variables.
• This is a line in (a,b
a b) - plane parameterized by x and y.
2
– So: a point (x1,y1) in xy - space gives a line in (a,b) space.
– Another p
point ((x2,y2) on the same line y = ax + b in xyy - space
p
will give rise to another line in (a,b) - space.
(x,y)-space
Line intersection point in (ab)-space
gives
i
line
li parameters in
i (x,y)-space
( )
F3 05.09.16
INF 4300
13
F3 05.09.16
Hough transform – basic idea
• Two points (x
(x,y)
y) and (z
(z,k)
k) define a line in the (x,y)
(x y) plane.
plane
• These two points give two different lines in (a,b) space.
• In (a,b) space these lines will intersect in a point (a’,b’)
where a’ is the slope and b’ the intercept of the line
d fi d by
defined
b (x,y)
( ) and
d (z,k)
( k) in
i (x,y)
( ) space.
• In fact,
fact all points on the line defined by (x,y)
(x y) and (z,k)
(z k) in
(x,y) space will parameterize lines that intersect in (a’,b’)
in (a,b) space.
• Points that lie approximately on a line in the (x,y) space
will form a “cluster
cluster of crossings”
crossings in the (a,b)
(a b) space.
space
F3 05.09.16
INF 4300
15
Each point in (x,y) gives
a line in the (a,b)-plane
(a,b)-space
INF 4300
14
Hough transform – algorithm
•
•
•
•
Quantize the parameter space (a,b), i.e., divide it into cells.
This quantized space is often referred to as the accumulator cells.
In the figure in the next slide amin is the minimal value of a, etc.
C
Count
t th
the number
b off times
ti
a line
li intersects
i t
t a given
i
cell.
ll
– For each point (x,y) with value 1 in the binary image,
find the values of (a,b) in the range [[amin,amax],[bmin,bmax]]
defining the line corresponding to this point.
– Increase the value of the accumulator for these [a’,b’] point.
proceed with the next point
p
in the image.
g
– Then p
• Cells receiving more than a given number of “votes”
are assumed
d to
t correspond
d to
t lilines in
i (x,y)
( ) space.
– Lines can be found as peaks in this accumulator space.
F3 05.09.16
INF 4300
16
Hough transform - algorithm
Example – images and accumulator space
Thresholded
edge images
Note how noise
affects the
accumulator.
Even with noise,
the largest peaks
correspond to the
major lines.
Visualizing
g the
accumulator space
The height of the
peak will be defined
by the number of
pixels in the line.
Thresholding the
accumulator space
and superimposing
corresponding lines
onto the edge image
Hough accumulator cells
F3 05.09.16
INF 4300
17
F3 05.09.16
INF 4300
18
HT and p
polar representation
p
of lines
Polar representation of lines
• In practice, we do not use the equation
in order to represent lines (why?)
• Rather, we use the polar representation of lines:
– where ρ is the length of the normal from origo to the line,
and θ is the angle between this normal and the x-axis.
– You should verify that this is true for any (x,y) on the line!
F3 05.09.16
INF 4300
Polar representation of lines
19
F3 05.09.16
INF 4300
20
Hough transform and the polar
representation
t ti
Two points on a line …
• The polar (also called normal) representation of straight lines is
x cos   y sin   
• Each point (xi,yi) in the xy-plane gives a sinusoid in the -plane.
• M colinear point lying on the line
xi cos   yi sin   
will give M curves that intersect at (i,
j) in the parameter plane.
plane
parameter domain
• Local maxima in p
correspond to significant lines in the image domain.
F3 05.09.16
INF 4300
21
• Each curve in the figure represents
the familiy of lines that may pass
through a particular point (xi,yi) in
th xy-plane.
the
l
• The intersection point (’,’)
corresponds
p
to the line
that passes through two points
(xi,yi) and (xj,yi)
• A horizontal line will have =0
and  equal to the intercept with
the y-axis.
• A vertical line will have =90
and  equal to the intercept with
the x-axis.
F3 05.09.16
y
x
INF 4300
22
Algorithm continued
HT with (ρ,θ)-representation
(ρ,θ) representation of lines
• The cell (i,j) corresponds to the square associated with
parameter values (j, i).
• Initialize all A(i,j) cells with value 0.
• Partition the -plane into accumulator cells A[,],
A[ ]
[min, max]; [min, max]
• The range of  is 90
• For each foreground point (xk,yk) in thresholded edge image
– Horizontal lines have =0, 0
– Vertical lines have =90,
 90 0
0
– For all possible -values
 values
• Solve for  using  = xk cos j +yk sin j
• Round  to the closest cell value, q
• Increment A(i,q) if the j results in q
• The range of  is  sqrt(M2+N
N2)
if the image is of size MxN
• A(i,j)
A(i,j)=P
P means that P points in the xy-space
xy space lie on the line
j = x cos j + ysin j
• Th
The di
discretization
i i off  and
d  must give
i acceptable
bl
precision within and size of the parameter space.
• Find line candiates where A(i,j) peaks above a threshold.
F3 05.09.16
INF 4300
23
F3 05.09.16
INF 4300
24
Hough transform – example 1
• Example
p 1: 11x11 image
g and its Hough
g transform:
F3 05.09.16
INF 4300
• Example
p 2: 11x11 image
g and its Hough
g transform:
25
Hough transform – example 3
• Example 3: Natural scene & result of Sobel magnitude:
F3 05.09.16
INF 4300
Hough transform – example 2
27
F3 05.09.16
INF 4300
26
Hough transform – example 3
• Example 3: Natural scene and result of Sobel gradient
magnitude followed by thresholding:
F3 05.09.16
INF 4300
28
Hough transform – example 3
• Example 3: Accumulator matrix:
F3 05.09.16
INF 4300
Hough transform – example 3
• E
Example
l 3:
3 Original
O i i l image
i
(left)
(l ft) and
d 20 mostt prominent
i
t
lines superimposed on gradient magnitude image (right):
29
F3 05.09.16
HT - advantages
• Disadvantages:
–
–
–
–
Conceptually simple.
Easy implementation.
Handles missing and occluded data very gracefully.
Can be adapted to several types of forms, not just lines.
F3 05.09.16
30
HT - disadvantages
• Advantages:
g
–
–
–
–
INF 4300
INF 4300
31
Computationally complex for objects with many parameters.
Looks for only one single type of object.
L
Length
th and
d position
iti off a line
li segmentt cannott be
b determined.
d t
i d
Co-linear line segments cannot be separated.
• Can be “fooled”
fooled by “apparent
apparent lines”.
lines .
• Start and end of line segments found by post-processing.
F3 05.09.16
INF 4300
32
HT and line & edge linking
HT using the full gradient information
• Given a gradient magnitude image g(x,y) containing a line.
• Simple algorithm:
for all g(xi,yi)>T do
for all  do
= xi cos + yi sin
find indexes (m,n) corresponding to (ρ, θ) and increment A(m,n);
• Better
B tt algorithm
l ith if we h
have both
b th
– The gradient magnitude g(x,y)
– And the gradient components gx and gy
• So we can compute gradient direction
 gy 

 gx 
for all g(xi,yi)>T do
= xi cos(g(x,y)) + yi sin(g(x,y))
find indexes (m
(m, n) corresponding to (
(, g(x,y)),
(x y)) increment A(m,n);
A(m n);
F3 05.09.16
INF 4300
How can we look for lines with a certain orientation?
Obtain a thresholded edge image
Specify subdivisions in the -plane.
 plane
Examine the counts of the accumulator cells (A)
for high pixel concentrations (= length of line).
line)
4. Examine the relationship (principally for continuity)
between pixels corresponding to a chosen A-cell
A cell.
•
 g ( x, y )  arctan
• The new algorithm:
g
Q:
1.
2
2.
3.
33
Using edge linking
•
Continuity here normally means distance between
disconnected pixels.
A gap in the line can be bridged if the length of the gap
is less than a certain threshold.
F3 05.09.16
INF 4300
34
Hough transform for circles
• A circle in the xy-plane
xy plane is given by
?
( x  xc ) 2   y  yc   r 2
parameter space.
p
What size,, what resolution?
• So we have a 3D p
2
• A simple 3D accumulation procedure:
set all A[xc,yyc,r]
r]=0;
0;
for every (x,y) where g(x,y)>T
for all xc
for all yc
r = sqrt((x-xc)2+(y-yc)2);
A[ c,yc,r]] = A[x
A[x
A[ c,yc,r]+1;
] 1
• Better procedure(s)? 1 circle? Several circles?
• Applications …
F3 05.09.16
INF 4300
35
F3 05.09.16
INF 4300
36
Hough transform for ellipses
Hough transform for ellipses
• A general ellipse in the xy-plane
xy plane has 5 parameters:
– Position of center (xc, yc), semi-axes (a,b), and orientation (θ).
• Thus,, we have a 5D parameter
p
space.
p
• For large images and full parameter resolution,
straight forward HT may easily overwhelm your computer!
• Reducing accumulator dimensionality:
– Pick pixel pairs with opposite gradient directions
– Accumulate 2D histogram of mid-points of such pairs
– Peak histogram locations are center candidates.
– Reduces HT
HT-accumulator
accumulator from 5D to 3D
3D.
• Reducing accumulator dimensionality (2):
– Pick pixel pair (P,Q) with non-parallel tangents.
– Tangents intersect at a point T.
– Let M be mid-point of PQ.
– Line TM goes through ellipse center.
center
– Accumulate 2D histogram of mid-points of such pairs
– Peak histogram locations are center candidates.
– Reduces HT-accumulator from 5D to 3D.
• Using
U i
other
h tricks
i k …
– Disadvantage: Fails if there are lots of occlusions.
F3 05.09.16
INF 4300
37
F3 05.09.16
HT of a convex polygon
• A
An equilateral
il t l triangle
ti
l off a given
i
size
i is
i positioned
iti
d
so that the origin coincides with the centre of mass
of the triangle, as shown in the sketch.
Y
h
X
• The three three TH peaks will be π/3 apart.
• The peaks
p
will have the same height.
g
• The normals onto the three sides are of the same length, => ρ=h
• What will happen in the Hough domain, if the triangle is rotated
anti-clockwise around its centre, (angular domain is[-π/2,π/2])?
ti 1  cos( i 1 ) sin( i 1 )  xi 
 
 
 ti   cos( i ) sin( i )   yi 
(θ,ρ)-domain
domain will slide in the
• Answer: The three maxima in the (θ,ρ)
positive θ-direction, keeping the 60 degree (π/3) distance between
the maxima and the constant ρ-value, sliding out of the [-π/2,π/2]domain at π/2 and re-appearing at –π/2.
–π/2
vi =[xi,yi]T
• Which can be solved for the vertex
• Given 0 ≤ θ1 < θ2 … < θN ≤ 2π,, we can solve for everyy vertex,,
and reconstruct the polygon from the N HT-peaks.
INF 4300
38
HT of rotated triangle
• A convex p
polygon
yg
is determined
by its HT:
For (x,y) on si-1
t i-1 = x cos(θ
(θi-1) + y sin(θ
i (θi-1)
For (x,y) on si
t i = x cos(θi) + y sin(θi)
• Each vertex belongs to exactly two sides.
• Given the HT peaks of these two sides, we can write
F3 05.09.16
INF 4300
39
F3 05.09.16
INF 4300
40
Hough transform – exercise 1
Hough transform – exercise 2
Next exercise: The randomized line-detection
line detection Hough transform.
transform
•
• Familiarize with Matlab function for line detection:
– Functions hough()
hough(), houghpeaks()
houghpeaks(), and houghlines()
• Next exercise:
– Test Hough transform for equal size circles
on the coins image.
F3 05.09.16
INF 4300
41
Learning goals - HT
• Understand the basic Hough Transform,
Transform and its limitations
• Understand that the normal representation is more general
• Be able
bl to implement
l
line
l
detecting
d
HT with
h accumulator
l
matrix
– Understand how this may be improved by gradient direction information.
• Be able to implement simple HT for circles of given size
size,
in order to find position of circular objects in image.
– Understand how this may be improved.
• Understand simple HT to detect ellipses
– Understand how this may be improved.
• Understand the basic random Hough transform.
• Do the exercises!
F3 05.09.16
INF 4300
43
Simple idea (line case):
– From the edge image, pick two points.
– Find the  and  corresponding to this set of points
points.
– Increment the indicated (,) cell.
– Once a cell reaches a certain (low) count, assume that an edge is
present in the image
image.
– Verify this.
– If truly present:
• Store
St
line
li parameters
t
• Erase this line from the edge image
– Continue until no more points or until the number of iterations
b t
between
ttwo d
detections
t ti
iis ttoo hi
high.
h
– Orders of magnitude faster than the ordinary transform.
F3 05.09.16
INF 4300
42