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.

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

example

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 hidden 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 hidden 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 nonnegative 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.

  • If you do not specify threshold, or if you specify an empty array ([]), then edge chooses the value or values heuristically and automatically, depending on the input data. The best way to vary the threshold is to run the function once, outputting the calculated threshold as the threshOut argument. Then, starting from the calculated threshold value, increase or decrease the threshold to detect fewer or more edge pixels, respectively.

  • For the "Sobel", "Prewitt", and "Roberts" methods, the function uses the threshold value to determine the magnitude of the gradient to consider as an edge. A threshold value of 0 selects all edges in the image, even if they have very low strength.

  • For the "log" and "zerocross" methods, the function uses the threshold value to determine the magnitude of the zero-crossing to consider as an edge. In other words, when the threshold is high, a large jump across zero is an edge, while a small jump is not. If you specify a threshold value of 0, then the output image has closed contours because it includes every zero-crossing in the input image.

  • The "Canny" and "approxcanny" methods use two thresholds for the gradient: a high threshold for low edge sensitivity and a low threshold for high edge sensitivity. Edge detection starts with the low sensitivity result, and then grows to include connected edge pixels from the high sensitivity result. This helps fill in gaps in the detected edges. The function 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 as the higher threshold value. In this case, edge calculates the lower threshold value as threshold*0.4.

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.

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