Week 5 Lab 3:
Task 1: Work your way through the script file. Using the help function, understand how each function works, from edge detection, Hough Transform and line detection. Write a summary of how this algorithm works, particularly when finding the start/finish of a line.
Line 8: imread reads the image ‘cluttera2.jpg’ and creates a 786×1024 matrix.
Line 11: rgb2gray converts the image vector I from an RGB image to a grayscale image I.
Line 14: edge(I, ’canny’) uses canny edge detection to find the edges in the image I. Pixels which are classed as edges will be labelled as 1 and pixels which are not edges labelled as 0. Canny edge detection finds edges by looking for local maxima of the gradient of I. The edge function calculates the gradient using the derivative of a Gaussian filter. This method uses two thresholds to detect strong and weak edges, including weak edges in the output if they are connected to strong edges. By using two thresholds, the Canny method is less likely than the other methods to be fooled by noise, and more likely to detect true weak edges.
Line 17: [H,T,R] = hough(BW) , creates a hough transformation matrix, H, with accompanying theta, T, and Rho R, matrices. The hough transformation describes a set of lines from an origin point, of length rho and angle theta (with respect to the x axis), to a perpendicular vector from a line in the image I. Each line has it’s own T and R value within H.
The rows in H are the are the rho values and the columns are the theta values. H indexes how many lines are described by each given value of T and R. The values of T and R are discrete, which means for lines of the same T and/or R value respectively, the index in H will be
CS Help, Email: tutorcs@163.com
additive. So higher values in H mean more lines are described by the same T and R. This can be seen in figure 1. To achieve these discrete values, T and Rho are rounded to the nearest allowed T and R values given in the matrixes T and R.
Figure 1. A representation of the Hough Transform. Rho is the distance from the origin to the perpendicular of the line in the image. Theta is the angle between the perpendicular of the line and the x axis.
The Standard Hough Transform (SHT) uses the parametric representation of a line:
rho = x*cos(theta) + y*sin(theta)
The variable rho is the distance from the origin to the line along a vector perpendicular to the line. theta is the angle of the perpendicular projection from the origin to the line measured in degrees clockwise from the positive x-axis. The range of theta is −90°≤θ<90°. The angle of the line itself is θ+90°, also measured clockwise with respect to the positive x-axis.
Lines 20-23: This code displays the original image in a subplot.
Lines 25-27: This code displays the edge detected image, BW in a subplot.
Figure 2. The original image I and the edge detected image of I, BW.
Line 31: This calculates the hough peaks, the peaks in H, using the function houghpeaks. H is the hough transform, 10 is the number of hough peaks, ‘threshold’ is the minimum value in H to be considered for a peak, and this threshold value is given as ‘ceil(0.3*max(H))’. Ceil rounds a number up to the next integer greater than itself. The matrix P contains x,y co- ordinates of the peaks found.
Line 32 and 33: x and y are vectors which contain the x and y co-ordinates of the hough peaks.
Lines 36 to 41: This plots the hough transform with an x-axis of theta and a y axis of rho. The hough peaks are marked by a red square, which is seen in figure 3.
Figure 3. Hough map of the Hough transformaton, with the hough peaks marked by a red sqaure.
Line 45: The function houghlines finds the houghlines from the edge detected image, BW and uses the T, R and P vectors. The function extracts line segments in the image BW associated with particular bins in the Hough transform, H. P contains the row and column co- ordinates, which are used to search for line segments. Lines is a structure which has number of rows corresponding to the number of line segments found. For each line, there are 4 parameters stored in 4 columns, these are the start and end point of the line, in x and y and the value of theta and row that describes the line. This can be seen in figure 4.
Figure 4. Each field represents a line, found using houghlines.
Line 45: “ ‘Fill gap’, 5 “ means that if the distance between these two line segments are below the threshold value, 5 in this example, then the line segments are merged into the same line. “ ‘MinLength’,7 “ means that line segments below a length of 7 are discarded.
Lines 47 to 65: This code plots each line on the original image. The start of each line is marked by a yellow cross and the end of the line is a red cross, the line itself is green. This can be seen in figure 5.
Line 68: This colours the longest line segment in cyan.
Figure 5. The houghline segments shown on the original image.
The start and the ends of lines are defined from the values of T and R which were found from the houghpeaks function. These T and R values are then converted to a line in x and y ,which is parametrically mapped using the line equation
rho = x*cos(theta) + y*sin(theta).
Programming Help
What is the effect of increasing/decreasing the required number of peaks in ‘houghpeaks’?
The parameter NUMPEAKS in houghpeaks() determines the maximum number of local maxia in the given hough space that the function should identify. As each point in the hough space represents one line in euclidean space, increasing the required number of peaks causes the algorithm to detect more distinct lines in the image. This has the secondary effect of decreasing the average strength of the detected lines.
In images containing especially thick line shapes, detecting more peaks may result in detecting multiple lines corresponding to a single true line in the image.
This behaviour can be controlled using the NHoodSize parameter, which defines an area around an identified peak which is set to zero once that peak is detected.
Over a certain limit, depending on the threshold parameter used, increasing NUMPEAKS will not increase the number of local maxima detected, as all local maxima above the threshold have already been detected.
TASK 3: Replace the Canny Edge detector with other algorithms. Which one do you think performs best and why?
I tried Matlab’s built-in canny, sobel, roberts, and laplacian of gaussian edge detectors as the basis for the hough transform algorithm provided. It is difficult to assess visually which gave the best result, and it will depend both on the application and the many other parameters chosen to optimise the algorithm (threshold, min length, gap filling etc).
In general, Canny is best at detecting weak edges because it uses two thresholds, for strong and weak edges, and ignores weak edges that are likely to be noise. It therefore gives a more detailed result, and helps the hough algorithm to detect longer solid lines. However, because of the additional small detail it also seems more inclined to detect spurious lines by connecting a series of small details.
Roberts and sobel find less true edges but are potentially the most reliable for true feature detection with the given peak threshold.
Code Help, Add WeChat: cstutorcs