Find gradient magnitude and direction of 2-D image
collapse all in page
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
, of the 2-D grayscale or binary image I
.
example
[Gmag,Gdir]= imgradient(I,method)
returns the gradient magnitude and direction using the specified method
.
example
[Gmag,Gdir]= imgradient(Gx,Gy)
returns the gradient magnitude and direction from the directional gradients Gx
and Gy
in the x and y directions, respectively.
Examples
collapse all
Calculate Gradient Magnitude and Direction Using Prewitt Method
Open Live Script
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.
imshowpair(Gmag,Gdir,"montage");title("Gradient Magnitude (Left) and Gradient Direction (Right) Using Prewitt Method")
Calculate Gradient Magnitude and Direction Using Directional Gradients
Open Live Script
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
I
— Input image
2-D grayscale image | 2-D binary image
Input image, specified as a 2-D grayscale or 2-D binary image.
Data Types: single
| double
| int8
| int32
| uint8
| uint16
| uint32
| logical
method
— Gradient operator
"sobel"
(default) | "prewitt"
| "central"
| "intermediate"
| "roberts"
Gradient operator, specified as one of the following values.
Method | Description |
---|---|
"sobel" | Sobel gradient operator. The gradient of a pixel is a weighted sum of pixels in the 3-by-3 neighborhood. For gradients in the vertical (y) direction, the weights are: [ 1 2 1 0 0 0 -1 -2 -1 ] |
"prewitt" | Prewitt gradient operator. The gradient of a pixel is a weighted sum of pixels in the 3-by-3 neighborhood. For gradients in the vertical (y) direction, the weights are: [ 1 1 1 0 0 0 -1 -1 -1 ] |
"central" | Central difference gradient. The gradient of a pixel is a weighted difference of neighboring pixels. In the y direction, |
"intermediate" | Intermediate difference gradient. The gradient of a pixel is the difference between an adjacent pixel and the current pixel. In the y direction, |
"roberts" | Roberts gradient operator. The gradient of a pixel is the difference between diagonally adjacent pixels. For gradients in one direction, the weights are: [ 1 0 0 -1 ] |
Data Types: char
| string
Gx
— Horizontal gradient
numeric matrix
Horizontal gradient, specified as a numeric matrix. The horizontal (x) axis points in the direction of increasing column subscripts. You can use the imgradientxy function to calculate Gx
.
Data Types: single
| double
| int8
| int32
| uint8
| uint16
| uint32
Gy
— Vertical gradient
numeric matrix
Vertical gradient, specified as a numeric matrix of the same size as Gx. The vertical (y) axis points in the direction of increasing row subscripts. You can use the imgradientxy function to calculate Gy
.
Data Types: single
| double
| int8
| int32
| uint8
| uint16
| uint32
Output Arguments
collapse all
Gmag
— Gradient magnitude
numeric matrix
Gradient magnitude, returned as a numeric matrix of the same size as image I or the directional gradients Gx and Gy. Gmag
is of class double
, unless the input image or directional gradients are of data type single
, in which case it is of data type single
.
Data Types: double
| single
Gdir
— Gradient direction
numeric matrix
Gradient direction, returned as a numeric matrix of the same size as gradient magnitude Gmag. 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 or directional gradients are of data type single
, in which case it is of data type 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, in the x and y directions, respectively. The horizontal (x) axis points in the direction of increasing column subscripts. The vertical (y) axis points in the direction of increasing row subscripts. 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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
imgradient
supports the generation of C code (requires MATLAB® Coder™). Note that if you choose the genericMATLAB Host Computer
target platform,imgradient
generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Types of Code Generation Support in Image Processing Toolbox.The value of
method
must be a compile time constant.The generated code does not always produce the same results as MATLAB for the
Gdir
output.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Image Processing on a GPU.
Version History
Introduced in R2012b
expand all
R2022b: Support for thread-based environments
imgradient
now supports thread-based environments.
R2021b: Generate C code using MATLAB Coder
imgradient
now supports the generation of C code (requires MATLAB Coder).
See Also
imgradientxy | imgradientxyz | imgradient3 | edge | fspecial
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office