This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

edge

Find edges in intensity image

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(___)

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.

You optionally can find edges using a GPU (requires Parallel Computing Toolbox™). For more information, see Image Processing on a GPU.

example

BW = edge(I,method) detects edges in image I using the edge-detection algorithm specified by method.

The 'Canny' and 'approxcanny' methods are not supported on a GPU.

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'.

Examples

collapse all

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')

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

collapse all

Input image, specified as a 2-D grayscale or binary image.

For the 'approxcanny' method, images of datatype single or double must be normalized in the range [0 1].

To find edges using a GPU, specify I as a gpuArray that contains a grayscale or binary image.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Edge detection method, specified as one of the following.

MethodDescription
'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.
'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 derivative of a Gaussian filter. This method uses two thresholds to detect strong and weak edges, including weak edges in the output if they are connected to strong edges. By using two thresholds, the Canny method is less likely than the other methods to be fooled by noise, and more 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 expense of less precise detection. Floating point images are expected to be normalized in the range [0 1].

Note

The 'Canny' and 'approxcanny' methods are not supported on a GPU.

Sensitivity threshold, specified as a numeric scalar for any method, or a 2-element vector for the 'Canny' and 'approxcanny' methods. edge ignores all edges that are not stronger than threshold. For more information about this parameter, see Algorithm.

  • If you do not specify threshold, or if you specify an empty array ([]), then edge chooses the value or values automatically.

  • For the 'log' and 'zerocross' methods, if you specify the threshold value 0, then the output image has closed contours because it includes all the zero-crossings in the input image.

  • The 'Canny' and 'approxcanny' methods use two thresholds. edge disregards all edges with edge strength below the lower threshold, and preserves all edges with edge strength above the higher threshold. You can specify threshold as a 2-element vector of the form [low high] with low and high values in the range [0 1]. You can also specify threshold as a numeric scalar, which edge assigns to the higher threshold. In this case, edge uses threshold*0.4 as the lower threshold.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Direction of edges to detect, specified as 'horizontal', 'vertical', or 'both'. The direction argument is only valid when the method is 'Sobel', 'Prewitt', or 'Roberts'.

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

Filter, specified as a matrix. The h argument is supported by the 'zerocross' method only.

Data Types: double

Standard deviation of the filter, specified as a scalar. The sigma argument is supported by the 'Canny' and 'log' methods only.

MethodDescription
'Canny'

Scalar value that specifies the standard deviation of the Gaussian filter. The default is sqrt(2). edge chooses the size of the filter automatically, based on sigma.

'log' (Laplacian of Gaussian)

Scalar value that specifies the standard deviation of the Laplacian of Gaussian filter. The default is 2. The size of the filter is n-by-n, where n=ceil(sigma*3)*2+1.

Data Types: double

Output Arguments

collapse all

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.

If you use a GPU to find the edges, then BW is returned as a gpuArray that contains a logical array.

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.

If you use a GPU to find the edges, then threshOut is returned as a gpuArray that contains a numeric scalar.

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.

If you use a GPU to find the edges, then Gv is returned as a gpuArray that contains a 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.

If you use a GPU to find the edges, then Gh is returned as a gpuArray that contains a numeric array.

Tips

  • The function edge changed in Version 7.2 (R2011a). Previous versions of the Image Processing Toolbox™ used a different algorithm for computing the Canny method. If you need the same results produced by the previous implementation, use the following syntax: BW = edge(I,'canny_old',___)

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.

References

[1] Canny, John, "A Computational Approach to Edge Detection," IEEE Transactions on Pattern Analysis and Machine Intelligence, Vol. PAMI-8, No. 6, 1986, pp. 679-698.

[2] Lim, Jae S., Two-Dimensional Signal and Image Processing, Englewood Cliffs, NJ, Prentice Hall, 1990, pp. 478-488.

[3] Parker, James R., Algorithms for Image Processing and Computer Vision, New York, John Wiley & Sons, Inc., 1997, pp. 23-29.

Extended Capabilities

Introduced before R2006a