canny-edge-detection

Edge detection is an image processing technique to find boundaries of objects in images. Here are an example image and the detected edges:

image

There are lots of edge detection algorithms but in this assignment, you will implement an algorithm with the following three steps:

• Smoothing
• Gradient calculation
• Non-maximum suppression
• Thresholding
• Hysteresis Thresholding

Smoothing

Before starting the actual edge detection, we first smooth the image to reduce the amount of edges detected from the noise. Smoothing is typically done by applying a low-pass filter to the image (as the noise is often high frequency term in the image). In this assignment, we will use a Gaussian blur. The following example shows the effect of the Gaussian smoothing:

image

Gaussian blur is defined as a convolution between the image and the following 5x5 filter, a discrete Gaussian function in 2D space truncated after two tabs:

image

Gradient calculation

Simply put, the edges in an image are from the sudden changes of brightness, assuming that each object consists of pixels that do not greatly vary in their brightness. One way to measure how big the change is to calculate the gradient magnitude at each pixel. The gradient operator we are going to use in this assignment is Sobel operator. Sobel operator is based on the following two 3x3 filters, which calculate x and y component of the gradient, respectively:

image

Once x and y components of the gradient is computed, the magnitude can be computed by:

image

result of gradient calculation:

image

Non-maximum suppression

One of the artifacts from smoothing is that it also makes the object boundaries blurry. As a result, the gradients calculated from the previous step might make the final edges span multiple pixels. In order to get sharper edges, we find local maxima in the gradient image and remove the others. Local maxima can be found in various ways, but in this assignment we will implement a simple one: a gradient is considered locally maximal if it is either greater than or equal to its neighbors in the positive and negative gradient direction.

Thresholding

Once we have computed a measure of edge strength (typically the gradient magnitude), the next stage is to apply a threshold, to decide whether edges are present or not at an image point. The lower the threshold, the more edges will be detected, and the result will be increasingly susceptible to noise and detecting edges of irrelevant features in the image. Conversely a high threshold may miss subtle edges, or result in fragmented edges

Hysteresis Thresholding

It uses two threshold values: a high (Th) and a low (Tl) Threshold. All pixels with gradient magnitude are greater than high threshold are considered as edge pixels, and all pixels with magnitude gradient values less than low threshold are considered as noisy edge points and dicarded. Finally, the pixels with magnitude gradient value between thetwo thresholds are considered as edge point only if they are connected to strong edge points.

Final Result Of Edge detection by canny:

final_edges