MATLAB Source Code for Canny Edge Detector
- Login to Download
- 1 Credits
Resource Overview
Detailed Documentation
The following code presents a MATLAB implementation of edge detection using the Canny operator. Developed by John F. Canny in 1986, the Canny edge detector is a multi-stage algorithm widely recognized for its ability to identify image edges while effectively minimizing noise interference. The algorithm's key stages include Gaussian filtering, gradient computation, non-maximum suppression, and double thresholding. Our implementation leverages MATLAB's built-in functions to simplify the algorithm's execution while maintaining computational efficiency. For deeper understanding of Canny's theoretical foundations, users are encouraged to consult relevant literature and modify the code for experimental purposes.
```matlab
% Read input image using imread function
img = imread('lena.png');
% Convert RGB image to grayscale using rgb2gray
gray_img = rgb2gray(img);
% Apply Gaussian filter with standard deviation 1 for noise reduction
gaussian_img = imgaussfilt(gray_img, 1);
% Compute gradient components using gradient function
[Gx,Gy] = gradient(gaussian_img);
% Calculate gradient magnitude through Euclidean norm
G = sqrt(Gx.*Gx + Gy.*Gy);
% Perform non-maximum suppression with 1.5 pixel radius
nms_img = nonmaxsup(G, atan2(Gy, Gx), 1.5);
% Set double thresholds: high threshold at 10% of maximum intensity
high_thresh = max(nms_img(:)) * 0.1;
% Low threshold as 5% of high threshold for hysteresis thresholding
low_thresh = high_thresh * 0.05;
% Apply hysteresis thresholding to finalize edge detection
edge_img = hysthresh(nms_img, high_thresh, low_thresh);
% Display resulting edge map using imshow function
imshow(edge_img);
```
- Login to Download
- 1 Credits