Find gradient magnitude and direction of 3-D image

collapse all in page

## Syntax

`[Gmag,Gazimuth,Gelevation]= imgradient3(I)`

`[Gmag,Gazimuth,Gelevation]= imgradient3(I,method)`

`[Gmag,Gazimuth,Gelevation]= imgradient3(Gx,Gy,Gz)`

## Description

example

`[Gmag,Gazimuth,Gelevation]= imgradient3(I)`

returns the gradient magnitude, `Gmag`

, gradient direction, `Gazimuth`

, and gradient elevation `Gelevation`

of the 3-D grayscale or binary image `I`

.

`[Gmag,Gazimuth,Gelevation]= imgradient3(I,method)`

calculates the gradient magnitude, direction, and elevation using the specified `method`

.

`[Gmag,Gazimuth,Gelevation]= imgradient3(Gx,Gy,Gz)`

calculates the gradient magnitude, direction, and elevation from the directional gradients `Gx`

, `Gy`

, and `Gz`

in the *x*, *y*, and *z* directions, respectively.

## Examples

collapse all

### Compute 3-D Gradient Magnitude and Direction Using Sobel Method

Open Live Script

Read 3-D data into the workspace and prepare it for processing.

`volData = load('mri');sz = volData.siz;vol = squeeze(volData.D);`

Calculate the gradients.

[Gmag, Gaz, Gelev] = imgradient3(vol);

Visualize the gradient magnitude as a montage.

figure, montage(reshape(Gmag,sz(1),sz(2),1,sz(3)),'DisplayRange',[])title('Gradient magnitude')

## Input Arguments

collapse all

`I`

— Input image

3-D grayscale image | 3-D binary image

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

`method`

— Gradient operator

`"sobel"`

(default) | `"prewitt"`

| `"central"`

| `"intermediate"`

Gradient operator, specified as one of the following values.

Value | Meaning | ||||||
---|---|---|---|---|---|---|---|

| Sobel gradient operator. The gradient of a pixel is a weighted sum of pixels in the 3-by-3-by-3 neighborhood. For example, in the depth (
| ||||||

| Prewitt gradient operator. The gradient of a pixel is a weighted sum of pixels in the 3-by-3-by-3 neighborhood. For example, in the depth (
| ||||||

`"central" ` | Central difference gradient. The gradient of a pixel is a weighted difference of neighboring pixels. For example, in the depth ( | ||||||

`"intermediate"` | Intermediate difference gradient. The gradient of a pixel is the difference between an adjacent pixel and the current pixel. For example, in the depth ( |

When applying the gradient operator at the boundaries of the image, `imgradient3`

assumes values outside the bounds of the image equal the nearest image border value. This behavior is similar to the `"replicate"`

boundary option in `imfilter`

.

**Data Types: **`char`

| `string`

`Gx`

— Horizontal gradient

3-D numeric array

Horizontal gradient, specified as a 3-D numeric array. The horizontal (*x*) axis points in the direction of increasing column subscripts. You can use the imgradientxyz function to calculate `Gx`

.

`Gy`

— Vertical gradient

3-D numeric array

Vertical gradient, specified as a 3-D numeric array of the same size as Gx. The vertical (*y*) axis points in the direction of increasing row subscripts. You can use the imgradientxyz function to calculate `Gy`

.

`Gz`

— Depth gradient

3-D numeric array

Depth gradient, specified as a 3-D numeric array of the same size as Gx. The depth (*z*) axis points in the direction of increasing plane subscripts. You can use the imgradientxyz function to calculate `Gz`

.

## Output Arguments

collapse all

`Gmag`

— Magnitude of the gradient vector

3-D numeric array

Magnitude of the gradient vector, returned as a 3-D numeric array of the same size as image I or the directional gradients, Gx, Gy, and Gz.

`Gmag`

is of data type `double`

, unless the input image or any of the directional gradients are of data type `single`

. In this case, `Gmag`

is of data type `single`

.

`Gazimuth`

— Azimuthal angle

3-D numeric array

Azimuthal angle, returned as a 3-D numeric array of the same size as the gradient magnitude, Gmag. `Gazimuth`

contains angles in degrees within the range [-180, 180] measured between positive *x*-axis and the projection of the point on the *x*-*y* plane.

`Gazimuth`

is of data type `double`

, unless the input image or any of the directional gradients are of data type `single`

. In this case, `Gmag`

is of data type `single`

.

**Azimuth and Elevation**

`Gelevation`

— Gradient elevation

3-D numeric array

Gradient elevation, returned as a 3-D numeric array of the same size as the gradient magnitude, Gmag. `Gelevation`

contains angles in degrees within the range [-90, 90] measured between the radial line and the *x*-*y* plane.

`Gelevation`

is of data type `double`

, unless the input image or any of the directional gradients are of data type `single`

. In this case, `Gmag`

is of data type `single`

.

## Algorithms

`imgradient3`

does not normalize the gradientoutput. If the range of the gradient output image has to match therange 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/44 and for Prewitt,the normalization factor is 1/18.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

`imgradient3`

supports the generation of C code (requires MATLAB^{®}Coder™). For more information, see Code Generation for Image Processing.When generating code, the input argument

`method`

mustbe a compile-time constant.

### GPU Code Generation

Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Usage notes and limitations:

When generating code, the input argument

`method`

must be a compile-time constant.

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

## Version History

**Introduced in R2016a**

expand all

### R2022b: Support for thread-based environments

`imgradient3`

now supports thread-based environments.

## See Also

imgradientxyz | imgradient | imgradientxy

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