Main Content

edge

Find edges in 2-D grayscale image

Description

BW = edge(I) returns a binary image BW containing 1s where the function finds edges in the grayscale or binary image I and 0s elsewhere. By default, edge uses the Sobel edge detection method.

Tip

To find edges in a 3-D grayscale or binary image, use the edge3 function.

example

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,Gx,Gy] = edge(___) also returns the directional gradients. For the Sobel and Prewitt methods, Gx and Gy correspond to the horizontal and vertical gradients, respectively. For the Roberts methods, Gx and Gy correspond to the gradient at angles of 135° and 45° 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)

Figure contains an axes object. The axes object contains an object of type image.

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

Figure contains an axes object. The axes object contains an object of type image.

Input Arguments

collapse all

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

For the "approxcanny" method, images of data type single or double must be normalized to the range [0, 1]. If I has values outside the range [0, 1], then you can use the rescale function to rescale values to the expected range.

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 to the range [0, 1].

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 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 numeric matrix. The h argument is supported by the "zerocross" method only.

Data Types: double

Standard deviation of the filter, specified as a numeric 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.

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

Horizontal gradient, returned as a numeric array of the same size as I. A large horizontal gradient magnitude indicates a strong vertical edge.

Note

If you select the Roberts method, then edge returns the gradient calculated at an angle of 135° from horizontal.

Vertical gradient, returned as a numeric array of the same size as I. A large vertical gradient magnitude indicates a strong horizontal edge.

Note

If you select the Roberts method, then edge returns the gradient calculated at an angle of 45° from horizontal.

Algorithms

  • For the gradient-magnitude edge detection methods (Sobel, Prewitt, and 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

Version History

Introduced before R2006a

expand all