Académique Documents
Professionnel Documents
Culture Documents
1. Edge Detection
In the field of Image Processing, the extraction of geometric features from images is very common problem. Over the years, several different approaches have been devised to extract these features. These different approaches can be characterized and classified in several different ways. Some of the techniques involve global examination of the image, while others only involve local examination of each pixel in the image.
[g, t]= edge(f, 'method', parameters ... , options ... ); where f is the input image. The most popularly used approaches are listed in the following table.
Additional approaches and control parameters are discussed in MATLAB's documentation. Your lab instructor will give you a tour of some of the details of the three approaches below in the lab lecture. In the output, g is a logical array with 1's at the locations where edge points were detected in f and 0's elsewhere. Parameter t is optional, it gives the threshold used by edge to determine which gradient values are strong enough to be called edge points. Edge Detector Sobel Canny Basic Properties Finds edges using the Sobel approximation to the derivatives. Finds edges by looking for local maxima of the gradient of f(x,y). The gradient is calculated using the derivative of a Gaussian filter. The method uses two thresholds to detect strong and weak edges, and includes the weak edges in the output only if they are connnected to strong edges. Therefore, this method is more likely to detect true weak edges. Finds edges by looking for zero crossings after filtering f(x,y) with a user defined filter. This method is often combined with the Laplacian of a Gaussian filter, but any filter that approximates the second derivative of the image's data will do. If you don't provide your own filter, Zero Crossing uses the equivalent of this filter:
H = FSPECIAL('log', 13, 2);
Zero Crossing
See also the log section of help FSPECIAL. Now let us try those different methods and parameters to see what is the difference between them:
T=100; f=zeros(128,128); f(32:96,32:96)=255; [g1, t1]=edge(f, 'sobel', 'vertical'); imshow(g1); t1
sigma=1; f=zeros(128,128); f(32:96,32:96)=255; [g3, t3]=edge(f, 'canny', [0.04 0.10], sigma); figure,imshow(g3); t3
2. Hough Transform
The Hough Transform (HT) is a robust method for finding lines in images that was developed by Paul Hough. The main idea for the HT is as follows:
y
For each line L, there is a unique line segment S perpendicular to L which passes through the origin. This segment can be used to represent a line and is called the Hessian Normal Form or HNF.
y y
S has a unique distance or radius r from the origin and angle from the horizontal axis of the image. This angle and distance define a point in parameter space, sometimes known as Hough space. o In MATLAB, the origin is located at (1,1). Other systems may place the origin at the center of the image, or at (0,0) o The angle need only cover 180. Sometimes positive values from 0 to 180 are used. By default MATLAB uses -90 to 90 A point in image space has an infinite number of lines that could pass through it, each with a unique distance and angle. This set of lines corresponds to a sinusoidal function in parameter space. Two points on a line in image space correspond to two sinusoids which cross at a point in parameter space. That point in parameter space corresponds to that line in image space, and all sinusoids corresponding to points on that line will pass through that point.
To implement this algorithm we quantize the parameter space by using a 2D array of counters, where the array coordinates represent the parameters of the line. This is commonly known as an accumulator array. The HT method for finding lines in images generally consists of the following three stages:
y y y
Accumulate tracings of the parametric sinusoid for each point in a binary edge map into the accumulator array. Find peak values in the accumulator array Verify that the peaks found correspond to legitimate lines, rather than noise.
Let's try a more complex shape, the edge detected square from the examples above:
%generate the picture f=zeros(128,128); f(32:96,32:96)=255; %find edges sigma=1; [g3, t3]=edge(f, 'canny', [0.04 0.10], sigma); %Do the Hough transform [H t r] = hough(g3); %r and c store column labels... %Display the transform in such a way that we can draw points on it later... imshow(H, [], 'XData', t, 'YData', r ); %Add axis labels to the picture xlabel('\theta'), ylabel('\rho'); axis on, axis normal;
where H is the Hough Transfrom matrix, and numpeaks is the maximum number of peak locations to look for. Be aware that peaks suppresses other peaks that are close to a peak just detected, and that if peaks are too weak, they will not be detected. These behaviours can be controlled with parameters. Consult the help file for houghpeaks for details. Continuing the sample from above:
%detect four peaks P = houghpeaks(H,4); %draw peaks over Hough transform %don't replace the picture when we start to draw hold on; plot( t( P(:,2) ), r( P(:,1) ), 's', 'color', 'green');
3. Global Threshoding
See section 10.3 in Digital Image Processing Using Matlab
In this section we discuss one way of choosing the threshold automatically. An algorithm like the following can be used to choose a threshold, T automatically: 1. Select an initial estimate for T. A possible initial value is the midpoint between the minimum and maximun intensity values in the image. 2. Segment the image using T. This will produce two groups of pixels: o G1 consisting of all pixels with intensity values > T o G2, consisting of pixels with values < T. 3. Compute the average intensity values x1 and x2 for the pixels in regions G1 and G2. 4. Compute a new threshod value: T=1/2(x1+x2) 5. Repeat steps 2 through 4 until the difference in T in successive iterations is smaller than a predifined parameter T0. The above iterative method can be implemented as follows:
T=0.5*(double(min(f(:)))+double(max(f(:)))); done = false; while ~done g = f >= T; Tnext = 0.5*(mean(f(g))+mean(f(~g))); done = abs (T-Tnext) < 0.5; T = Tnext; end
where g is a binary mask, f(g) is a subset of f defined by g. You can also use IPT's function graythresh to reach the same goal.
T = graythresh(f)
4. Exercise
Part 1: Edge Maps
1. Download the following image "building.jpg" and store it in MATLAB's "Current Directory".
2. Identify which of the following is the result of zerocrossing or Canny detector Image 1 Image 2
3. Reproduce the results for input image building.jpg. I encourage you to try different T and Sigma to get the best result. Part 2: Automatical Threshoding
1. Download the following image "text.jpg" and store it in MATLAB's "Current Directory".
2. Reproduce the results to get the image looks like the following enhance_text.gif.
Part 3: The Hough Transform 1. Perform a hough transform on the Canny edge results from Part 1. 2. Find a reasonable number of peaks in your Hough transform results using houghpeaks. You may want to fine tune your results by adjusting the threshold and neighborhood suppression parameters. 3. Use these peaks with houghlines as documented in help houghlines to produce a line plot of the original image. The example on the help page rotates the picture before finding edges. There is no need to do that, though I won't deduct marks if you do. There is no need to highlight the longest segment either. Deliverables:
y
Part 1
o
A script called edge_maps.m that: Identifies, with code comments, which edge detection method was used for the two images. Produces zero crossing and canny edge maps for building.jpg.
Part 2
o
A script called my_threshold.m that: finds and prints the optimal T for text.jpg uses T to threshold text.jpg and displays the results. A script called my_hough.m that uses Hough transform functions to produce a line drawing superimposed on the Canny edge map of the image in Part 1.
Part 3
o
5. References
y y
Digital Image Processing, Using MATLAB, by Rafael C. Gonzalez, Richard E. Woods, and Steven L. Eddins Image Processing Toolbox, For Use with MATLAB (MATLAB's documentation)-available through MATLAB's help menu or online at: http://www.mathworks.com/access/helpdesk/help/toolbox/images/