Hough Transform The Hough transform is a very general technique for feature detection. In the present context, we will use it for the detection of straight lines and circles as contour descriptors in edge point arrays. We could use other variants of the Hough transform to detect circular and other shapes. We could even use it outside of computer vision, for example in data mining applications. So understanding the Hough transform may benefit you in many situations. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 1 Hough Transform The Hough transform is a voting mechanism. In general, each point in the input space votes for several combinations of parameters in the output space. Those combinations of parameters that receive the most votes are declared the winners. We will use the Hough transform to fit a straight line to edge position data. To keep the description simple and consistent, let us assume that the input image is continuous and described by an x-y coordinate system. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 2 Hough Transform A straight line can be described by the equation: y = mx + c The variables x and y are the parameters of our input space, and m and c are the parameters of the output space. For a given value (x, y) indicating the position of an edge in the input, we can determine the possible values of m and c by rewriting the above equation: c = -xm + y You see that this represents a straight line in m-c space, which is our output space. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 3 Hough Transform Example: Each of the three points A, B, and C on a straight line in input space are transformed into straight lines in output space. y c C C winner parameters B B A 0 A x input space 0 m output space The parameters of their crossing point (which would be the winners) are the parameters of the straight line in input space. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 4 Hough Transform Hough Transform Algorithm: 1. Quantize input and output spaces appropriately. 2. Assume that each cell in the parameter (output) space is an accumulator (counter). Initialize all cells to zero. 3. For each point (x, y) in the image (input) space, increment by one each of the accumulators that satisfy the equation. 4. Maxima in the accumulator array correspond to the parameters of model instances. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 5 Hough Transform The Hough transform does not require preprocessing of edge information such as ordering, noise removal, or filling of gaps. It simply provides an estimate of how to best fit a straight line (or other curve model) to the available edge data. If there are multiple straight lines in the image, the Hough transform will result in multiple peaks. You can search for these peaks to find the parameters for all the corresponding straight lines. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 6 Improved Hough Transform Here is some practical advice for doing the Hough transform. The m-c space described on the previous slides is simple but not very practical. It cannot represent vertical lines, and the closer the orientation of a line gets to being vertical, the greater is the change in m required to turn the line significantly. We are going to discuss an alternative output space that requires a bit more computation but avoids the problems of the m-c space. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 7 Improved Hough Transform As we said before, it is problematic to use m (slope) and c (intercept) as an output space. Instead, it is a good idea to use the orientation and length d of the normal of a straight line to describe it. The normal n of a straight line l is perpendicular to l and connects l with the origin of the coordinate system. The range of is from 0 to 360, and the range of d is from 0 to the length of the image diagonal. Note that we can skip the interval from 180 to 270, because it would require a negative d. Let us assume that the image is 450×450 units large. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 8 Improved Hough Transform 0 0 Column j 450 636 representation of same line in output space d d Row i line to be described 450 0 0 input space output space 360 The parameters and d form the output space for our Hough transform. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 9 Improved Hough Transform For any edge point (i0, j0) indicated by our Sobel edge detector, we have to find all parameters and d for those straight lines that pass through (i0, j0). We will then increase the counters in our output space located at every (, d) by the edge strength, i.e., the magnitude provided by the Sobel detector. This way we will find out which parameters (, d) are most likely to indicate the clearest lines in the image. But first of all, we have to discuss how to find all the parameters (, d) for a given point (i0, j0). April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 10 Improved Hough Transform By varying from 0 to 360 we can find all lines crossing (i0, j0): Column j 0 0 Row i 1 2 3 d3 450 But how can we compute parameter d for each value of ? d2 d1 (i0, j0) 450April 26, 2016 Idea: Rotate (i0, j0) and the normal around origin by - so that the normal lands on i-axis. Then the i-coordinate of the rotated point is the value of d. Introduction to Artificial Intelligence Lecture 23: Computer Vision III 11 Improved Hough Transform And how do we rotate a point in two-dimensional space? The simplest way is to multiply the point vector with a rotation matrix. We compute the rotated point (iR, jR) as obtained by rotation of point (i0, j0) around the point (0, 0) by the angle as follows: April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 12 Improved Hough Transform We are only interested in the i-coordinate: iR = i0 cos - j0 sin In our case, we want to rotate by the angle -: iR = i0 cos(-) - j0 sin(-) iR = i0 cos + j0 sin Now we can compute parameter d as a function of i0, j0, and : d(i0, j0; ) = i0 cos + j0 sin By varying we are now able to determine all parameters (, d) for a given point (i0, j0) and increase the counters in output space accordingly. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 13 Improved Hough Transform We can then define the straight line representing the “winner” in parametric form using a parameter p. The points (i, j) of the line are then given by the following equation: By varying p within an appropriate range, we can compute every pixel of our straight line. But how can we find the vector (i, j) that determines the slope of the line? April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 14 Improved Hough Transform The idea here is that the orientation of the straight line is perpendicular to its normal. Since the orientation of the normal is given by the angle , the orientation of the straight line must be given by - 90. Therefore, we get: April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 15 Improved Hough Transform Then the complete equation for the straight line with parameters (, d) looks like this: Just vary parameter p in steps of 1 (to catch every pixel) between approximately -900 to 900 (twice the image size). Whenever (i, j) is within the image range, then visualize that pixel in the bitmap. April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 16 Sample Results Input Image April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 17 Sample Results Edges in input image (Sobel filter output) April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 18 Sample Results -90 d 0 90 180 Hough-transformed edge image April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 19 Sample Results -90 d 0 90 180 Five greatest maxima in Hough-transformed image April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 20 Sample Results Lines in input image corresponding to Hough maxima April 26, 2016 Introduction to Artificial Intelligence Lecture 23: Computer Vision III 21