write canny filter from scratch in matlab

To write a Canny filter from scratch in MATLAB, follow these steps:

  1. Load the image you want to process using the imread function:
main.m
img = imread('image.png');
27 chars
2 lines
  1. Convert the image to grayscale using the rgb2gray function:
main.m
grayImg = rgb2gray(img);
25 chars
2 lines
  1. Apply Gaussian smoothing to the image using the imgaussfilt function:
main.m
smoothedImg = imgaussfilt(grayImg, 1); % change the standard deviation to adjust the amount of smoothing
105 chars
2 lines
  1. Calculate the gradient magnitude and orientation of the image using the imgradient function:
main.m
[mag, orientation] = imgradient(smoothedImg, 'sobel');
55 chars
2 lines
  1. Perform non-maximum suppression on the gradient magnitude to thin out the edges using the edgeThinning function, as follows:
main.m
thinnedMag = edgeThinning(mag, orientation);
function thinnedMag = edgeThinning(mag, orientation)
    [m, n] = size(mag);
    thinnedMag = zeros(m, n);

    % convert orientation to a range of [0, 180] degrees for simplicity
    orientation = mod(orientation + 180, 180);

    % loop over each pixel except the border pixels
    for i = 2:(m-1)
        for j = 2:(n-1)
            % determine the two nearest gradient directions
            if (orientation(i,j) < 22.5 || orientation(i,j) >= 157.5) % horizontal gradients
                leftGrad = mag(i,j-1);
                rightGrad = mag(i,j+1);
            elseif (orientation(i,j) >= 22.5 && orientation(i,j) < 67.5) % diagonal gradients
                leftGrad = mag(i+1,j-1);
                rightGrad = mag(i-1,j+1);
            elseif (orientation(i,j) >= 67.5 && orientation(i,j) < 112.5) % vertical gradients
                leftGrad = mag(i+1,j);
                rightGrad = mag(i-1,j);
            else % diagonal gradients
                leftGrad = mag(i-1,j-1);
                rightGrad = mag(i+1,j+1);
            end
            
            % perform non-maximum suppression
            if (mag(i,j) >= leftGrad && mag(i,j) >= rightGrad)
                thinnedMag(i,j) = mag(i,j);
            end
        end
    end
end
1295 chars
34 lines
  1. Apply hysteresis thresholding to the thinned gradient magnitude to obtain the final edge map, using the edgeLinking function, as follows:
main.m
edgeMap = edgeLinking(thinnedMag, 0.1, 0.2); % change the thresholds to adjust the strength of the edges
function edgeMap = edgeLinking(thinnedMag, lowThreshRatio, highThreshRatio)
    % calculate the high and low thresholds based on the maximum and minimum of the thinned gradient magnitude
    magMax = max(thinnedMag(:));
    magMin = min(thinnedMag(:));
    lowThresh = lowThreshRatio * magMax;
    highThresh = highThreshRatio * magMax;
    
    % loop over each non-zero pixel in the thinned gradient magnitude
    [m, n] = size(thinnedMag);
    edgeMap = zeros(m, n);
    for i = 1:m
        for j = 1:n
            if (thinnedMag(i,j) >= highThresh)
                % if the pixel's magnitude is above the high threshold, mark it as an edge point
                edgeMap(i,j) = 1;
            elseif (thinnedMag(i,j) >= lowThresh)
                % if the pixel's magnitude is above the low threshold, check if it is connected to an edge point
                if (i > 1 && edgeMap(i-1,j) == 1)
                    edgeMap(i,j) = 1;
                elseif (i < m && edgeMap(i+1,j) == 1)
                    edgeMap(i,j) = 1;
                elseif (j > 1 && edgeMap(i,j-1) == 1)
                    edgeMap(i,j) = 1;
                elseif (j < n && edgeMap(i,j+1) == 1)
                    edgeMap(i,j) = 1;
                elseif (i > 1 && j > 1 && edgeMap(i-1,j-1) == 1)
                    edgeMap(i,j) = 1;
                elseif (i < m && j > 1 && edgeMap(i+1,j-1) == 1)
                    edgeMap(i,j) = 1;
                elseif (i > 1 && j < n && edgeMap(i-1,j+1) == 1)
                    edgeMap(i,j) = 1;
                elseif (i < m && j < n && edgeMap(i+1,j+1) == 1)
                    edgeMap(i,j) = 1;
                end
            end
        end
    end
end
1788 chars
40 lines
  1. Visualize the final edge map using the imshow function:
main.m
imshow(edgeMap);
17 chars
2 lines

gistlibby LogSnag