Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith -...

12
Assignment 3: Edge Detection Joe Smith - EE Affiliate I. INTRODUCTION This assignment looks at different techniques of detect- ing edges in an image. Edge detection is a fundamental tool in computer vision to analyse objects and to identify correspondence between similar objects. The coursework will look at the multiple stages involved in the Canny de- tector including Gaussian smoothing, non-maximal sup- pression and direction aware hysteresis. II. PREWITT AND SOBEL KERNELS Edge detection algorithms commonly use either Pre- witt and Sobel kernels. These are applied to the image through convolution. There is a kernel in the x-direction to highlight edges in the x-direction and a kernel to high- light edges in the y-direction (see Fig 2 for comparison). Initially, an algorithm to implement the convolution of each kernel with the image was built (see prewitt.m). This was contrived by scrolling through each row of 9 pix- els and taking the dot product with the kernel. Taking the sum of this obtained a weighted value of the central pixel. After further research, it was noticed that this could be achieved more efficiently using the inbuilt conv2 function (see sobel.m). Also, originally, the edges were combined by taking the amplitude from the square root of the squares of the edges in the x and y direction. How- ever, for computation efficiency we can use the following approximation instead: g mag = q g 2 x + g 2 y ≈|g x | + |g y | (1) The resulting image must be scaled to 255 levels, the standard level of intensity in a grayscale image. Finally, the image is thresholded at a user-defined value. The response of both algorithms are shown in Fig 3 with 90 chosen as the threshold. This was a point seen when not too many high frequency edges were picked up but enough clear edges displayed. There is little perceivable difference between the quality of edge detection using these two algorithms. Using the tic and toc parameters, the benefits of the two adjustments made can be seen. The optimised algorithm is 56 times faster. >> sobel(li,90); Elapsed time is 0.679604 seconds. >> prewitt(li, 90); Elapsed time is 37.944933 seconds. A refinement was implemented using the derivative of a Gaussian to filter the image before applying the edge detection. This has the effect of smoothing the image and removing noise which is prone to detection by the Sobel or Prewitt filters. This makes the algorithm more complex and has additional tunable parameters for the standard deviation and the size of the filter window. The size of the filter window effectively truncates the Gaus- sian. It is reasonable to do this after 3 standard devi- ations. The effects of a 0.3 standard deviation can be seen on a lower threshold of 60 in Fig 4. High frequency components are removed in the grass and the brickwork of the lighthouse, leaving more true edges. Finally, the image was edge detected using the in-built Canny algorithm in Matlab. As can be seen in Fig 5, this algorithm is far superior. Edges are detected excel- lently. The rest of this paper will look at improvements to achieve this level of detection. III. NON-MAXIMAL SUPPRESSION ALGORITHM Non-maximal suppression works by looking at the an- gles of the edges by taking the arctan of the x-component and y-component. It can be seen that if a pixel value is non-maximal in a line tangential to its edge angle, it may be removed from the edge map. π/2 (i - 1,j - 1) (i +1,j + 1) 0 ±π if -π/2 if edge > π/8 && edge < 3π/8 FIG. 1: A graphic showing the algorithm behind this imple- mentation of NMS This is implemented by splitting the edge direction into an 8 point compass. If the edge is in the horizontal di- rection, compare the top and bottom pixels. If the edge is in the vertical direction, compare the left and right direction. If the edge points north west or south east, compare the the top right and bottom left pixels. Alter- natively, compare the bottom right and top left pixels. We may do this by looking in a π/8 range and setting the tangential comparison accordingly with a series of if loops. A diagrammatic illustration of this algorithm is shown in Fig 1. The effect of edge thinning is clearly observed when comparing the non-maximal suppression on Sobel, Prewitt, and Sobel with Gauss smoothing to

Transcript of Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith -...

Page 1: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Assignment 3: Edge Detection

Joe Smith - EE Affiliate

I. INTRODUCTION

This assignment looks at different techniques of detect-ing edges in an image. Edge detection is a fundamentaltool in computer vision to analyse objects and to identifycorrespondence between similar objects. The courseworkwill look at the multiple stages involved in the Canny de-tector including Gaussian smoothing, non-maximal sup-pression and direction aware hysteresis.

II. PREWITT AND SOBEL KERNELS

Edge detection algorithms commonly use either Pre-witt and Sobel kernels. These are applied to the imagethrough convolution. There is a kernel in the x-directionto highlight edges in the x-direction and a kernel to high-light edges in the y-direction (see Fig 2 for comparison).

Initially, an algorithm to implement the convolutionof each kernel with the image was built (see prewitt.m).This was contrived by scrolling through each row of 9 pix-els and taking the dot product with the kernel. Takingthe sum of this obtained a weighted value of the centralpixel. After further research, it was noticed that thiscould be achieved more efficiently using the inbuilt conv2function (see sobel.m). Also, originally, the edges werecombined by taking the amplitude from the square rootof the squares of the edges in the x and y direction. How-ever, for computation efficiency we can use the followingapproximation instead:

gmag =√g2x + g2y ≈ |gx|+ |gy| (1)

The resulting image must be scaled to 255 levels, thestandard level of intensity in a grayscale image. Finally,the image is thresholded at a user-defined value. Theresponse of both algorithms are shown in Fig 3 with 90chosen as the threshold. This was a point seen whennot too many high frequency edges were picked up butenough clear edges displayed. There is little perceivabledifference between the quality of edge detection usingthese two algorithms. Using the tic and toc parameters,the benefits of the two adjustments made can be seen.The optimised algorithm is 56 times faster.

>> sobel(li,90);Elapsed time is 0.679604 seconds.>> prewitt(li, 90);Elapsed time is 37.944933 seconds.

A refinement was implemented using the derivative ofa Gaussian to filter the image before applying the edge

detection. This has the effect of smoothing the imageand removing noise which is prone to detection by theSobel or Prewitt filters. This makes the algorithm morecomplex and has additional tunable parameters for thestandard deviation and the size of the filter window. Thesize of the filter window effectively truncates the Gaus-sian. It is reasonable to do this after 3 standard devi-ations. The effects of a 0.3 standard deviation can beseen on a lower threshold of 60 in Fig 4. High frequencycomponents are removed in the grass and the brickworkof the lighthouse, leaving more true edges.

Finally, the image was edge detected using the in-builtCanny algorithm in Matlab. As can be seen in Fig 5,this algorithm is far superior. Edges are detected excel-lently. The rest of this paper will look at improvementsto achieve this level of detection.

III. NON-MAXIMAL SUPPRESSIONALGORITHM

Non-maximal suppression works by looking at the an-gles of the edges by taking the arctan of the x-componentand y-component. It can be seen that if a pixel value isnon-maximal in a line tangential to its edge angle, it maybe removed from the edge map.

π/2

(i− 1, j − 1)

(i+ 1, j + 1)

0±π

if

−π/2

if edge > π/8&& edge < 3π/8

FIG. 1: A graphic showing the algorithm behind this imple-mentation of NMS

This is implemented by splitting the edge direction intoan 8 point compass. If the edge is in the horizontal di-rection, compare the top and bottom pixels. If the edgeis in the vertical direction, compare the left and rightdirection. If the edge points north west or south east,compare the the top right and bottom left pixels. Alter-natively, compare the bottom right and top left pixels.We may do this by looking in a π/8 range and settingthe tangential comparison accordingly with a series of ifloops. A diagrammatic illustration of this algorithm isshown in Fig 1. The effect of edge thinning is clearlyobserved when comparing the non-maximal suppressionon Sobel, Prewitt, and Sobel with Gauss smoothing to

Page 2: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

the pre-suppressed image in Figs 6, 7 & 8.

IV. HYSTERESIS THRESHOLDING

In image processing, hysteresis compares two imagesto build an intermediate image. The function takes twobinary images that have been thresholded at differentlevels. The higher threshold has a smaller populationof white pixels. The values in the higher threshold aremore likely to be real edges. In hysteresis, if a value in thelarger population is connected to a point in the smallerpopulation, we can assume it is a real edge and add it tohysteresis image. A simple way to do this is to use thebwselect(BW1,C,R,N) function. This returns a binaryimage by observing objects in the image BW1 that over-lap pixels at row R, column C. An object is defined bythe parameter N . With N = 8, the algorithm looks for8-connected neighbours set to 1. The effects of hysteresiscan be seen in Figs 9, 10 & 11. The hysteresis is not par-ticularly intelligent and chooses to add large patches ofhigh frequency information based on connectivity. Later,a better edge aware technique will be implemented.

V. FAST FOURIER TRANSFORM

The discrete fast Fourier transform function fft2() con-verts a 2 dimensional signal into the frequency spectrum.This is implemented in MATLAB using the Cooley-Tukey algorithm [1] which reduces computational opera-tions for an N-length vector fromO(N2) needed in a stan-dard discrete Fourier transform (DFT) to O(N log(N))by expressing the DFT recursively in terms of smallerDFTs.

Once converted into the domain, the image must bescaled by the magnitude in order to display a range of fre-quencies. The magnitude of the signal shows how muchof a frequency is present in the image. The phase spec-ifies where this component exists in the image. Smallthin edges are displayed as high frequency componentsand large edges are seen as lower frequency components.A log scale was taken for the lighthouse image. Observ-ing a difficult spectrum such as seen in Fig 12 provideslittle useful information but the spectral form can be use-ful for processing as is seen in the next section. A simplerimage shows its components more clearly in Fig 13.

VI. HIGH PASS AND LOW PASS FILTER

To apply a high pass filter, low frequencies must be at-tenuated and the high frequencies left untouched. Like-wise, with a low pass filter, the high frequencies are at-tenuated. This is easy to achieve in the frequency domainwhere the components of frequency are known. An idealfilter will have an infinite roll-off. Each value can bemultiplied by 1 or 0 dependent on whether it is passed or

attenuated. Rather than using a box filter as suggestedby the script, is more natural to use a circular filter. Theradius of the filter defines the cut off frequency. For a lowpass, only the values within the radius of the circle willbe kept. For the high pass, it is the values outside of theradius which are kept. An algorithm was built to draw acircle based on the input cut-off frequency radius by the

user. This uses the equation of a circle r = ±√x2 + y2

defining the vectors x and y to cover all quadrants, pos-itive and negative, to make the full circle.

From Fig 14, it is noted most of the spectral data iscontained in the low frequencies. This is intuitive forthis is where the most defined edges in the image lie.Removing a small diameter circle from the image losesmost of the information.

VII. EDGE DIRECTION AWARE HYSTERESISALGORITHM

A new algorithm was written to use edge orientationin hysteresis. The high threshold is considered the baseimage. If pixels in the lower threshold are in the sameedge direction as adjacent ones in the higher threshold,they are included in the output image. This uses thesame pi/8 window condition explained for non-maximalsuppression. The effects of this algorithm can be seen inFig 15. This form of hysteresis is much more subtle. No-tice the roof of the shed is more joined up after hysteresiscompared to the high threshold from 3 to 6 pixels on theright and 4 to 10 on the left. Large areas of randomconnected pixels have not been added. Turning downthe direction tolerance, i.e. having a larger window than9-point connectivity, would improve on these results.

VIII. SCALE SPACE PYRAMID

First, a hysteresis is performed on two images thresh-olded at 50 levels but with the first σ = 0.5 and the sec-ond σ = 5. The larger standard deviation removes moreof the high frequency components. The original hystere-sis function must be used as these two images will havedifferent edge directions. The result of this can be seenin Fig 16. Some of the intermediate frequencies are ab-sent. A scale space pyramid iteratively applies Gaussianson the image with the effect of downsampling. A fullpyramid would have many different layers. Each layeris defined by the standard deviation required to down-sample the image to half its original size. The standardrule is to use σ2 = t where t is the scaling of the originalimage. This algorithm will not attempt an accurate pyra-mid but merely look at the addition of several differentstandard deviations to find frequency components. A tenlevel pyramid is shown in Fig 17. It must be noted thatchanging the range of standard deviations and the stepsbetween them as well as the initial threshold level changesedge detection results dramatically. The top image has a

2 of 12

Page 3: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

threshold of 20, starting at 0.4 standard deviations andincreasing in 0.2 increments. The bottom has a thresholdof 50, starting at 0.5 standard deviation and increasingin increments of 0.25.

IX. CONCLUSIONS

In conclusion, several stages of edge detection were ex-plored and algorithms produced. Different kernels were

compared. The benefits of non-maximal suppression andhysteresis were discussed. Further advanced forms of hys-teresis were attempted using edge direction and gaussianpyramids. The techniques are very much user-definedbased on thresholding and standard deviation values. Foreach image, these parameters can be tweaked to get agood response. A more robust technique would bypassthese human parameters.

[1] J. Cooley and J. Tukey, “An algorithm for the machine cal-culation of complex Fourier series,” Mathematics of com-putation, 1965.

3 of 12

Page 4: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

FIG. 2: Here shows the edges detected using the Sobel kernels for the x-direction and the y-direction.

FIG. 3: Here are the Prewitt and Sobel algorithms applied to the image with a 90 level threshold test

FIG. 4: Here is the effect of pre-blurring on the image before edge detection. Notice high frequency components are no longerpresent

4 of 12

Page 5: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

FIG. 5: The in-built canny detector is superior at detecting edges

FIG. 6: Non maximal suppression on the Sobel edge detection

5 of 12

Page 6: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

FIG. 7: Non maximal suppression on the Prewittt edge detection

FIG. 8: Non maximal suppression on the Sobel edge detection with Gaussian blurring

6 of 12

Page 7: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

FIG. 9: Hysteresis on the Sobel edge detection

FIG. 10: Hysteresis on the Prewitt edge detection

FIG. 11: Hysteresis on the Sobel edge detection with Gaussian blurring

7 of 12

Page 8: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

FIG. 12: The FFT of the lighthouse image

FIG. 13: FFT of a box

8 of 12

Page 9: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

FIG. 14: Applying high pass and low pass filters to an image

9 of 12

Page 10: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

FIG. 15: The effect of edge aware hysteresis in comparison to basic connectivity hysteresis

10 of 12

Page 11: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

FIG. 16: Using two different standard deviation values to form the basis of hysteresis

11 of 12

Page 12: Assignment 3: Edge Detectionzceeg99/imagepro/three.pdf · Assignment 3: Edge Detection Joe Smith - EE A liate I. INTRODUCTION This assignment looks at di erent techniques of detect-ing

Edge Detection Joe Smith

FIG. 17: Here are the results on a ten level pyramid.

12 of 12