# Canny Edge Detection with Matlab

Fully working code of canny edge detection is given at the end. For explanation read complete article thanks.

### Edge Detection:

Edges in an image are those points which show sudden change of intensity and with help of derivatives(1st order, 2nd order) we can find this change in an image.

### Canny Edge Detector:

Canny edge detector is a complex but accurate detector as compared to **Marr-Hilderth detector**. And it is based on these 3 following objectives:

**1. Low Error Rate:**

Its says that all edges should be found and these detected edges should be as much close to true edges as possible

**2. Edge points should be well localized:**

It states that the edges located should be as close to true edges as possible. In other words, the distance between a point detected or marked as an edge by canny detector and the centre of the true edge should be minimum.

**3. Single Edge Point Response:**

The detector must output one edge point for each true edge point.

### The Canny Edge Detector: Algorithm

#### Step 1:

Let f(x,y) denote the input image and G(x,y) denote the Gaussian function:

G(x,y)=e^-(x^2 + y^2/2 a^2)

We form a smoothed image fs(x,y) by convolving G and f:

fs(x,y) = G(x,y) * f(x,y)

#### Step 2:

Compute the gradient magnitude and direction(angle):

M(x,y) =√gx^2 + gy^2

and a(x,y)=arctan(gy/gx)

where gx = ∂fs/∂x and gy=∂fs/∂y

#### Step 3:

The gradient M(x,y) typically contains wide range around local maxima. Next step is to thin those ridges.

**Nonmaxima Suppression:**

Let d1,d2,d3, and d4 denote the four basic edge directions for a 3×3 region: horizontal, -45 degrees, vertical, +45 degrees, respectively.

1. Find the direction dk that is closest to a(x,y)

2. If the value of M(x,y) is less than at least one of its two neighbours along dk, let gn(x,y)=0(suppression);

otherwise, let gn(x,y)=M(x,y)

Figure below shows this

#### Step 4:

Final operation is to threshold gn(x,y) to reduce false edge points.

**Hysteresis Thresholding:**

Mathematically its done like this:

gnh(x,y) = gn(x,y)>=Th

gnl(x,y) = gn(x,y)>=Tl

and

gnl(x,y) = gnl(x,y) – gnh(x,y)

**Explanation:**

Depending on the value of Th, the edges in gnh(x,y) typically have gaps. Longer edges are formed using the following procedure:

(a). Locate the next unvisited edge pixel p, in gnh(x,y)

(b). Mark as valid edge pixel all the weak pixels in gnl(x,y) that are connected to p using 8-connectivity.

(c). If all non-zero pixel in gnh(x,y) that were not marked as valid edge pixels .

### The Canny Edge Detection: Summary

1. Smooth the input image with a Gaussian filter

2. Compute the gradient magnitude and angle images

3. Apply non-maxima suppression to the gradient magnitude image

4. Use double thresholding and connectivity analysis to detect and link edges

### Output

## Matlab Code:

Click this **link** to download m-file of Canny Edge Detector. For any queries feel free to comment. Thanks!

Hi,

cant seem to get this to work with any other image i keep getting the error ‘ Index exceeds matrix dimensions’ can anybody help?

This code is not generic and for a given image dimensions, if you want to do edge detection for other images too plz check their dimensions and insert accordingly.. That is why you are getting an error…Thanks!

Sir, can u pleas elaborate how to insert the index dimension

insert image dimension where?

i mean image dimension.

Hi,

Thank you very much for posting this. I find it very useful and well explained. I didn’t quite understand the significance of gn from non-maxima supression.

Thanks!

I all the time emailed this blog post page to all my friends, because if like to

read it after that my links will too.

Thanks a lot Isabel….:)

Can you please explain the code

Can I expect the code for ” Digital Video Watermarking”???

wajahat ali shah thank u so much, plz harris corner detector ka explanatory code to send karo ya post karo

am nt getting clear image for lung xray.. so pls tell me how to change the parameter or something which will give d clear output