make canny edge detector from scratch in matlab

To make Canny edge detector from scratch in MATLAB, follow these steps:

  1. Read the image you want to apply the edge detection on using the imread function.

    main.m
    img = imread('image.jpg');
    
    27 chars
    2 lines
  2. Convert the image into grayscale as Canny edge detector works on grayscale images.

    main.m
    img_gray = rgb2gray(img);
    
    26 chars
    2 lines
  3. Use the gradient operator to calculate the magnitude (edge strength) and direction of the edges in the image using the following code:

    main.m
    [Gmag, Gdir] = imgradient(img_gray, 'prewitt');
    
    48 chars
    2 lines
  4. Apply non-maximum suppression to thin out the edges. This involves finding pixels that have the maximum value in the edge direction and setting all other pixels in that direction to zero using the following code:

    main.m
    Gdir(Gdir < 0) = Gdir(Gdir < 0) + 180;
    Gdir(Gdir == 180) = -180;
    Gdir(Gdir < 22.5 & Gdir >= -22.5) = 0;
    Gdir(Gdir < 67.5 & Gdir >= 22.5) = 45;
    Gdir(Gdir < -67.5 & Gdir >= -112.5) = -90;
    Gdir(Gdir < 112.5 & Gdir >= 67.5) = 90;
    Gdir(Gdir < -22.5 & Gdir >= -67.5) = 135;
    Gdir(Gdir < 67.5 & Gdir >= 22.5) = 45;
    Gdir(Gdir < 180 & Gdir >= 112.5) = -135;
    
    suppressed = nonmaxsupp(Gmag, Gdir);
    
    386 chars
    12 lines
  5. Apply thresholding to remove weak edges using the following code:

    main.m
    edge_thresh = 0.2;
    [edge_strong, edge_weak] = threshold(suppressed, edge_thresh);
    
    82 chars
    3 lines
  6. Apply hysteresis thresholding to connect weak edges to strong edges using the following code:

    main.m
    edge_strong = hysthresh(edge_strong, edge_weak);
    
    49 chars
    2 lines
  7. Finally, display the detected edges using the following code:

    main.m
    imshow(edge_strong);
    
    21 chars
    2 lines

Here are the helper functions that are used in the above steps:

  1. nonmaxsupp: Implements non-maximum suppression by keeping pixels that have the maximum value in the edge direction and setting all other pixels in that direction to zero.

    main.m
    function suppressed = nonmaxsupp(Gmag, Gdir)
        [rows, cols] = size(Gmag);
        suppressed = zeros(rows, cols);
    
        for r=2:rows-1
            for c=2:cols-1
                dir = Gdir(r, c);
                if dir == 0 || dir == -180
                    if Gmag(r, c) >= Gmag(r, c-1) && Gmag(r, c) >= Gmag(r, c+1)
                        suppressed(r, c) = Gmag(r, c);
                    end
                elseif dir == 45 || dir == -135
                    if Gmag(r, c) >= Gmag(r-1, c+1) && Gmag(r, c) >= Gmag(r+1, c-1)
                        suppressed(r, c) = Gmag(r, c);
                    end
                elseif dir == 90 || dir == -90
                    if Gmag(r, c) >= Gmag(r-1, c) && Gmag(r, c) >= Gmag(r+1, c)
                        suppressed(r, c) = Gmag(r, c);
                    end
                elseif dir == 135 || dir == -45
                    if Gmag(r, c) >= Gmag(r-1, c-1) && Gmag(r, c) >= Gmag(r+1, c+1)
                        suppressed(r, c) = Gmag(r, c);
                    end
                end
            end
        end
    end
    
    991 chars
    28 lines
  2. threshold: Computes strong and weak edges based on the threshold value.

    main.m
    function [edge_strong, edge_weak] = threshold(suppressed, edge_thresh)
        edge_strong = suppressed > edge_thresh;
        edge_weak = suppressed > 0.05;
        edge_weak = edge_weak & ~edge_strong;
    end
    
    196 chars
    6 lines
  3. hysthresh: Performs hysteresis thresholding to connect weak edges to strong edges.

    main.m
    function edge_strong = hysthresh(edge_strong, edge_weak)
        [rows, cols] = size(edge_strong);
        for r=2:rows-1
            for c=2:cols-1
                if edge_weak(r, c) && any(edge_strong(r-1:r+1, c-1:c+1))
                    edge_strong(r, c) = true;
                end
            end
        end
    end
    
    288 chars
    11 lines

gistlibby LogSnag