# Documentation

### This is machine translation

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

Gradient magnitude and direction of an image

## Syntax

``````[Gmag,Gdir] = imgradient(I)``````
``````[Gmag,Gdir] = imgradient(I,method)``````
``````[gpuarrayGmag,gpuarrayGdir] = imgradient(gpuarrayI,___)``````
``````[Gmag,Gdir] = imgradient(Gx,Gy)``````
``````[gpuarrayGmag,gpuarrayGdir] = imgradient(gpuarrayGx,gpuarrayGy)``````

## 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(Gx,Gy)``` 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');`

Display the gradient magnitude and direction.

```figure imshowpair(Gmag, Gdir, 'montage'); title('Gradient Magnitude, Gmag (left), and Gradient Direction, Gdir (right), using Prewitt method')```

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

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

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

Calculate x- and y-directional gradients, using the Sobel gradient operator by default.

`[Gx, Gy] = imgradientxy(I);`

```figure imshowpair(Gx, Gy, 'montage') title('Directional Gradients, Gx and Gy, using Sobel method')```

`[Gmag, Gdir] = imgradient(Gx, Gy);`

Display the gradient magnitude and direction.

```figure imshowpair(Gmag, Gdir, 'montage') title('Gradient Magnitude, Gmag (left), and Gradient Direction, Gdir (right), 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

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`

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

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

Gradient operator, specified as one of the following values.

MethodDescription
`'sobel'`Sobel gradient operator (default)
`'prewitt'`Prewitt gradient operator
`'central'`

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

`'intermediate'`

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

`'roberts'`Roberts gradient operator

Data Types: `char`

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`

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`

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

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

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

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`

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`

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`

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`

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

`imgradient` does not normalize the gradient output. If the range of the gradient output image has to match the range of the input image, consider normalizing the gradient image, depending on the `method` argument used. For example, with a Sobel kernel, the normalization factor is 1/8, for Prewitt, it is 1/6, and for Roberts it is 1/2.