This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

imgradient

Gradient magnitude and direction of an image

Syntax

[Gmag,Gdir] = imgradient(I)
[Gmag,Gdir] = imgradient(I,method)
[Gmag,Gdir] = imgradient(Gx,Gy)

Description

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

You optionally can compute the gradient magnitude and direction using a GPU (requires Parallel Computing Toolbox™). For more information, see Image Processing on a GPU.

example

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

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.

Examples

collapse all

Read an image into workspace.

I = imread('coins.png');

Calculate the gradient magnitude and direction, specifying the Prewitt gradient operator.

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

Read image and compute gradient magnitude and gradient direction using Prewitt’s gradient operator.

Read image.

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

Calculate gradients and display.

[Gmag, Gdir] = imgradient(I,'prewitt');

figure, imshow(Gmag, []), title('Gradient magnitude')
figure, imshow(Gdir, []), title('Gradient direction')

Read an image into workspace.

I = imread('coins.png');

Calculate the x- and y-directional gradients. By default, imgradientxy uses the Sobel gradient operator.

[Gx,Gy] = imgradientxy(I);

Display the directional gradients.

imshowpair(Gx,Gy,'montage')
title('Directional Gradients Gx and Gy, Using Sobel Method')

Calculate the gradient magnitude and direction using the directional gradients.

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

Display the gradient magnitude and direction.

imshowpair(Gmag,Gdir,'montage')
title('Gradient Magnitude (Left) and Gradient Direction (Right)')

Input Arguments

collapse all

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

To calculate the gradient magnitude and direction using a GPU, specify I as a gpuArray that contains a grayscale or binary 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 | string

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

To calculate the gradient magnitude and direction using a GPU, specify Gx as a gpuArray that contains a numeric matrix.

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

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

To calculate the gradient magnitude and direction using a GPU, specify Gy as a gpuArray that contains a numeric matrix.

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

Output Arguments

collapse all

Gradient magnitude, returned as a numeric 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.

If you use a GPU to compute the gradient magnitude and direction, then Gmag is returned as a gpuArray that contains a numeric matrix.

Data Types: double | single

Gradient direction, returned as a numeric 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.

If you use a GPU to compute the gradient magnitude and direction, then Gdir is returned as a gpuArray that contains a numeric matrix.

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.

Extended Capabilities

Introduced in R2012b