 CannyEdgeDetect - Maple Help

ImageTools

 CannyEdgeDetect
 detect edge of an image by using Canny algorithm Calling Sequence CannyEdgeDetect(img) Parameters

 img - Image Options

 • 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") Description

 • 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 $\sqrt{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\mathrm{\sigma }⌉+1$.   A $2k+1$ by $2k+1$ Gaussian filter is generated by following formula:
 ${H}_{i,j}=\frac{{ⅇ}^{-\frac{{\left(i-k-1\right)}^{2}+{\left(j-k-1\right)}^{2}}{2{\mathrm{\sigma }}^{2}}}}{2\mathrm{\pi }\mathrm{\sigma }}$,
 where $1\le i,j\le 2k+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: $\left[\begin{array}{ccc}-1& 0& 1\\ -2& 0& 2\\ -1& 0& 1\end{array}\right]$ and $\left[\begin{array}{ccc}1& 2& 1\\ 0& 0& 0\\ -1& -2& -1\end{array}\right]$
 • Sobel5: $\left[\begin{array}{ccccc}1& 4& 6& 4& 1\\ 2& 8& 12& 8& 2\\ 0& 0& 0& 0& 0\\ -2& -8& -12& -8& -2\\ -1& -4& -6& -4& -1\end{array}\right]$  and $\left[\begin{array}{ccccc}-1& -2& 0& 2& 1\\ -4& -8& 0& 8& 4\\ -6& -12& 0& 12& 6\\ -4& -8& 0& 8& 4\\ -1& -2& 0& 2& 1\end{array}\right]$
 • Scharr: $\left[\begin{array}{ccc}3& 0& -3\\ 10& 0& -10\\ 3& 0& -3\end{array}\right]$ and $\left[\begin{array}{ccc}3& 10& 3\\ 0& 0& 0\\ -3& -10& -3\end{array}\right]$
 • 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. Examples

 > $\mathrm{with}\left(\mathrm{ImageTools}\right):$

 > $\mathrm{img}≔\mathrm{Read}\left(\mathrm{cat}\left(\mathrm{kernelopts}\left(\mathrm{datadir}\right),"/images/ship.jpg"\right)\right):$
 > $\mathrm{Embed}\left(\mathrm{img}\right)$ We determine its edges using Canny edge detection.

 > $\mathrm{edge}≔\mathrm{CannyEdgeDetect}\left(\mathrm{img}\right):$
 > $\mathrm{Embed}\left(\mathrm{edge}\right)$ We can see the edges superimposed (in green) on the original image, as follows:

 > $\mathrm{Embed}\left({\mathrm{~}}_{\mathrm{+}}\left(\mathrm{Mask}\left(\mathrm{Create}\left(\mathrm{Height}\left(\mathrm{img}\right),\mathrm{Width}\left(\mathrm{img}\right),3,\mathrm{background}=\left[0,1,0\right]\right),\mathrm{edge}\right),\mathrm{ },\mathrm{Mask}\left(\mathrm{img},{\mathrm{~}}_{\mathrm{-}}\left(1,\mathrm{ },\mathrm{edge}\right)\right)\right)\right)$ In order to find out the thresholds used, we can either request them as output, or set the infolevel option.

 > $\mathrm{CannyEdgeDetect}\left(\mathrm{img},'\mathrm{output}'='\mathrm{thresholds}'\right)$
 ${0.1125000000}{..}{0.2812500000}$ (5.1)
 > ${\mathrm{infolevel}}_{\mathrm{ImageTools}}≔2$
 ${{\mathrm{infolevel}}}_{{\mathrm{ImageTools}}}{≔}{2}$ (5.2)
 > $\mathrm{CannyEdgeDetect}\left(\mathrm{img}\right):$
 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.

 > $\mathrm{edge2}≔\mathrm{CannyEdgeDetect}\left(\mathrm{img},'\mathrm{low}'=0.2,'\mathrm{high}'=0.9,'\mathrm{operator}'='\mathrm{Scharr}'\right)$
 CannyEdgeDetect: thresholds used for Canny edge detection are: low=0.2, high=0.9 > $\mathrm{Embed}\left(\mathrm{edge2}\right)$  Compatibility

 • The ImageTools[CannyEdgeDetect] command was introduced in Maple 2022.