Edge
Edge
Syntax
BW = edge(I)
BW = edge(I,method)
BW = edge(I,method,threshold)
BW = edge(I,method,threshold,direction)
BW = edge(___,'nothinning')
BW = edge(I,method,threshold,sigma)
BW = edge(I,method,threshold,h)
[BW,threshOut] = edge(___)
[BW,threshOut,Gv,Gh] = edge(___)
[gpuarrayBW,threshOut] = edge(gpuarrayI,___)
Description
BW = edge(I) returns a binary image BW containing 1s where the function finds edges in the input
image I and 0s elsewhere. By default, edge uses the Sobel edge detection method.
exa
BW = edge(I,method) detects edges in image I using the edge-detection algorithm specified by method.
BW = edge(I,method,threshold) returns all edges that are stronger than threshold.
BW = edge(I,method,threshold,direction) specifies the orientation of edges to detect. The Sobel and
Prewitt methods can detect edges in the vertical direction, horizontal direction, or both. The Roberts method
can detect edges at angles of 45° from horizontal, 135° from horizontal, or both. This syntax is valid only
when method is 'Sobel', 'Prewitt', or 'Roberts'.
BW = edge(___,'nothinning') skips the edge-thinning stage, which can improve performance. This syntax
is valid only when method is 'Sobel', 'Prewitt', or 'Roberts'.
BW = edge(I,method,threshold,sigma) specifies sigma, the standard deviation of the filter. This syntax is
valid only when method is 'log' or 'Canny'.
BW = edge(I,method,threshold,h) detects edges using the 'zerocross' method with a filter, h, that you
specify. This syntax is valid only when method is 'zerocross'.
[BW,threshOut] = edge(___) also returns the threshold value.
[BW,threshOut,Gv,Gh] = edge(___) also returns the directional gradient magnitudes. For the Sobel and
Prewitt methods, Gv and Gh correspond to the vertical and horizontal gradients. For the Roberts
methods, Gv and Gh correspond to the gradient at angles of 45° and 135° from horizontal, respectively. This
syntax is valid only when method is 'Sobel','Prewitt', or 'Roberts'.
exa
[gpuarrayBW,threshOut] = edge(gpuarrayI,___) performs the edge detection operation on a GPU.
The 'Canny' and 'approxcanny' methods are not supported on a GPU. The input image and the output
image are gpuArrays. This syntax requires Parallel Computing Toolbox™.
Examples
Compare Edge Detection Using Canny and Prewitt Methods
Try This Example
Read a grayscale image into the workspace and display it.
I = imread('circuit.tif');
imshow(I)
Find edges using the Canny method.
BW1 = edge(I,'Canny');
Find edges using the Prewitt method.
BW2 = edge(I,'Prewitt');
Display both results side-by-side.
imshowpair(BW1,BW2,'montage')
Find Edges Using Prewitt Method on a GPU
Read a grayscale image, creating a gpuArray.
I = gpuArray(imread('circuit.tif'));
Find edges using the Prewitt method.
BW = edge(I,'prewitt');
Display the results.
figure, imshow(BW)
Input Arguments
I — Grayscale or binary image
2-D, real, nonsparse, numeric or logical array
Grayscale or binary image, specified as a 2-D, real, nonsparse, numeric or logical array.
For the 'approxcanny' method, images of datatype single or double must be normalized in the range [0
1].
Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
method — Edge detection method
'Sobel' (default) | 'Prewitt' | 'Roberts' | 'log' | 'zerocross' | 'Canny' | 'approxcanny'
Edge detection method, specified as one of the following.
Method Description
'Sobel' Finds edges at those points where the gradient of the image I is maximum, using the Sobel approximation to the derivative.
'Prewitt' Finds edges at those points where the gradient of I is maximum, using the Prewitt approximation to the derivative.
'Roberts' Finds edges at those points where the gradient of I is maximum, using the Roberts approximation to the derivative.
'log' Finds edges by looking for zero-crossings after filtering I with a Laplacian of Gaussian (LoG) filter.
Method Description
'zerocross' Finds edges by looking for zero-crossings after filtering I with a filter that you specify, h
'Canny' Finds edges by looking for local maxima of the gradient of I. The edge function calculates the gradient using the derivativ
weak edges, including weak edges in the output if they are connected to strong edges. By using two thresholds, the Canny m
likely to detect true weak edges.
'approxcanny' Finds edges using an approximate version of the Canny edge detection algorithm that provides faster execution time at the e
point images are expected to be normalized in the range [0 1].
Note
If you select the Roberts method, then the 'horizontal' direction actually detects edges at an angle of 135°
from horizontal, and the 'vertical' direction detects edges at an angle of 45° from horizontal.
Data Types: char | string
h — Filter
matrix
Filter, specified as a matrix. The h argument is supported by the 'zerocross' method only.
Data Types: double
sigma — Standard deviation of the filter
scalar
Standard deviation of the filter, specified as a scalar. The sigma argument is supported by
the 'Canny' and 'log' methods only.
Method Description
'Canny' Scalar value that specifies the standard deviation of the Gaussian filter. The default is sqrt(2). edge cho
'log' (Laplacian of Gaussian) Scalar value that specifies the standard deviation of the Laplacian of Gaussian filter. The default is 2. The s
Data Types: double
gpuarrayI — Input image
gpuArray
Input image, specified as a gpuArray.
Output Arguments
BW — Output binary image
logical array
Output binary image, returned as a logical array of the same size as I, with 1s where the function finds edges
in I and 0s elsewhere.
threshOut — Threshold value used in the computation
numeric scalar | 2-element vector | []
Threshold value used in the computation, returned as a 2-element vector for the 'Canny' method, an empty
vector ([]) for the 'approxcanny' method, or a numeric scalar for all other edge detection methods.
Gv — Vertical gradient
numeric array
Vertical gradient, returned as a numeric array of the same size as I.
Note
If you select the Roberts method, then edge returns the gradient calculated at an angle of 45° from horizontal.
Gh — Horizontal gradient
numeric array
Horizontal gradient, returned as a numeric array of the same size as I.
Note
If you select the Roberts method, then edge returns the gradient calculated at an angle of 135° from
horizontal.
gpuarrayBW — Output binary image when run on a GPU
gpuArray
Output binary image when run on a GPU, returned as a gpuArray.
Algorithms
For the gradient-magnitude edge detection methods (Sobel, Prewitt, Roberts), edge uses threshold to
threshold the calculated gradient magnitude.
For the zero-crossing methods, including Laplacian of Gaussian, edge uses threshold as a threshold for the
zero-crossings. In other words, a large jump across zero is an edge, while a small jump is not.
The Canny method applies two thresholds to the gradient: a high threshold for low edge sensitivity and a low
threshold for high edge sensitivity. edge starts with the low sensitivity result and then grows it to include
connected edge pixels from the high sensitivity result. This helps fill in gaps in the detected edges.
In all cases, edge chooses the default threshold heuristically, depending on the input data. The best way to
vary the threshold is to run edge once, capturing the calculated threshold as the second output argument.
Then, starting from the value calculated by edge, adjust the threshold higher to detect fewer edge pixels, or
lower to detect more edge pixels.