Matlab implementation of Canny edge detection algorithm from scratch.
Algorithm contains following steps:
1. Filtering input image with Gaussian filter with given standard deviation (filter size should be equal or greater than 6 * sigma)
2. Determining horizontal and vertical gradients of the filtered image
Original Image | Gradients in x direction | Gradients in y direction |
---|---|---|
3. Determining magnitude and angle od the gradients with following formulas:
Gradient magnitude | Gradient angle |
---|---|
4. Quantization of the angle of the gradient on the following directions: 0, -45, 45, 90 degrees.
5. Supression of the gradients that do not represent local maximum.
We iterate thorugh every pixel of the magnitude of the gradient and we read qunatized value of the gradient angle for that pixel. For every direction of the gradient angle specific matrix of 0s and 1s is defined:
We take the matrix that corresponds to the read quantized value of the gradient angle and multiply it with 3x3 gradient magnitude surrounding of the pixel. If the maximum value in the resulting 3x3 matrix is not in the middle, the pixel value is set to zero. This must NOT be done inplace!
6. Determining the maps of weak and strong edges based on low and high threshold.
All values of the magnitude of the gradient that are higher than T_high are set to value 1 in the map of the combined edges and they go into the map of the strong edges. All values between T_low and T_high are set to value 50/250 in the map of the combined edges and they go into the map of the weak edges. All values below T_low are set to 0.
Weak edges | Strong edges | Combined edges |
---|---|---|
7. Determining final map of edges by including weak edges that are connected with strong edges.
We iterate thorugh the combined map of the edges and if the edge is weak we check the 3x3 surrounding arround that edge. If in that surrounding exists at least 1 strong edge, the value of the weak edge is set to strong. This must NOT be done in place! This procedure repeats until there is no change from weak to strong.
Run the main.m to test the algorithm on the house.tif, camerman.tif, van.tif and lena.tif images.