You are on page 1of 16

Technical University of Cluj-Napoca

Faculty of Electronics, Telecommunications and Information Technology

Line detection by Hough transform


Team “Cutting Edge”

Coordinating teacher: Students:


S.l. Dr. Ing. Mihaela Cîșlariu Jimon Lucian-Daniel
Brândușe Mălin-Dorin
Group 2041
2022
Table of Contents

Introduction ................................................................................................................................... 3
About the project ................................................................................................................................3
The problem ...................................................................................................................................3
The solution....................................................................................................................................3
State-of-the-art .............................................................................................................................. 3
Theoretical fundamentals ............................................................................................................. 4
Gray images .......................................................................................................................................4
Canny Edge Detection........................................................................................................................4
Hough Transform ...............................................................................................................................6
Implementation ............................................................................................................................. 9
Experimental results ................................................................................................................... 11
Conclusion and possible improvements .................................................................................... 14
Bibliography ................................................................................................................................ 15

2
Introduction

About the project


The present project entitled ‘Line detection by Hough transform’’ was structured and developed as a
part of the ‘Digital Image Processing’ lecture, being the main aspect of its project sessions. The project
is using several computer vision theories and algorithms, which are all based on the image processing
principles, which were studied during the mentioned lecture.
The project aimed to develop software, based on genuine image processing techniques that can detect
visible lines in an input image.

The problem

The main problem for the project’s goal was to use a specific algorithm, called Hough transform, to
develop an application, based on the source code implemented by the authors, that can detect lines,
based on the edges present in a digital image.

The solution

One of the initial conditions of the present paper is the usage of the programming language Python,
which among other extraordinary benefits comes with extended usability of different libraries and pre-
defined or open-source functions, including computer vision functionalities. This way Python was
chosen as the main tool of the project.
The first part of the detection process is to convert the input image into a grayscale image. Then the
obtained image must be used to perform edge detection, which in the presented case will be done with
the Canny edge detection method. Then, as the last step, the Hough transform will give the lines
detected as wished.

State-of-the-art

As vehicle intelligence systems are getting more and more desired both by car producers and drivers,
detection algorithms are getting more attention, as well as image processing techniques. This concerns
the Hough transform as well since it is a relatively simple algorithm which can be successfully
implemented in lane detection (N. Thongpan) and object detection (Nima Razavi), both purposes well
acknowledged with a proper artificial intelligence model, and the recognition pattern obtained with the
Hough transform serves (mainly with a ‘Deep Hough Transform’ (Kai Zhao, 2021) used to semantic
line detection) as the optimal training base for the machine learning process.
3
Another current approach is also based on the low computational requirements of the presented
transformation, thus it is used in embedded systems, where speed and accuracy can be critical (Orlando
Chuquimia, 2018).
The current paper does not propose to realize a new method or purpose for the Hough transform but
aims to describe and present, based on the previously performed state-of-the-art, a simple, efficient
and likely didactic approach to the matter, with a solid learning attribute being present.

Theoretical fundamentals

Gray images
In digital image processing, grey images are the processed images whose pixels represent and light
intensity (or brightness) of the original pixel. These images present therefore only three colours: black,
white, and grey, but the last one has multiple levels.
The brightness value will give the obtained colour, with the minimum value being black, the maximum
value being white and the in-between values being the grey levels. The specific value depends on the
bit depth of each pixel, which means the number of bits used to represent one single pixel’s information
in binary form. This dependency means that every binary value from 0 to the value 2bit depth – 1 will
have a corresponding grayscale value.
For example, for a pixel having 8b in its binary representation, the intensity and the grayscale colour
will be based on the following relation:
𝑩𝒍𝒂𝒄𝒌, 𝒘𝒉𝒆𝒏 𝑳 = 𝑳𝒎𝒊𝒏 = 𝟎
𝑮𝒓𝒂𝒚 𝒍𝒆𝒗𝒆𝒍𝒔, 𝒘𝒉𝒆𝒏 𝑳𝒎𝒊𝒏 < 𝑳 < 𝑳𝒎𝒂𝒙 (1)
𝑾𝒉𝒊𝒕𝒆, 𝒘𝒉𝒆𝒏 𝑳 = 𝑳𝒎𝒂𝒙 = 𝟐𝟓𝟓
The resulting grey images are the best choice for further image processing steps, because they contain
all the necessary visual aspects in terms of objects of the image but simplify the further algorithms by
reducing the complexities with the use of the pixel in their minimal form, and the use of the 2-D scene
method, instead of 3-D scenes.

Canny Edge Detection


As Anil K. Jain mentions, edge detection is ‘a problem of fundamental importance’ (Jain, 1986). This
is a segmentation approach, which is based on considerably high (local) changes in intensity, and

4
characterizes actual physical objects, therefore edge detection is useful in object identification,
registration etc.
These edges can be modelled differently, based on their intensity profiles, thus the detection process
can differ on the used model concept. For example, edge types are step edges, ramp edges, or roof
edges.
The Canny edge detection uses the step edge model, which is considered an ‘ideal’ edge model, being
based on the idea that the transition between two intensity levels is happening with a minimal distance
of 1 pixel. This is rarely met since digital images often have blurry-edged objects, but detection
algorithms are suitable and are frequently used.
The Canny edge detection, although it is more complex than most edge detection algorithms, is
considered ‘superior in general’ (Rafael C. Gonzalez, 2008) to other edge detectors. It has three main
points, as mentioned below.
1. Low error range detection,
2. The detected edge points should be as close as possible to the true edges of the object.
3. A real edge should be detected only once, while noise should not define false edges.
Without any further description, as the project’s main theme is the Hough transformation, the Canny
edge detection algorithm can be presented with the following four steps.
1. Noise reduction.
2. Gradient calculation.
3. Non-maximum suppression to the gradient magnitude image
4. Double threshold, then edge tracking by hysteresis.
In the first phase, in noise reduction, the noise from the image is resolved by applying a Gaussian
filter. The technique of convolution of the image with a Gaussian Kernel is applied, the size of which
depends on the desired level of blur. Thus, the smaller the kernel, the less visible the blur will be.
Gradient calculation detects the intensity and direction of the edge, calculating the image gradient.
When the image is blurred, you can calculate the Ix and Iy derivatives, using the Sobel Kx and Ky
kernels.
−1 0 1
𝐾 = −2 0 2 (2)
−1 0 1
1 2 1
𝐾 = 0 0 0 (3)
−1 −2 −1
After which the magnitude G and the slope of the gradient are calculated, using the formulas:

𝐺= 𝐼 +𝐼 (4)

𝐼
𝜃(𝑥, 𝑦) = arctan (5)
𝐼

5
Non-Maximum suppression is the step in which the algorithm goes through all the points in the
intensity gradient matrix and looks for pixels with the maximum value in the direction of the edge.
Double Threshold detects 3 types of pixels: strong, weak, and irrelevant (or non-relevant) pixels. The
strong pixels have a high enough intensity to make part of the final edge, the weak ones have a weaker
intensity, but are not weak enough to be considered irrelevant, and the rest are considered irrelevant
pixels in terms of the currently analysed edge.
In the final step, edge tracking by hysteresis, based on the results from the previous step, weak pixels
are transformed into strong pixels if and only if at least one pixel around them is strong.
The resulting images will mean the main input data for the project, as they are the base digital images
to be analysed by the proposed line detection software, based on the Hough Transform.

Hough Transform
In image processing, a straight line can be represented with the equation below.
𝑦 = 𝑎𝑥 + 𝑏 (6)
Based on Equation 6, to detect a straight line in the image space, the parameters a and b, which is not
accessible in the image domain. But in a domain of (a,b) coordinates, the line y will represent a point
from the image domain. This domain is called the Hough domain or Hough space.
In reverse logic, points which lay on the same lines in the image domain will determine lines in the
Hough domain. This transformation concept is graphically represented in the following figure, in
which in the first coordinate system based on x and y, the points (x1,y1) and (x2,y2) lay on the same
line y = a0x + b0. This is transformed into Hough space by creating a line b = ax1+y1 and another one
b = ax2+y2 for each point, based on their coordinates. The intersection of the two obtained lines gives
the parameters of the original line from the x-y plane. The more points on the original line, the more
lines will cross the same intersection point in the Hough domain, which will make the voting process
for determining the intersection easier.

6
Figure 1: Hough Transform visualization

This representation of the line with the slope parameter a can be problematic in image processing, as
it is hard to define it. Thus, the polar coordinates will be the better choice for the line representation in
the image domain, resulting in the line equation presented
below.
𝜌 = 𝑥 cos 𝜃 + 𝑦 sin 𝜃 (7)
The range of values for the 𝜃 coordinate is [−90°, +90°], and the maximum value for the magnitude
𝜌 can be defined with equation
8.
𝜌 = 𝑥 +𝑦 (8)
Thus, the range for 𝜌 is between - 𝑥 + 𝑦 , and + 𝑥 + 𝑦 .
The new representation form is graphically illustrated in Figure 2.

Figure 2: Hough Transform with polar coordinates

7
As presented earlier, the parameter pair (𝜃, 𝜌) is the key to determining the lines in Hough space,
henceforth the lien detection is based on this parametric concept.
The algorithm is known to have the following steps:
1. Edge detection (with Canny, presented in the previous semi-chapter).
2. Create an accumulator matrix with dimension [𝑛𝑢𝑚𝑏𝑒𝑟_𝑜𝑓_𝜌, 𝑛𝑢𝑚𝑏𝑒𝑟_𝑜𝑓_𝜃] initialized with
0.
3. Mapping the points from the edge to the Hough domain and saving the parameters to the
accumulator.
4. Interpretation of the accumulator, based on thresholding.
5. Conversion of infinite lines to finite lines.
The resolution of the accumulator will determine the precision of the detection. Usually, a resolution
of 1 pixel per 𝜌 and 1 degree for 𝜃 is satisfactory (Masci, 2009)
As the polar coordinates were introduced earlier, the mapping process as step 3 will generate sinusoids
in the Hough domain, and the accumulator interpretation of the one-but-last step is based on a chosen
threshold, above which every value from the sine will represent a line. For example, if a threshold of
70% of the largest value from the accumulator was chosen, that means that every value greater than
this will represent an infinite line in the image domain, with the parameters (𝜃, 𝜌) corresponding to
the found above-threshold value.
The conversion from infinite lines to finite lines can be performed with various methods, but one which
does not require as much memory and system performance is the Progressive Probabilistic Hough
Transform (J Matas, 1998).
The basic concept of this improved algorithm is to transform random pixels from the edge image and
load the result into the accumulator. When a bin from the accumulator has got a number of votes higher
than a chosen threshold, a line is determined, and the edge image is analysed along that line to check
if there are any finite lines. After this, all the pixels of this line are removed. In this way, the algorithm
can detect finite lines.
An example of results for the Hough transform can be observed below, as Figure 3 presents.

Figure 3: Hough Transform example (a. Original image, b. Edge Image, c. Result image)

8
Implementation

Regarding the implementation of the proposed project, the selected programming language was Python,
which is arguably the most used programming language in image processing (Khandelwal, 2022),
along with C++.
One of the main benefits of using Python, besides its easy-to-use characteristic, is a large number of
libraries which can be used for various purposes. Hence, the libraries OpenCV, matplotlib, and Tkinter
were used for functions which were not meant to be implemented manually, such as GUI creation,
Canny edge detection etc.
The software application implemented has two main parts: the line detection function, which will
return based on the input parameters (Hough threshold and input image) a resulting image which has
the detected lines placed on the original image; and a GUI implemented with Tkinter which is the
integrated Python graphic library.
The back-end component is entirely based on the Theoretical fundamentals part of the current paper,
and the simple algorithm flow can be represented as Figure 4 shows below.

Figure 4: Flowchart of the implemented program

Regarding the GUI, this can be seen in Figure 5 and has the following components and functionalities:
 Browse Images button, for .jpg / .png file choices. After choosing an image, it will be visible
on the right side of the window
 Sliders for both the Hough threshold and the Canny edge detection threshold
 Apply Hough Transform labelled button, for performing the line detection. It will update the
displayed image
 Show Canny edge detection will display in a new window the result of the Canny edge detection

9
 Show Accumulator button will display in a new window the accumulator calculated by the
Hough algorithm
 Compare with OpenCV line detection, a button which will call the Hough Line detection
function implemented within the OpenCV library, the result being shown in a new window
 Exit button for closing the application

Figure 5: Application’s GUI

Future application upgrades could add more functionalities (e.g. saving method for the detected lines).

10
Experimental results

Based on the purpose of the developed software, the testing database was filled with relevant images
obtained from various sources, which can underline the line detection’s accuracy. Several examples
(initial image and image with detected lines) can be seen in the following figure.

Figure 6: Example 1, with settings: Hough Threshold = 291, Canny Threshold = 184

Figure 7: Example 2, with settings: Hough Threshold = 140, Canny Threshold = 192

11
Figure 8: Example 3, with settings: Hough Threshold = 140, Canny Threshold = 192

For analysis purposes, the following concepts are introduced. The line detection performance can be
judged based on two factors: the hit ratio (detected lines / existent lines) and the multiple lines
coefficient, which shows if a line is detected multiple times. In the case of the examples presented
above, the following charts were made, based on counting the real lines present in each image, by eye.

Hit ratio
1.2

0.8

0.6

0.4

0.2

0
Ex1 Ex2 Ex3

Figure 9: Hit ratio results

12
Multiple lines
30

25

20

15

10

0
Ex1 Ex2 Ex3

Figure 10: Multiple lines results

Because a hit ratio of 1 and a multiple lines coefficient of 0 are the best cases, the results indicate that
as the complexity of the images increase, the efficiency of the line detector decreases. It can be
observed that the third example has the best overall line detection score, as it has 0 multiple lines and
a 1 hit ratio, but it is the simplest image, having a black background, and visible lines. On contrary,
the second image seems to have the worst results, with 24 multiple lines and a hit ratio of around 0.4,
but this is as expected, since a large number of unclear lines, curves and elements are present in the
image.
These results are heavily influenced by the thresholds chosen, and the presented cases were all set to
the best combination of parameters. For a better understanding, a parameter change example is
presented below.

Figure 11: Different results with different threshold both for Canny threshold and Hough threshold

13
Conclusion and possible improvements

As the goal of the project is to create software which can detect lines of an input image using the
Hough transform, all the important theoretical aspects were presented and described.
The project uses grayscale images, which are the fundamental input data for computer vision uses, and
then a Canny edge detection will result in a secondary image, which will give all the edges present in
the original picture. The main part and core of the project is the third part of the process, which based
on the transformation concept from geometric parameters of a point from an image, will map them as
sinusoids in a polar coordinate system, then a voting mechanism will check the intersection scores of
the sines, resulting in finite lines from the original image. These lines will be marked, and then applied
on the input image, as the implementation part presents.
The described detector is wrapped in a Python-cored GUI, implemented with the Tkinter library, and
converted into an executable file with the PyInstaller library.
A future improvement would be to upgrade the detecting algorithm with the Progressive Probabilistic
Hough Transform version of it, making it fitter to a possible final goal: lane detection for embedded
devices (like car ECUs).
A possible issue of the mentioned Progressive Probabilistic Hough Transform is that different runs
could result in different detected lines, due to the case when more lines share the same pixel. If two
lines cross, the firstly detected line will generate a pixel deletion, which will also delete the shared
pixel of the second line. In the case of more intersecting lines, more points will miss in later detected
lines, which can even result in not reaching the put threshold value at voting. This can be excluded
with an additional condition of not deleting the intersection points.
In conclusion, the proposed Line detector using Hough transform was successfully implemented with
the help of the theoretical fundamentals and the software implementation described, detecting lines
from any given .jpg image file chosen, with a quite high success rate in the case of simpler images and
an acceptable precision in the case of more complex images, with the possibility of choosing the
threshold parameters which determine the hit ratio, and the multiple lines score of the detection.

14
Bibliography

1. J Matas, C. G. (1998). Progressive Probabilistic Hough Transform. British Machine Vision


Conference . Praha.
2. Jain, A. K. (1986). Fundamentals of Digital Image Processing. Englewood Cliffs, New Jersey:
Prentice-Hall, Inc.
3. Kai Zhao, Q. H.-B.-M. (2021). Deep Hough Transform for Semantic Line Detection. IEEE
Transactions on Pattern Analysis and Machine Intelligence 2021.
4. Khandelwal, N. (2022, December 7th ). Image Processing in Python: Algorithms, Tools, and
Methods You Should Know. Retrieved from Neptune: https://neptune.ai/blog/image-
processing-
python#:~:text=Python%20is%20one%20of%20the,of%20image%20processing%20very%2
0efficiently. [accessed on 29.12.2022]
5. Lee, S. (2020, May 2). Lines Detection with Hough Transform. Retrieved from Towards Data
Science: https://towardsdatascience.com/lines-detection-with-hough-transform-
84020b3b1549 [accessed on 29.12.2022]
6. Liu, H. (2020). Robot Systems for Rail Transit Applications. Elsevier.
7. MAITRA, S. (2019, February 24). What Canny Edge Detection algorithm is all about?
Retrieved from Medium: https://medium.com/@ssatyajitmaitra/what-canny-edge-detection-
algorithm-is-all-about-103d94553d21 [accessed on 29.12.2022]
8. Masci, D. F. (2009). Line Detection by Hough transformation. IPAC - Caltech.
9. N. Thongpan, M. K. (n.d.). The State of the Art in Development a Lane Detection for
Embedded Systems Design.
10. Nima Razavi, J. G. (n.d.). Latent Hough Transform for Object Detection. Retrieved from
Microsoft: https://www.microsoft.com/en-us/research/wp-
content/uploads/2016/11/rgkg_eccv2012.pdf [accessed on 29.12.2022]
11. Orlando Chuquimia, A. P. (2018). FPGA-based Real Time Embedded Hough Transform.
DASIP 2018 -Conference on Design and Architectures for Signal and Image Processing. Porto,
Portugal.
12. Rafael C. Gonzalez, R. E. (2008). Digital Image Processing. Upper Saddle River, New Jersey:
Pearson Education International.
13. Sahir, S. (2019, January 25). Canny Edge Detection Step by Step in Python — Computer Vision.
Retrieved from Towards Data Science: https://towardsdatascience.com/canny-edge-detection-
step-by-step-in-python-computer-vision-b49c3a2d8123 [accessed on 29.12.2022]
14. Verma, D. S. (2008). Edge detection and line detection in Image processing. Retrieved from
Babasaheb Bhimrao Ambedkar University:
https://bbau.ac.in/dept/CS/TM/Edge%20detection%20.pdf [accessed on 29.12.2022]
15
15. Week 4: Hough Transform (Line and Circle Detection). (2021). Retrieved from SBME
Tutorials: https://sbme-tutorials.github.io/2021/cv/notes/4_week4.html [accessed on
29.12.2022]
16. Why to use Grayscale Conversion during Image Processing? (n.d.). Retrieved from Is a hit:
https://www.isahit.com/blog/why-to-use-grayscale-conversion-during-image-
processing#:~:text=Why%20is%20grayscale%20needed%20for,to%20its%20barest%20mini
mum%20pixel. [accessed on 29.12.2022]

16

You might also like