521466S Machine Vision Assignment #7 Hough transform Spring 2014 In this assignment we use the hough transform to extract lines from images. We use the standard (r, θ) parametrization of lines, lter the accumulator, and extract the most prominent lines. Figure 1: The Kanizsa triangle. Theory Here is a brief summary of the hough transform. Refer to the lecture notes for a more detailed explanation. We will implement a simple Hough transform. Note that there are more robust ways of implementing it, but they are also more complicated. The hough transform consists of 4 basic steps: threshold gradient, accumulate votes, lter the accumulator, and extract objects. Threshold gradient The rst step is to select which pixels will cast votes. We threshold the gradient image and the pixels above threshold will cast votes. Selecting the threshold is not trivial and could vary across the image. In this case we will use a xed threshold for each image. Accumulate votes A given pixel (x, y) in the image could potentially belong to any line of the form ρ = x cos θ + y sin θ (1)
. This is, in theory, an innite collection of lines. However, we will discretize and bound the line space (ρ, θ) so that this becomes a nite set. Then each point will vote for all the lines it could belong to. We will discretize any variable a with max value max a and min value min a into N a bins with the following formulas s a = max a min a [units / bin] (2) N a 1 ( ) a mina k(a) = round + 1 [bin] (3) s a where s a is the step that describes how many units each bin represents and k a is the bin index for a given value of a. The angle of the line θ can be easily bounded between [0..π]. However, the radius ρ depends on the image size. The maximum possible radius would be produced by the pixel farthest away from the origin and is thus max ρ = W 2 + H 2 for an image of size (W, H). Because we are bounding the angle to half the circle, we must accept negative radii and thus min ρ = max ρ. Similarly, the number of bins for θ can be xed (e.g. N θ = 400), but for ρ it should depend on the image size to avoid losing resolution (e.g. N ρ = round(max ρ )). Filter the accumulator Because of noise, pixels on the same line might not cast the votes to the exact same bin. Instead of a single bin with a high value, we might nd a small cluster of bins, all with high values. To cope with noise one should lter the accumulator. For example, one can perform a low-pass ltering and then apply non-maxima suppression. Note that the kernel size for the ltering depends on the discretization of the line space. Extract the objects One usually has to limit the objects extracted from the accumulator. Thus, it is necessary to order the objects according to the vote count. First, all bins that have a vote count above threshold are extracted. They are then ordered according to vote count. And nally, the bin index is transformed back to line parameters by inverting Eq. (3). Instructions 1. Download the code and images This assignment comes with Matlab functions and a testing images. downloaded from: They can be http://www.ee.oulu.fi/research/imag/courses/machine_vision/assignments/ The assignment also has a main script that should guide you through the steps: main.m. Two images are provided for testing, the Kanizsa triangle and a car. Execute the script rst with the triangle and then test the results with the car. 2. Thresholding
The thresholding code is provided in the main script. Execute it and observe the results. The results are trivial with the triangle but notice how some edges are missed with the car due to the xed threshold. Also notice that even a perfectly sharp edge will create thick thresholded lines. Figure 2: Thresholded image. 3. Build the accumulator Complete the function buildhoughaccumulator(). This function should calculate the discretization parameters from Eq. (3) and build the accumulator of size (N θ, N ρ ). Each point should cast votes along the sinusoidal line dened by Eq. (1). Figure 3: Accumulator. 4. Extract lines Complete the function extractlinesfromhoughaccumulator(). This function takes an accumulator and extracts the N most voted lines. Note that each bin in the accumulator corresponds to a line. The parameters of the line are implicitly contained in the bin row and column indices.
Extract the 6 most voted lines for the triangle image and visualize them (using plotlines()). The result should look like g. 4. Notice that the lines extracted are pretty much the same but slightly shifted. This happens because we haven't ltered the accumulator and votes have been cast on neighbouring bins. Figure 4: Extracted lines directly from accumulator. 5. Filter the accumulator The function filterhoughaccumulator() performs non-maxima suppression on the accumulator. Use it to produce a second accumulator with only maxima values. Extract lines on this new accumulator. The result should look like g. 5. Figure 5: Extracted lines from ltered accumulator. 6. Improvements Run the algorithm on the car image. Figure 6: First 50 extracted lines from car image. Notice that the given algorithm is not perfect. The black line that separates the door is not detected by the thresholding, neither are the vertical fence posts. Filtering could include smoothing as well as non-maxima suppression. Feel free to play with the algorithm and suggest any improvements with your submission.
Deliverables Send an email to dherrera@ee.oulu.fi with the following. The subject line should be Machine Vision-A7-[student number] (e.g. Vision-A7-1234567). Machine A single attached zip le named A7-[student number].zip which should include: 1. buildhoughaccumulator.m (1 point) 2. extractlinesfromhoughaccumulator.m (1 point) The deadline for this assignment is on Monday 23rd of April.