# edge

Find edges in intensity image

## Syntax

• `BW = edge(I)`
• `BW = edge(I,method)` example
• `BW = edge(I,method,threshold)`
• `BW = edge(I,method,threshold,direction)`
• `BW = edge(I,method,threshold,direction,'nothinning')`
• `BW = edge(I,method,threshold,direction,sigma)`
• ```[BW,threshOut] = edge(___)```
• ```[gpuarrayBW,threshOut] = edge(gpuarrayI,___)``` example

## Description

````BW = edge(I)` returns a binary image `BW` containing `1`s where the function finds edges in the input image `I` and `0`s elsewhere. By default, `edge` uses the Sobel edge detection method.This function supports code generation (see Tips).```

example

````BW = edge(I,method)` detects edges in image `I`, where `method` specifies the edge detection method used.```
````BW = edge(I,method,threshold)` detects edges in image `I`, where `threshold` specifies the sensitivity threshold. `edge` ignores all edges that are not stronger than `threshold`.```
````BW = edge(I,method,threshold,direction)` detects edges in image `I`, where `direction` specifies the direction in which the function looks for edges in the image: horizontally, vertically, or in both directions. Used only with the Sobel and Prewitt methods.```
````BW = edge(I,method,threshold,direction,'nothinning')` detects edges in image `I`, where `‘nothinning' ` speeds up the operation of the algorithm by skipping the additional edge-thinning stage. By default, the algorithm applies edge thinning. Used only with the Sobel, Prewitt, and Roberts methods. ```
````BW = edge(I,method,threshold,direction,sigma)` detects edges in image `I`, where `sigma` specifies the standard deviation of the filter. Used only with the Laplacian of Gaussian and Canny methods. ```
``````[BW,threshOut] = edge(___)``` returns the threshold value.```

example

``````[gpuarrayBW,threshOut] = edge(gpuarrayI,___)``` performs the edge detection operation on a GPU. The input image and the output image are gpuArrays. This syntax requires Parallel Computing Toolbox™. ```

## Examples

collapse all

### Compare Edge Detection Using Canny and Prewitt Methods

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 grayscale image, creating a gpuArray.

`I = gpuArray(imread('circuit.tif'));`

Find edges using the Prewitt method.

```BW = edge(I,'prewitt'); ```

Display results.

`figure, imshow(BW)`

## Input Arguments

collapse all

### `I` — Input intensity or binary image2-D, real, nonsparse numeric or logical array

Input intensity or binary image, specified as a 2-D, real, nonsparse, numeric, or logical array.

Example: `I = imread('circuit.tif'); BW = edge(I);`

Data Types: `single` | `double` | `int16` | `uint8` | `uint16` | `logical`

### `method` — Edge detection method`‘Sobel'` (default) | string

Edge detection method, specified as one of the following strings.

Method Description
`'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.

Not supported on a GPU.

`‘log'` (Laplacian of Gaussian)Finds edges by looking for zero-crossings after filtering `I` with a Laplacian of Gaussian filter.
`'Prewitt`'Finds edges using the Prewitt approximation to the derivative. It returns edges at those points where the gradient of `I` is maximum.
`'Roberts'`Finds edges using the Roberts approximation to the derivative. It returns edges at those points where the gradient of `I` is maximum.
`'Sobel'`Finds edges using the Sobel approximation to the derivative. It returns edges at those points where the gradient of I is maximum.
`'zerocross'`Finds edges by looking for zero-crossings after filtering I with a filter that you specify.

Example: `BW = edge(I,'Canny');`

Data Types: `char`

### `threshold` — Sensitivity thresholdscalar | two-element vector (Canny method only)

Sensitivity threshold, specified as a numeric scalar or, for the Canny method only, a two-element vector. `edge` ignores all edges that are not stronger than `threshold`. If you do not specify `threshold`, or if you specify an empty array (`[]`), `edge` chooses the value automatically. For information about this parameter, see Tips.

For the `‘log'` (Laplacian of Gaussian) and `'zerocross'` methods, if you specify the threshold value `0`, the output image has closed contours because it includes all of the zero-crossings in the input image.

Example: `BW = edge(I,'Prewitt',3);`

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

### `direction` — Direction of detection`‘both'` (default) | `'horizontal'` | `'vertical'`

Direction of detection, specified as the string `'horizontal'`, `'vertical'`, or `'both'`. Supported only by the Sobel and Prewitt methods.

Example: `BW = edge(I,'Prewitt',3,'horizontal');`

Data Types: `char`

### `sigma` — Standard deviation of the filter`2` (default) | scalar

Standard deviation of the filter, specified as a scalar. The following table describes this option for each method that supports it.

MethodDescription
Canny

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

Laplacian of Gaussian (log)

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

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

Example: `BW = edge(I,'Canny',3,5);`

Data Types: `double`

### `gpuarrayI` — Input imagegpuArray

Input image, specified as a gpuArray.

Example: ```gpuarrayI = gpuArray(imread('circuit.tif')); gpuarrayBW = edge(gpuarrayI,'prewitt');```

## Output Arguments

collapse all

### `BW` — Output binary imagelogical array

Output binary image, returned as a logical array, the same size as `I`, with `1`s where the function finds edges in `I` and `0`s elsewhere.

### `threshOut` — Threshold value used in the computationnumeric scalar

Threshold value used in the computation, returned as a numeric scalar.

### `gpuarrayBW` — Output binary image when run on a GPUgpuArray

Output binary image when run on a GPU, returned as a gpuArray.

collapse all

### Tips

• This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic `MATLAB Host Computer` target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

When generating code, note the following:

• The `method`, `direction`, and `sigma` arguments must be compile-time constants.

• Nonprogrammatic syntaxes are not supported. For example, if you do not specify a return value,`edge` displays an image. This syntax is not supported.

• Notes about the `threshold` parameter:

• 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 isn't.

• 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 (fewer edge pixels) or lower (more edge pixels).

• 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',...)`
• The syntax `BW = edge(... ,K)` has been removed. Use the `BW = edge(... ,direction)` syntax instead.

• The syntax `edge(I,'marr-hildreth',...)` has been removed. Use the `edge(I,'log',...)` syntax instead.

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