Hough Transform
Hough Transform
to isolate features of any regular curve like lines, circles, ellipses, etc. Hough transform in its simplest
from can be used to detect straight lines in an image. A generalized Hough transform can be used in
applications where simple analytic description of features is not possible. Due to the computational
complexity of the algorithm, people generally refrain from using it. Moreover learning based methods
can extract complex features from the image or a video that better suit the problem we are trying to
solve.
Algorithm
A straight line is the simplest boundary we can recognize in an image. Multiple straight lines can form
We transform the image space into hough space. By doing this we convert a line in image space to a
The equation of the line in the image space is of the form y = mx + c where m is the slope and c is the
y-intercept of the line. This line will be transformed to a point of the form (m, c) in the hough space.
But in this representation m goes to infinity for vertical lines. So let us use the polar coordinates
instead.
Dr.KVS
The line is represented by the length of that segment ρ , and the angle θ it makes with the x-axis. This
line will be transformed to a point of the form (ρ,θ) in the hough space.
The Hough transform constructs a histogram array representing the parameter space (i.e., an M x N
matrix, for M different values of the radius ρ and N different values of angle θ). For each parameter
combination,ρ and θ we then find the number of non-zero pixels in the input image that would fall
close to the corresponding line, and increment the array at position (ρ,θ) appropriately.
The intersection of multiple lines in image space represent corresponding multiple points in hough
space.
Similarly the reverse i.e lines intersecting at a point (m, c) in hough space can be transformed to a line
y = mx + c in image space.
Dr.KVS
If we have a line made up of many segments or points close to the same line equation in the image
So, consider a line in the image space which is an edge detected and has small discontinuities. To find
the continous line in an image we can transform this dicontinous line in image space to hough space
and look for intersection points in hough space. This intersection point in hough space will represent
Code
import numpy as np
import matplotlib.pyplot as plt
import cv2%matplotlib inline# Read in the image
image = cv2.imread('images/phone.jpg')# Change color to RGB (from BGR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)plt.imshow(image)
Dr.KVS
Performing Edge detection
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)# Define our parameters for Canny
low_threshold = 50
high_threshold = 100
edges = cv2.Canny(gray, low_threshold, high_threshold)plt.imshow(edges, cmap='gray')
Dr.KVS
plt.imshow(line_image)
For Hough Transforms, we will express lines in the Polar system. Hence, a line equation can
be written as:
y=(−cosθsinθ)x+(rsinθ)
1. In general for each point (x0,y0), we can define the family of lines that goes through that
point as:
Dr.KVS
rθ=x0⋅cosθ+y0⋅sinθ
Meaning that each pair (rθ,θ) represents each line that passes by (x0,y0).
2. If for a given (x0,y0) we plot the family of lines that goes through it, we get a sinusoid. For
instance, for x0=8 and y0=6 we get the following plot (in a plane θ - r):
3. We can do the same operation above for all the points in an image. If the curves of two
different points intersect in the plane θ - r, that means that both points belong to a same line.
For instance, following with the example above and drawing the plot for two more
points: x1=4, y1=9 and x2=12, y2=3, we get:
The three plots intersect in one single point (0.925,9.6), these coordinates are the parameters
( θ,r) or the line in which (x0,y0), (x1,y1) and (x2,y2) lay.
4. What does all the stuff above mean? It means that in general, a line can be detected by finding
the number of intersections between curves.The more curves intersecting means that the line
represented by that intersection have more points. In general, we can define a threshold of the
minimum number of intersections needed to detect a line.
5. This is what the Hough Line Transform does. It keeps track of the intersection between
curves of every point in the image. If the number of intersections is above some threshold,
then it declares it as a line with the parameters (θ,rθ) of the intersection point.
Dr.KVS
Standard and Probabilistic Hough Line Transform
• It consists in pretty much what we just explained in the previous section. It gives you as result
a vector of couples (θ,rθ)
• In OpenCV it is implemented with the function HoughLines()
• A more efficient implementation of the Hough Line Transform. It gives as output the
extremes of the detected lines (x0,y0,x1,y1)
• In OpenCV it is implemented with the function HoughLinesP()
Dr.KVS