imgradientxy

Directional gradients of an image

Syntax

  • [Gx,Gy] = imgradientxy(I) example
  • [Gx,Gy] = imgradientxy(I,method) example
  • [gpuarrayGx,gpuarrayGy] = imgradientxy(gpuarrayI,___) example

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.

example

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

example

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

Examples

expand all

Calculate directional gradients

Read image.

I = imread('coins.png');

Calculate gradient magnitude and gradient direction using Prewitt's gradient operator

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

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

Calculate directional gradients on a GPU

Read image into a gpuArray.

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

Calculate gradient magnitude and gradient direction using Prewitt's gradient operator and display images.

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

figure, imshow(Gx, []), title('Directional gradient: X axis')
figure, imshow(Gy, []), title('Directional gradient: Y axis')

Display gradient magnitude and direction in addition to directional gradients

Read image and return directional gradients, Gx and Gx, 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; imshowpair(Gmag, Gdir, 'montage'); axis off;
title('Gradient Magnitude, Gmag (left), and Gradient Direction, Gdir (right), using Sobel method')
figure; imshowpair(Gx, Gy, 'montage'); axis off;
title('Directional Gradients, Gx and Gy, using Sobel method')

Calculate gradient magnitude and direction in addition to directional gradients on a GPU

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

Read image into a gpuArray.

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

Calculate gradient and display images.

[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

expand 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, or a gpuArray.

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'

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)

Data Types: char

Output Arguments

expand all

Gx — Directional gradients along x-axismatrix

Directional gradient along the x-axis, returned as non-sparse 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.

When the input image I is a gpuArray, Gx is a gpuArray.

Data Types: single | double

gpuarrayGx — Directional gradients along x-axisgpuArray

Directional gradient along the x-axis, returned as non-sparse gpuArray 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.

Data Types: single | double

Gy — Directional gradient along they-axismatrix

Directional gradients along the y-axis, returned as non-sparse 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.

Data Types: single | double

gpuarrayGy — Directional gradient along they-axisgpuArray

Directional gradients along the y-axis, returned as non-sparse gpuArray 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.

Data Types: single | double

More About

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

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.

Was this topic helpful?