Design of Control Algorithms Using Z-transform
Lines Detection with Hough Transform
An algorithm to find lines in images
Note: You can read the Chinese version of this article here.
I. Motivation
Recently, I found myself having to incorporate a document scanner feature into an app. After doing s o me research, I came across an article written by Ying Xiong who was a member of the Dropbox's machine learning team. The article explains how the Dropbox's machine learning team implemented their document scanner by highlighting the steps they went through and the algorithms used in each step (Xiong, 2016). Through that article, I learnt about a method called the Hough Transform and how it can be used to detect lines in images. Hence, in this article, I would like to explain the Hough Transform algorithm and provide a "from-scratch" implementation of the algorithm in Python.
II. The Hough Transform
The Hough Transform is an algorithm patented by Paul V. C. Hough and was originally invented to recognize complex lines in photographs (Hough, 1962). Since its inception, the algorithm has been modified and enhanced to be able to recognize other shapes such as circles and quadrilaterals of specific types. In order to understand how the Hough Transform algorithm works, it is important to understand four concepts: edge image, the Hough Space and the mapping of edge points onto the Hough Space, an alternate way to represent a line, and how lines are detected.
Edge Image
An edge image is the output of an edge detection algorithm. An edge detection algorithm detects edges in an image by determining where the brightness/intensity of an image changes drastically ("Edge Detection — Image Processing with Python", 2020). Examples of edge detection algorithms include: Canny, Sobel, Laplacian, etc. It is common for an edge image to be binarized meaning all of its pixel values are either a 1 or a 0. Depending on your situation, either a 1 or a 0 can signify an edge pixel. For the Hough Transform algorithm, it is crucial to perform edge detection first to produce an edge image which will then be used as input into the algorithm.
The Hough Space and the Mapping of Edge Points onto the Hough Space
The Hough Space is a 2D plane that has a horizontal axis representing the slope and the vertical axis representing the intercept of a line on the edge image. A line on an edge image is represented in the form of y = ax + b (Hough, 1962). One line on the edge image produces a point on the Hough Space since a line is characterized by its slope a and intercept b. On the other hand, an edge point (xᵢ, yᵢ) on the edge image can have an infinite number of lines pass through it. Therefore, an edge point produces a line in the Hough Space in the form of b = axᵢ + yᵢ (Leavers, 1992). In the Hough Transform algorithm, the Hough Space is used to determine whether a line exists in the edge image.
An Alternate Way to Represent a Line
There is one flaw with representing lines in the form of y = ax + b and the Hough Space with the slope and intercept. In this form, the algorithm won't be able to detect vertical lines because the slope a is undefined/infinity for vertical lines (Leavers, 1992). Programmatically, this means that a computer would need an infinite amount of memory to represent all possible values of a. To avoid this issue, a straight line is instead represented by a line called the normal line that passes through the origin and perpendicular to that straight line. The form of the normal line is ρ = x cos(θ) + y sin(θ) where ρ is the length of the normal line and θ is the angle between the normal line and the x axis.
Using this, instead of representing the Hough Space with the slope a and intercept b, it is now represented with ρ and θ where the horizontal axis are for the θ values and the vertical axis are for the ρ values. The mapping of edge points onto the Hough Space works in a similar manner except that an edge point (xᵢ, yᵢ) now generates a cosine curve in the Hough Space instead of a straight line (Leavers, 1992). This normal representation of a line eliminates the issue of unbounded value of a that arises when dealing with vertical lines.
Line Detection
As mentioned, an edge point produces a cosine curve in the Hough Space. From this, if we were to map all the edge points from an edge image onto the Hough Space, it will generate a lot of cosine curves. If two edge points lay on the same line, their corresponding cosine curves will intersect each other on a specific (ρ, θ) pair. Thus, the Hough Transform algorithm detects lines by finding the (ρ, θ) pairs that has a number of intersections larger than a certain threshold. It is worth noting that this method of thresholding might not always yield the best result without doing some preprocessing like neighborhood suppression on the Hough Space to remove similar lines in the edge image.
III. The Algorithm
- Decide on the range of ρ and θ. Often, the range of θ is [ 0, 180 ] degrees and ρ is [ -d, d ] where d is the length of the edge image's diagonal. It is important to quantize the range of ρ and θ meaning there should be a finite number of possible values.
- Create a 2D array called the accumulator representing the Hough Space with dimension (num_rhos, num_thetas) and initialize all its values to zero.
- Perform edge detection on the original image. This can be done with any edge detection algorithm of your choice.
- For every pixel on the edge image, check whether the pixel is an edge pixel. If it is an edge pixel, loop through all possible values of θ, calculate the corresponding ρ, find the θ and ρ index in the accumulator, and increment the accumulator base on those index pairs.
- Loop through all the values in the accumulator. If the value is larger than a certain threshold, get the ρ and θ index, get the value of ρ and θ from the index pair which can then be converted back to the form of y = ax + b.
IV. The Code
Non Vectorized Solution
Vectorized Solution
V. Conclusion
To conclude, this article showcased the Hough Transform algorithm in its simplest form. As mentioned, this algorithm can extend beyond detecting straight lines. Over the years, many improvements have been made to this algorithm that allow it to detect other shapes such as circles, triangles, and even quadrilaterals of specific shapes. This resulted in many useful real world applications ranging from document scanning to lane detection in self-driving cars. I'm confident that many more amazing technologies will be powered by this algorithm in the foreseeable future.
Design of Control Algorithms Using Z-transform
Source: https://towardsdatascience.com/lines-detection-with-hough-transform-84020b3b1549
0 Response to "Design of Control Algorithms Using Z-transform"
Post a Comment