---
title: "GradientFilter"
language: "en"
type: "Symbol"
summary: "GradientFilter[data, r] gives the magnitude of the gradient of data, computed using discrete derivatives of a Gaussian of sample radius r. GradientFilter[data, {r, \\[Sigma]}] uses a Gaussian with standard deviation \\[Sigma]. GradientFilter[data, {{r1, r2, ...}, ...}] uses a Gaussian with radius ri at level i in data."
keywords: 
- local filtering
- gradient
- edge detection
- canny
- shen
- castan
- sobel
- nonmax
- non maxiumum
- non-max
- non-maximum
- suppression
- DiZenzo
- Zenzo
canonical_url: "https://reference.wolfram.com/language/ref/GradientFilter.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Image Filtering & Neighborhood Processing"
    link: "https://reference.wolfram.com/language/guide/ImageFilteringAndNeighborhoodProcessing.en.md"
  - 
    title: "Segmentation Analysis"
    link: "https://reference.wolfram.com/language/guide/SegmentationAnalysis.en.md"
  - 
    title: "Linear and Nonlinear Filters"
    link: "https://reference.wolfram.com/language/guide/LinearAndNonlinearFilters.en.md"
  - 
    title: "Image Processing & Analysis"
    link: "https://reference.wolfram.com/language/guide/ImageProcessing.en.md"
  - 
    title: "Feature Detection"
    link: "https://reference.wolfram.com/language/guide/FeatureDetection.en.md"
  - 
    title: "Signal Filtering & Filter Design"
    link: "https://reference.wolfram.com/language/guide/SignalFilteringAndFilterDesign.en.md"
  - 
    title: "Image Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md"
  - 
    title: "Video Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md"
related_functions: 
  - 
    title: "GradientOrientationFilter"
    link: "https://reference.wolfram.com/language/ref/GradientOrientationFilter.en.md"
  - 
    title: "GaussianFilter"
    link: "https://reference.wolfram.com/language/ref/GaussianFilter.en.md"
  - 
    title: "LaplacianGaussianFilter"
    link: "https://reference.wolfram.com/language/ref/LaplacianGaussianFilter.en.md"
  - 
    title: "ImageConvolve"
    link: "https://reference.wolfram.com/language/ref/ImageConvolve.en.md"
  - 
    title: "DiscreteDelta"
    link: "https://reference.wolfram.com/language/ref/DiscreteDelta.en.md"
  - 
    title: "RangeFilter"
    link: "https://reference.wolfram.com/language/ref/RangeFilter.en.md"
  - 
    title: "EdgeDetect"
    link: "https://reference.wolfram.com/language/ref/EdgeDetect.en.md"
  - 
    title: "MorphologicalPerimeter"
    link: "https://reference.wolfram.com/language/ref/MorphologicalPerimeter.en.md"
  - 
    title: "GaussianMatrix"
    link: "https://reference.wolfram.com/language/ref/GaussianMatrix.en.md"
  - 
    title: "ShenCastanMatrix"
    link: "https://reference.wolfram.com/language/ref/ShenCastanMatrix.en.md"
---
# GradientFilter

GradientFilter[data, r] gives the magnitude of the gradient of data, computed using discrete derivatives of a Gaussian of sample radius r.

GradientFilter[data, {r, σ}] uses a Gaussian with standard deviation σ.

GradientFilter[data, {{r1, r2, …}, …}] uses a Gaussian with radius ri at level i in data.

## Details and Options

* ``GradientFilter`` is commonly used to detect regions of rapid change in signals and images.

[image]

* For a single-channel image and for data, the gradient magnitude is the Euclidean norm of the gradient $g$ at a pixel position, approximated using discrete derivatives of Gaussians in each dimension.

* For multichannel images, the Jacobian matrix $J$ is $\left(
\begin{array}{c}
 g_1 \\
 g_2 \\
 \vdots  \\
\end{array}
\right)$, where $g_i$ is the gradient for channel $i$. Gradient magnitude is the square root of the largest eigenvalue of $J ^TJ$, returned as a single-channel image.

* The ``data`` can be any of the following:

|         |                                                   |
| ------- | ------------------------------------------------- |
| list    | arbitrary-rank numerical array                    |
| tseries | temporal data such as TimeSeries, TemporalData, … |
| image   | arbitrary Image or Image3D object                 |
| audio   | an Audio object                                   |
| video   | a Video object                                    |

* ``GradientFilter[data, r]`` uses standard deviation $\sigma =r/2$.

* The following options can be specified:

|                   |           |                      |
| ----------------- | --------- | -------------------- |
| Method            | Automatic | convolution kernel   |
| Padding           | "Fixed"   | padding method       |
| WorkingPrecision  | Automatic | the precision to use |

* The following suboptions can be given to ``Method`` :

|                     |          |                                        |
| ------------------- | -------- | -------------------------------------- |
| "DerivativeKernel"  | "Bessel" | convolution kernel                     |
| "NonMaxSuppression" | False    | whether to use non-maximum suppression |

* Possible settings for ``"DerivativeKernel"`` include:

|                       |                                                                        |
| --------------------- | ---------------------------------------------------------------------- |
| "Bessel"              | standardized Bessel derivative kernel, used for Canny edge detection   |
| "Gaussian"            | standardized Gaussian derivative kernel, used for Canny edge detection |
| "ShenCastan"          | first-order derivatives of exponentials                                |
| "Sobel"               | binomial generalizations of the Sobel edge-detection kernels           |
| {kernel1, kernel2, …} | explicit kernels specified for each dimension                          |

* ``GradientFilter[data, …]`` by default gives an array, audio object or image of the same dimensions as ``data``.

* With setting ``Padding -> None``, ``GradientFilter[data, …]`` normally gives an array, audio object or image smaller than ``data``.

* ``GradientFilter[image, …]`` returns an image of a real type.

---

## Examples (34)

### Basic Examples (3)

Gradient filter of a grayscale image:

```wl
In[1]:= GradientFilter[[image], 2]//ImageAdjust

Out[1]= [image]
```

---

Gradient filtering of a 3D image:

```wl
In[1]:= GradientFilter[[image], 2]//ImageAdjust//Image3DSlices

Out[1]= [image]
```

---

Apply gradient filtering to a vector of numbers:

```wl
In[1]:= GradientFilter[{1, 2, 3, 4, 5, 1, 5, 4, 3, 2, 1}, 1]

Out[1]= {0.5, 1., 1., 1., 1.5, 0., 1.5, 1., 1., 1., 0.5}
```

### Scope (11)

#### Data (7)

Gradient filter of a numeric vector:

```wl
In[1]:=
list = {0, 0, 0, 1, 4, 6, 4, 1, 0, 0, 0};
res = GradientFilter[list, 1]

Out[1]= {0., 0., 0.5, 2., 2.5, 0., 2.5, 2., 0.5, 0., 0.}

In[2]:= ListLinePlot[{list, res}]

Out[2]= [image]
```

---

Gradient filter of a numeric matrix:

```wl
In[1]:= (mat = N@BoxMatrix[1, 7])//MatrixForm

Out[1]//MatrixForm=
(⁠|    |    |    |    |    |    |    |
| -- | -- | -- | -- | -- | -- | -- |
| 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| 0. | 0. | 1. | 1. | 1. | 0. | 0. |
| 0. | 0. | 1. | 1. | 1. | 0. | 0. |
| 0. | 0. | 1. | 1. | 1. | 0. | 0. |
| 0. | 0. | 0. | 0. | 0. | 0. | 0. |
| 0. | 0. | 0. | 0. | 0. | 0. | 0. |⁠)

In[2]:= GradientFilter[mat, 1]//MatrixForm

Out[2]//MatrixForm=
(⁠|    |           |          |                         |          |           |    |
| -- | --------- | -------- | ----------------------- | -------- | --------- | -- |
| 0. | 0.        | 0.       | 0.                      | 0.       | 0.        | ...  0.453043  | 0.636834 | 0.5                     | 0.636834 | 0.453043  | 0. |
| 0. | 0.0702726 | 0.453043 | 0.5                     | 0.453043 | 0.0702726 | 0. |
| 0. | 0.        | 0.       | 0.                      | 0.       | 0.        | 0. |⁠)
```

---

Filter a ``TimeSeries`` :

```wl
In[1]:=
ts = TemporalData[TimeSeries, {CompressedData["«1188»"], {{0, 1., 0.01}}, 1, {"Continuous", 1}, {"Continuous", 1}, 1, 
  {ValueDimensions -> 1, ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}}, False, 
 10.1];

In[2]:= filtered = GradientFilter[ts, 3]

Out[2]=
TemporalData[TimeSeries, {CompressedData["«1186»"], {{0, 1., 0.01}}, 1, {"Continuous", 1}, {"Continuous", 1}, 1, 
  {ValueDimensions -> 1, ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}}}, False, 
 14.3]

In[3]:= ListLinePlot[{ts, filtered}, ...]

Out[3]= [image]
```

---

Filter an ``Audio`` signal:

```wl
In[1]:=
a = Import["ExampleData/rule30.wav"];
b = GradientFilter[a, 1]

Out[1]= [image]

In[2]:= AudioPlot[{a, AudioNormalize[b]}]

Out[2]= [image]
```

---

Filter a color image:

```wl
In[1]:= GradientFilter[[image], 2]// ImageAdjust

Out[1]= [image]
```

---

Filter video frames:

```wl
In[1]:= GradientFilter[Video["ExampleData/fish.mp4"], 2]

Out[1]= \!\(\*VideoBox["![Embedded Video Player](video://content-1553g)"]\)
```

---

Filter a 3D image:

```wl
In[1]:= GradientFilter[[image], 1]

Out[1]= [image]
```

#### Parameters (4)

Gradient filtering using increasing radii:

```wl
In[1]:= ImageAdjust[GradientFilter[[image], #]]& /@ {1, 3, 6}

Out[1]= {[image], [image], [image]}
```

---

Use different vertical and horizontal radii:

```wl
In[1]:=
i = [image];
GradientFilter[i, {{5, 1}}]//ImageAdjust

Out[1]= [image]
```

---

Gradient derivative of a 3D image in the vertical direction only:

```wl
In[1]:=
i = [image];
GradientFilter[i, {{1, 0}}]

Out[1]= [image]
```

Filtering of the horizontal planes only:

```wl
In[2]:= GradientFilter[i, {{0, 1}}]

Out[2]= [image]
```

---

The default standard deviation is $\sigma =r/2$ :

```wl
In[1]:=
i = [image];
GradientFilter[i, 5]// ImageAdjust

Out[1]= [image]
```

Specify a different $\sigma$ :

```wl
In[2]:= GradientFilter[i, {5, .5}]// ImageAdjust

Out[2]= [image]
```

### Options (9)

#### Method (3)

Compute the gradient magnitude using the default Bessel method:

```wl
In[1]:= GradientFilter[[image], 1]

Out[1]= [image]
```

Use the Shen–Castan method:

```wl
In[2]:= GradientFilter[[image], 1, Method -> "ShenCastan"]

Out[2]= [image]
```

Compute the gradient magnitude using Prewitt kernels:

```wl
In[3]:=
GradientFilter[[image], Method -> {(⁠|    |    |    |
| -- | -- | -- |
| -1 | -1 | -1 |
| 0  | 0  | 0  |
| 1  | 1  | 1  |⁠), (⁠|    |   |   |
| -- | - | - |
| -1 | 0 | 1 |
| -1 | 0 | 1 |
| -1 | 0 | 1 |⁠)}]

Out[3]= [image]
```

---

Typically, corners are rounded during gradient filtering:

```wl
In[1]:=
i = [image];
GradientFilter[i, 11]//ImageAdjust

Out[1]= [image]
```

The Shen–Castan method gives a better corner localization at large scales:

```wl
In[2]:= GradientFilter[i, 11, Method -> "ShenCastan"]//ImageAdjust

Out[2]= [image]
```

---

By default, non-max suppression is not applied:

```wl
In[1]:=
i = [image];
GradientFilter[i, 3]//ImageAdjust

Out[1]= [image]
```

Non-max suppression gives only the ridges of gradient lines:

```wl
In[2]:= GradientFilter[i, 3, Method -> "NonMaxSuppression" -> True]//ImageAdjust

Out[2]= [image]
```

Use Shen-Castan method with no-max suppression:

```wl
In[3]:= GradientFilter[[image], 21, Method -> {"ShenCastan", "NonMaxSuppression" -> True}]//ImageAdjust

Out[3]= [image]
```

#### Padding (2)

``GradientFilter`` using different padding methods:

```wl
In[1]:=
v = {0, 1, 2, 3, 4, 4, 3, 2, 1, 0};
pad = {"Fixed", "Periodic", "Reflected"};
ListLinePlot[GradientFilter[v, 2, Padding -> #]& /@ pad, PlotLegends -> pad]

Out[1]= [image]
```

---

``Padding -> None`` normally returns an image smaller than the input image:

```wl
In[1]:= GradientFilter[[image], 30, Padding -> None]//ImageAdjust

Out[1]= [image]
```

#### WorkingPrecision (4)

``MachinePrecision`` is by default used with integer arrays:

```wl
In[1]:= GradientFilter[{3, 3, 4, 4, 5, 5}, 100]

Out[1]= {0.0186022, 0.0186194, 0.0186279, 0.0186279, 0.0186194, 0.0186022}
```

Perform an exact computation instead:

```wl
In[2]:= GradientFilter[{3, 3, 4, 4, 5, 5}, 1, WorkingPrecision -> ∞]

Out[2]= {0, (1/2), (1/2), (1/2), (1/2), 0}
```

---

With real arrays, by default the precision of the input is used:

```wl
In[1]:= GradientFilter[{1.0000000000000000000, 2.0000000000000000000, 3.0000000000000000000, 4.0000000000000000000}, 1]

Out[1]= {0.5000000000000000000, 1.000000000000000000, 1.000000000000000000, 0.5000000000000000000}
```

Specify the precision to use:

```wl
In[2]:= GradientFilter[{1.0000000000000000000, 2.0000000000000000000, 3.0000000000000000000, 4.0000000000000000000}, 1, WorkingPrecision -> MachinePrecision]

Out[2]= {0.5, 1., 1., 0.5}
```

---

With symbolic arrays, exact computation is used:

```wl
In[1]:= GradientFilter[{a, b, c}, 1]

Out[1]= {Abs[-(a/2) + (b/2)], Abs[-(a/2) + (c/2)], Abs[-(b/2) + (c/2)]}
```

---

``WorkingPrecision`` is ignored when filtering images:

```wl
In[1]:= GradientFilter[[image], 1, WorkingPrecision -> Infinity]

Out[1]= [image]
```

An image of a real type is always returned:

```wl
In[2]:= ImageType[%]

Out[2]= "Real32"
```

### Applications (4)

Use gradient filtering to find edges:

```wl
In[1]:= GradientFilter[[image], 1]// Binarize

Out[1]= [image]
```

---

Compute an unsharp mask:

```wl
In[1]:=
i = [image];
u = GradientFilter[i, 1]

Out[1]= [image]
```

Add the unsharp mask to the original image:

```wl
In[2]:= i + 2u

Out[2]= [image]
```

---

Use gradient filtering as a preprocessing step for watershed segmentation:

```wl
In[1]:= (g = GradientFilter[[image], 2])//ImageAdjust

Out[1]= [image]

In[2]:= WatershedComponents[g, Method -> {"MinimumSaliency", 0.8}]//Colorize

Out[2]= [image]
```

---

 Get borders from a colored map:

```wl
In[1]:= GradientFilter[[image], 1]

Out[1]= [image]
```

### Properties & Relations (4)

``GradientFilter`` of a vector is the absolute value of the Gaussian first derivative of the vector:

```wl
In[1]:=
v = {1, 0, -1, 2, 3, -1, 0, 1};
GradientFilter[v, 1] == Abs[GaussianFilter[v, 1, 1]]

Out[1]= True
```

``GradientFilter`` of a grayscale image is the square root of the sum of squares of Gaussian first derivatives in each dimension of an image:

```wl
In[2]:=
i = [image];
GradientFilter[i, 1] == Sqrt[GaussianFilter[i, 1, {1, 0}]^2 + GaussianFilter[i, 1, {0, 1}]^2]

Out[2]= True
```

---

Impulse responses of gradient filter for selected radii:

```wl
In[1]:= ListLinePlot[GradientFilter[ArrayPad[{1}, 15], #]& /@ {1, 3, 6}, PlotRange -> All, PlotLegends -> {"r=1", "r=3", "r=6"}]

Out[1]= [image]
```

Gradient filter impulse responses in 2D:

```wl
In[2]:= ImageAdjust[GradientFilter[Image[ArrayPad[{{1}}, 50]], {50, #}]]& /@ {5, 10, 20}

Out[2]= [image]
```

---

Impulse responses of gradient filter using different "DerivativeKernel" settings:

```wl
In[1]:= Labeled[ImageAdjust[GradientFilter[Image[ArrayPad[{{1}}, 50]], 50, Method -> #]], #]& /@ {"Bessel", "Gaussian", "ShenCastan", "Sobel"}

Out[1]= [image]
```

---

Gradient filtering of a binary image gives a grayscale image of a real type:

```wl
In[1]:= d = Image[DiskMatrix[23, 81], "Bit"]

Out[1]= [image]

In[2]:= GradientFilter[d, {2, 1}]

Out[2]= [image]

In[3]:= ImageType[%]

Out[3]= "Real32"
```

### Possible Issues (1)

Gradient filtering usually results in a dark image with small pixel values:

```wl
In[1]:= GradientFilter[[image], 2]

Out[1]= [image]
```

Adjusting for brightness creates a more visible image gradient:

```wl
In[2]:= ImageAdjust[%]

Out[2]= [image]
```

### Neat Examples (2)

Compute and visualize multiscale gradient filtering:

```wl
In[1]:=
t = Table[GradientFilter[[image], 2 ^ r], {r, 0, 6}];
Image3D[t, BoxRatios -> {1, 1, .5}, ColorFunction -> "GrayLevelOpacity"]

Out[1]= [image]
```

---

An artistic effect based on image gradients:

```wl
In[1]:=
i = [image];
Opening[i, DiskMatrix[5]] + ImageAdjust[ GradientFilter[i, 2]]

Out[1]= [image]
```

## See Also

* [`GradientOrientationFilter`](https://reference.wolfram.com/language/ref/GradientOrientationFilter.en.md)
* [`GaussianFilter`](https://reference.wolfram.com/language/ref/GaussianFilter.en.md)
* [`LaplacianGaussianFilter`](https://reference.wolfram.com/language/ref/LaplacianGaussianFilter.en.md)
* [`ImageConvolve`](https://reference.wolfram.com/language/ref/ImageConvolve.en.md)
* [`DiscreteDelta`](https://reference.wolfram.com/language/ref/DiscreteDelta.en.md)
* [`RangeFilter`](https://reference.wolfram.com/language/ref/RangeFilter.en.md)
* [`EdgeDetect`](https://reference.wolfram.com/language/ref/EdgeDetect.en.md)
* [`MorphologicalPerimeter`](https://reference.wolfram.com/language/ref/MorphologicalPerimeter.en.md)
* [`GaussianMatrix`](https://reference.wolfram.com/language/ref/GaussianMatrix.en.md)
* [`ShenCastanMatrix`](https://reference.wolfram.com/language/ref/ShenCastanMatrix.en.md)

## Related Guides

* [Image Filtering & Neighborhood Processing](https://reference.wolfram.com/language/guide/ImageFilteringAndNeighborhoodProcessing.en.md)
* [Segmentation Analysis](https://reference.wolfram.com/language/guide/SegmentationAnalysis.en.md)
* [Linear and Nonlinear Filters](https://reference.wolfram.com/language/guide/LinearAndNonlinearFilters.en.md)
* [Image Processing & Analysis](https://reference.wolfram.com/language/guide/ImageProcessing.en.md)
* [Feature Detection](https://reference.wolfram.com/language/guide/FeatureDetection.en.md)
* [Signal Filtering & Filter Design](https://reference.wolfram.com/language/guide/SignalFilteringAndFilterDesign.en.md)
* [Image Computation: Update History](https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md)
* [Video Computation: Update History](https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md)

## History

* [Introduced in 2008 (7.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn70.en.md) \| [Updated in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) ▪ [2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md) ▪ [2015 (10.1)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn101.en.md) ▪ [2016 (11.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn110.en.md) ▪ [2025 (14.3)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn143.en.md)