# Documentation

Gradient magnitude and direction of an image

## Syntax

• ```[Gmag,Gdir] = imgradient(I)```
• ```[Gmag,Gdir] = imgradient(I,method)``` example
• ```[gpuarrayGmag,gpuarrayGdir] = imgradient(gpuarrayI,___)``` example
• ```[Gmag,Gdir] = imgradient(GxGy)``` example
• ```[gpuarrayGmag,gpuarrayGdir] = imgradient(gpuarrayGx,gpuarrayGy)``` example

## Description

``````[Gmag,Gdir] = imgradient(I)``` returns the gradient magnitude, `Gmag`, and the gradient direction, `Gdir`, for the grayscale or binary image `I`. ```

example

``````[Gmag,Gdir] = imgradient(I,method)``` returns the gradient magnitude and direction using specified `method`.```

example

``````[gpuarrayGmag,gpuarrayGdir] = imgradient(gpuarrayI,___)``` performs the operation on a GPU. The input image and the return values are gpuArrays. This syntax requires the Parallel Computing Toolbox™.```

example

``````[Gmag,Gdir] = imgradient(GxGy)``` returns the gradient magnitude and direction using directional gradients along the x-axis, `Gx`, and the y-axis, `Gy`, such as that returned by `imgradientxy`. The x-axis points in the direction of increasing column subscripts and the y-axis points in the direction of increasing row subscripts.```

example

``````[gpuarrayGmag,gpuarrayGdir] = imgradient(gpuarrayGx,gpuarrayGy)``` performs the operation on a GPU. The input x and y gradients and the return values are gpuArrays. This syntax requires the Parallel Computing Toolbox.```

## Examples

collapse all

```I = imread('coins.png'); ```

```[Gmag, Gdir] = imgradient(I,'prewitt'); figure; imshowpair(Gmag, Gdir, 'montage'); title('Gradient Magnitude, Gmag (left), and Gradient Direction, Gdir (right), using Prewitt method') axis off;```

```I = gpuArray(imread('coins.png')); imshow(I)```

```[Gmag, Gdir] = imgradient(I,'prewitt'); figure, imshow(Gmag, []), title('Gradient magnitude') figure, imshow(Gdir, []), title('Gradient direction')```

Read image and return directional gradients, `Gx` and `Gy`, as well as gradient magnitude and direction, `Gmag` and `Gdir`, utilizing default Sobel gradient operator.

```I = imread('coins.png'); ```

```[Gx, Gy] = imgradientxy(I); [Gmag, Gdir] = imgradient(Gx, Gy); figure, imshow(Gmag, []), title('Gradient magnitude') figure, imshow(Gdir, []), title('Gradient direction') title('Gradient Magnitude (Gmag) and Gradient Direction (Gdir) using Sobel method') figure; imshowpair(Gx, Gy, 'montage'); axis off; title('Directional Gradients, Gx and Gy, using Sobel method') ```

Read image and return directional gradients, `Gx` and `Gy`, as well as gradient magnitude and direction, `Gmag` and `Gdir`, utilizing default Sobel gradient operator.

```I = gpuArray(imread('coins.png')) ```

Calculate gradients and display them. Note that when you specify a gpuArray to `imgradientxy`, it returns `Gx` and `Gy` as gpuArrays. The results are the same as the previous example.

```[Gx, Gy] = imgradientxy(I); [Gmag, Gdir] = imgradient(Gx, Gy); figure, imshow(Gmag, []), title('Gradient magnitude') figure, imshow(Gdir, []), title('Gradient direction') figure, imshow(Gx, []), title('Directional gradient: X axis') figure, imshow(Gy, []), title('Directional gradient: Y axis')```

## Input Arguments

collapse all

### `I` — Input imagegrayscale image | binary image

Input image, specified as a grayscale or binary image, that is, a numeric or logical 2-D matrix that must be nonsparse.

Data Types: `single` | `double` | `int8` | `int32` | `uint8` | `uint16` | `uint32` | `logical`

### `gpuarrayI` — Input imagegpuArray

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

Data Types: `single` | `double` | `int8` | `int32` | `uint8` | `uint16` | `uint32` | `logical`

### `method` — Gradient operator`‘Sobel'` (default) | `‘Prewitt'` | `'CentralDifference'` | `'IntermediateDifference'` | `‘Roberts'`

Gradient operator, specified as one of the text strings in the following table.

MethodDescription
`‘Sobel'`Sobel gradient operator (default)
`‘Prewitt'`Prewitt gradient operator
`'CentralDifference'`,

Central difference gradient: ``` dI/dx = (I(x+1) - I(x-1))/2```

`'IntermediateDifference'`

Intermediate difference gradient: ``` dI/dx = I(x+1) - I(x)```

`‘Roberts'`Roberts gradient operator

Data Types: `char`

### `Gx` — Directional gradients along x-axis (horizontal) matrix

Directional gradient along x-axis (horizontal), specified as non-sparse matrix equal in size to image `I`, typically returned by `imgradientxy`.

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

### `Gy` — Directional gradients along the y-axis (vertical)matrix

Directional gradient along y-axis (vertical), specified as non-sparse matrix equal in size to image `I`, typically returned by `imgradientxy`.

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

### `gpuarrayGx` — Directional gradients along x-axis gpuArray

Directional gradient along x-axis, specified as a gpuArray, typically returned by `imgradientxy`.

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

### `gpuarrayGy` — Directional gradients along the y-axisgpuArray

Directional gradient along y-axis, specified as a gpuArray, typically returned by `imgradientxy`.

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

## Output Arguments

collapse all

### `Gmag` — Gradient magnitudematrix

Gradient magnitude, returned as a non-sparse matrix the same size as image `I`. `Gmag` is of class `double`, unless the input image `I` is of class `single`, in which case it is of class `single`.

Data Types: `double` | `single`

### `gpuarrayGmag` — Gradient magnitudegpuArray

Gradient magnitude, returned as a non-sparse gpuArray the same size as image `I`. `Gmag` is of class `double`, unless the input image `I` is of class `single`, in which case it is of class `single`.

Data Types: `double` | `single`

### `Gdir` — Gradient directionmatrix | gpuArray

Gradient direction, returned as a nonsparse matrix the same size as image `I`. `Gdir` contains angles in degrees within the range [-180 180] measured counterclockwise from the positive x-axis. (The x-axis points in the direction of increasing column subscripts.) `Gdir` is of class double, unless the input image `I` is of class single, in which case it is of class single.

When `I` or `Gx` and `Gy` are gpuArrays, `Gdir` is a gpuArray.

Data Types: `double` | `single`

### `gpuarrayGdir` — Gradient directiongpuArray

Gradient direction, returned as a nonsparse gpuArray the same size as image `I`. `Gdir` contains angles in degrees within the range [-180 180] measured counterclockwise from the positive x-axis. (The x-axis points in the direction of increasing column subscripts.) `Gdir` is of class double, unless the input image `I` is of class single, in which case it is of class single.

Data Types: `double` | `single`

collapse all

### Tips

• When applying the gradient operator at the boundaries of the image, values outside the bounds of the image are assumed to equal the nearest image border value. This is similar to the `'replicate'` boundary option in `imfilter`.

### Algorithms

The algorithmic approach taken in `imgradient` for each of the listed gradient methods is to first compute directional gradients, `Gx` and `Gy`, with respect to the x-axis and y-axis. The x-axis is defined along the columns going right and the y-axis is defined along the rows going down. The gradient magnitude and direction are then computed from their orthogonal components `Gx` and `Gy`.