detect edge of an image by using Canny algorithm
low : numeric , low threshold
high : numeric , high threshold
operator : identical(Sobel3, Sobel5, Scharr, "Sobel3", "Sobel5", "Scharr")
norm : identical(L1, L2, "L1", "L2")
filtersize : posint , the size of the Gaussian filter
sigma : numeric , the standard deviation of Gaussian filter
output : identical(thresholds, image, "threshold", "image")
The CannyEdgeDetect(img) command detects the edges of img by using the Canny algorithm. This algorithm consists of four steps, which are: smoothing by a Gaussian filter; determining the gradient; non-maximum suppression; and edge thresholding.
The Gaussian filter that is used in the command can be specified by filtersize and sigma. The default value for sigma is 2. The filtersize of Gaussian filter can only be an odd number. If the value of filtersize is not specified, filtersize is calculated by 2⁢2⁢σ+1. A 2⁢k+1 by 2⁢k+1 Gaussian filter is generated by following formula:
where 1≤i,j≤2⁢k+1 and H is the Gaussian filter. Then H is normalized by dividing by its sum.
In the second step, Maple determines the gradient in the horizontal and vertical direction and then combines these to obtain a single value for each pixel. The operator option specifies the convolution masks to be used to find the horizontal and vertical gradients. The default is Sobel3. The value of each convolution mask is listed below.
Sobel3: −101−202−101 and 121000−1−2−1
Sobel5: 1464128128200000−2−8−12−8−2−1−4−6−4−1 and −1−2021−4−8084−6−120126−4−8084−1−2021
Scharr: 30−3100−1030−3 and 3103000−3−10−3
The norm option specifies the norm to use in step 2 to combine the gradients in the horizontal and vertical directions. One can choose between the L1 and L2 norms. The L1 norm is the sum of the absolute value of the horizontal and vertical gradients, and the L2 norm is the square root of the sum of the squares of the gradient components. The default is L1.
The non-maximum suppression step is intended to ensure that only one edge is found for transitions that are more than one pixel wide.
In the final step of Canny algorithm, pixels where the value is greater than the high threshold are considered part of an edge. Pixels where the value is less than low threshold are not considered part of an edge. If the values lie between the low and high threshold, then they are considered part of an edge if they connect to an already accepted point.
The options low and high should be between 0 and 1. The default value for low and high is based on img. The value of high is calculated by assuming 70% of the pixels are not edges. The low threshold is 0.4 * high. The ratio 0.4 will always apply if only one of the low and high are provided. The low and high thresholds can be returned by specifying output to thresholds or "thresholds", or it can be viewed by setting infolevel[ImageTools] to 2 or higher.
We read in an image.
img ≔ Read⁡cat⁡kernelopts⁡datadir,/images/ship.jpg:
We determine its edges using Canny edge detection.
edge ≔ CannyEdgeDetect⁡img:
We can see the edges superimposed (in green) on the original image, as follows:
Embed⁡`~``+`⁡Mask⁡Create⁡Height⁡img,Width⁡img,3,background=0,1,0,edge,` $`,Mask⁡img,`~``-`⁡1,` $`,edge
In order to find out the thresholds used, we can either request them as output, or set the infolevel option.
infolevelImageTools ≔ 2
CannyEdgeDetect: thresholds used for Canny edge detection are: low=0.1125, high=0.2812
We can use a different operator to find the gradient by using the operator option, as follows. We also show how to explicitly specify the low and high thresholds for the last step.
edge2 ≔ CannyEdgeDetect⁡img,'low'=0.2,'high'=0.9,'operator'='Scharr'
CannyEdgeDetect: thresholds used for Canny edge detection are: low=0.2, high=0.9
The ImageTools[CannyEdgeDetect] command was introduced in Maple 2022.
For more information on Maple 2022 changes, see Updates in Maple 2022.
Download Help Document