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.

imgradientxy

Directional gradients of an image

Syntax

[Gx,Gy] = imgradientxy(I)
[Gx,Gy] = imgradientxy(I,method)

Description

example

[Gx,Gy] = imgradientxy(I) returns the directional gradients, Gx and Gy, the same size as the input image I.

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.

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

example

[Gx,Gy] = imgradientxy(I,method) returns the directional gradients using the specified method.

Examples

collapse all

Read an image into workspace.

I = imread('coins.png');

Calculate the x- and y-directional gradients using the Prewitt gradient operator.

[Gx, Gy] = imgradientxy(I,'prewitt');

Display the directional gradients.

figure
imshowpair(Gx, Gy, 'montage');
title('Directional Gradients: x-direction, Gx (left), y-direction, Gy (right), using Prewitt method')

Read an image into a gpuArray.

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

Calculate gradient magnitude and gradient direction using Prewitt’s gradient operator, and display the images.

[Gx, Gy] = imgradientxy(I,'prewitt');
figure, imshow(Gx, []), title('Directional gradient: X axis')
figure, imshow(Gy, []), title('Directional gradient: Y axis')

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 non-sparse numeric or logical 2-D matrix.

To calculate the directional gradients 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)

Data Types: char | string

Output Arguments

collapse all

Directional gradient along the x-axis, returned as a numeric matrix equal in size to image I. The x-axis points in the direction of increasing column subscripts. The output matrices are of class double, unless the input image is of class single, in which case they are of class single.

If you use a GPU to compute the directional gradients, then Gx is returned as a gpuArray that contains a numeric matrix.

Data Types: single | double

Directional gradients along the y-axis, returned as a numeric matrix equal in size to image I. The y-axis points in the direction of increasing row subscripts. The output matrices are of class double, unless the input image is of class single, in which case they are of class single.

If you use a GPU to compute the directional gradients, then Gy is returned as a gpuArray that contains a numeric matrix.

Data Types: single | double

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.

Algorithms

The algorithmic approach is to compute directional gradients 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.

imgradientxy 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, and for Prewitt, it is 1/6.

Extended Capabilities

Introduced in R2012b