The **Canny edge detector** is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images. It was developed by John F. Canny in 1986. Canny also produced a *computational theory of edge detection* explaining why the technique works.

The Canny filter is a multi-stage edge detector. It uses a filter based on the derivative of a Gaussian in order to compute the intensity of the gradients.The Gaussian reduces the effect of noise present in the image. Then, potential edges are thinned down to 1-pixel curves by removing non-maximum pixels of the gradient magnitude. Finally, edge pixels are kept or removed using hysteresis thresholding on the gradient magnitude.

The Canny has three adjustable parameters: the width of the Gaussian (the noisier the image, the greater the width), and the low and high threshold for the hysteresis thresholding.

The general criteria for edge detection include:

- Detection of edge with low error rate, which means that the detection should accurately catch as many edges shown in the image as possible
- The edge point detected from the operator should accurately localize on the center of the edge.
- A given edge in the image should only be marked once, and where possible, image noise should not create false edges.

To understand with a example in python lets first generate a noisy image and see what we can do to the image with canny algorithm

# Generate noisy image of a square

im = np.zeros((128, 128))

im[32:-32, 32:-32] = 1

im = ndi.rotate(im, 15, mode='constant')

im = ndi.gaussian_filter(im, 4)

im += 0.2 * np.random.random(im.shape)

this code generates a random image of square we use the skimage library here to generate image then we rotate it 15 degree then applied gaussian filter

# Compute the Canny filter for two values of sigma

edges1 = feature.canny(im)

edges2 = feature.canny(im, sigma=3)

Now we just applies the canny filter with two sigma values now lets see how it change the image or how accurately it detects the edges but lets see how this algorithm works:

- Apply Gaussian filter to smooth the image in order to remove the noise
- Find the intensity gradients of the image
- Apply non-maximum suppression to get rid of spurious response to edge detection
- Apply double threshold to determine potential edges
- Track edge by hysteresis: Finalize the detection of edges by suppressing all the other edges that are weak and not connected to strong edges.

# display results

fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3),

sharex=True, sharey=True)

ax1.imshow(im, cmap=plt.cm.gray)

ax1.axis('off')

ax1.set_title('noisy image', fontsize=20)

ax2.imshow(edges1, cmap=plt.cm.gray)

ax2.axis('off')

ax2.set_title('Canny filter, $\sigma=1$', fontsize=20)

ax3.imshow(edges2, cmap=plt.cm.gray)

ax3.axis('off')

ax3.set_title('Canny filter, $\sigma=3$', fontsize=20)

fig.tight_layout()

plt.show()

This is why canny edge detector is so popular in computer vision

If you like the post and want your colleagues or friends to learn the same hit the like button share it on linkedin ,facebook, twitter let’s grow together for a bias free machine-human compiled future.