---
title: "MeanShiftFilter"
language: "en"
type: "Symbol"
summary: "MeanShiftFilter[data, r, d] filters data by replacing every value by the mean of the pixels in a range-r neighborhood and whose value is within a distance d. MeanShiftFilter[data, {r1, r2, ...}, d] uses ri for filtering the i\\[Null]^thdimension in data."
keywords: 
- mean-shift
- meanshift
- mean shift
- cluster analysis
- feature space
- noise removal
- denoising
canonical_url: "https://reference.wolfram.com/language/ref/MeanShiftFilter.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Image Restoration"
    link: "https://reference.wolfram.com/language/guide/ImageRestoration.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: "Image Filtering & Neighborhood Processing"
    link: "https://reference.wolfram.com/language/guide/ImageFilteringAndNeighborhoodProcessing.en.md"
  - 
    title: "Video Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md"
related_functions: 
  - 
    title: "MeanFilter"
    link: "https://reference.wolfram.com/language/ref/MeanFilter.en.md"
  - 
    title: "MeanShift"
    link: "https://reference.wolfram.com/language/ref/MeanShift.en.md"
  - 
    title: "BilateralFilter"
    link: "https://reference.wolfram.com/language/ref/BilateralFilter.en.md"
  - 
    title: "GuidedFilter"
    link: "https://reference.wolfram.com/language/ref/GuidedFilter.en.md"
  - 
    title: "TrimmedMean"
    link: "https://reference.wolfram.com/language/ref/TrimmedMean.en.md"
---
# MeanShiftFilter

MeanShiftFilter[data, r, d] filters data by replacing every value by the mean of the pixels in a range-r neighborhood and whose value is within a distance d.

MeanShiftFilter[data, {r1, r2, …}, d] uses ri for filtering the $i$$$^{\text{th}}$$dimension in data.

## Details and Options

* ``MeanShiftFilter`` is used to locally smooth data and diminish noise while preserving significant jumps such as edges in images, where the amount of smoothing is dependent on the values of ``r`` and ``d``.

[image]

* The function applied to each range-``r`` neighborhood is ``MeanShift``.

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

* For multichannel images and audio signals, the distance is computed between channel vectors.

* ``MeanShiftFilter[data, {r1, r2, …}, d]`` computes the mean shift value in $\left(2 r_1+1\right)\times \left(2 r_2+1\right)\times \ldots$ blocks centered on each sample.

* ``MeanShiftFilter`` assumes the index coordinate system for lists and images.

[image]

* At the data boundaries, ``MeanShiftFilter`` uses smaller neighborhoods.

* The following options can be given:

|                   |                   |                                              |
| ----------------- | ----------------- | -------------------------------------------- |
| DistanceFunction  | EuclideanDistance | how to compute the distance between values   |
| MaxIterations     | 1                 | maximum number of iterations to be performed |

* For a complete list of possible settings for ``DistanceFunction``, see the reference page for ``MeanShift``.

* The possible range for the distance parameter ``d`` depends on the distance function as well as the dimension of the color space.

---

## Background & Context

``MeanShiftFilter`` is a filter for smoothing images to remove local variations typically caused by noise, rough textures, etc. ``MeanShiftFilter`` is often used as a preprocessing step before doing other image analysis operations such as segmentation.

Unlike most other noise-removing filters (e.g. ``MeanFilter``), ``MeanShiftFilter`` preserves edges in the image. Other similar functions include ``PeronaMalikFilter``, ``BilateralFilter``, and ``NonlocalMeansFilter``.

---

## Examples (22)

### Basic Examples (3)

Mean-shift filtering of a vector:

```wl
In[1]:= MeanShiftFilter[ {1, 0, 1, 4, 4, 5, 2, 1}, 2, 1]

Out[1]= {0.666667, 0.666667, 0.666667, 4.33333, 4.33333, 4.33333, 1.5, 1.5}
```

---

Filter a ``TimeSeries`` :

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

In[2]:= filtered = MeanShiftFilter[ts, 5, 2]

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

In[3]:= ListLinePlot[{ts, filtered}, PlotLegends -> {"original data", "filtered"}]

Out[3]= [image]
```

---

Mean-shift filtering of a color image:

```wl
In[1]:= MeanShiftFilter[[image], 10, .5]

Out[1]= [image]
```

### Scope (12)

#### Data (7)

Mean-shift filtering of data:

```wl
In[1]:= MeanShiftFilter[ {0, 1, 10, 11}, 1, 1]

Out[1]= {0.5, 0.5, 10.5, 10.5}
```

---

Mean-shift filtering of a 2D array:

```wl
In[1]:=
MeanShiftFilter[(⁠|   |   |   |   |
| - | - | - | - |
| 0 | 3 | 2 | 2 |
| 3 | 9 | 9 | 6 |
| 5 | 8 | 7 | 0 |
| 3 | 1 | 1 | 2 |⁠), 1, 2]//MatrixForm

Out[1]//MatrixForm=
(⁠|         |         |         |     |
| ------- | ------- | ------- | --- |
| 0.      | 2.66667 | 2.33333 | 2.  |
| 3.66667 | 8.25    | 8.25    | 6.5 |
| 3.66667 | 8.25    | 7.8     | 1.  |
| 3.      | 1.66667 | 1.      | 1.  |⁠)
```

---

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 = MeanShiftFilter[ts, 5, 1, MaxIterations -> 100]

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

In[3]:= ListLinePlot[{ts, filtered}, PlotLegends -> {"original data", "filtered"}]

Out[3]= [image]
```

---

Filter an ``Audio`` signal:

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

In[2]:= b = MeanShiftFilter[a, 25, 0.5]

Out[2]= [image]

In[3]:= AudioPlot[{a, b}]

Out[3]= [image]
```

---

Mean-shift filtering of a grayscale image:

```wl
In[1]:= MeanShiftFilter[[image], 5, 0.1]

Out[1]= [image]
```

---

Filter video frames:

```wl
In[1]:= MeanShiftFilter[Video["ExampleData/fish.mp4"], 3, .02]

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

---

Mean-shift filtering of a 3D image:

```wl
In[1]:= MeanShiftFilter[[image], 6, 0.1]

Out[1]= [image]
```

#### Parameters (5)

Specify one radius to be used in all directions:

```wl
In[1]:= MeanShiftFilter[[image], 5, 1]

Out[1]= [image]
```

---

Mean-shift filtering in just the first direction:

```wl
In[1]:= MeanShiftFilter[[image], {5, 0}, 1]

Out[1]= [image]
```

---

Filtering in just the second direction:

```wl
In[1]:= MeanShiftFilter[[image], {0, 5}, 1]

Out[1]= [image]
```

---

Mean-shift filtering of a 3D image in the vertical direction only:

```wl
In[1]:= MeanShiftFilter[[image], {5, 0, 0}, 1]

Out[1]= [image]
```

Mean-shift filtering of a 3D image in the horizontal planes only:

```wl
In[2]:= MeanShiftFilter[[image], {0, 5, 5}, 1]

Out[2]= [image]
```

---

Mean-shift filter averages only over pixels that differ in value by less than ``d`` :

```wl
In[1]:= Labeled[MeanShiftFilter[[image], 5, #], #]& /@ {0.1, 0.3, 0.5}

Out[1]= [image]
```

### Options (3)

#### DistanceFunction (2)

By default, ``EuclideanDistance`` is used:

```wl
In[1]:= MeanShiftFilter[[image], 10, .5]

Out[1]= [image]
```

---

Specify the distance function:

```wl
In[1]:= MeanShiftFilter[[image], 10, .5, DistanceFunction -> ManhattanDistance]

Out[1]= [image]
```

#### MaxIterations (1)

By default, only one iteration of mean shift is applied to input:

```wl
In[1]:= MeanShiftFilter[[image], 3, .1]

Out[1]= [image]
```

Use ``MaxIterations`` to specify the number of iterations:

```wl
In[2]:= MeanShiftFilter[[image], 3, .1, MaxIterations -> 100]

Out[2]= [image]
```

### Applications (2)

Use mean-shift filtering to smooth an image while preserving the edges:

```wl
In[1]:= MeanShiftFilter[[image], 4, 0.05, MaxIterations -> 30]

Out[1]= [image]
```

---

Use mean-shift filtering as a preprocessing step for image segmentation:

```wl
In[1]:= ClusteringComponents[MeanShiftFilter[[image], 3, 0.2], 5]// Colorize

Out[1]= [image]
```

### Properties & Relations (1)

``MeanShiftFilter`` is equivalent to ``MeanFilter`` for distance ``d`` greater than the data dynamic range:

```wl
In[1]:=
r = 1;
data = RandomReal[{0, r}, 100];

In[2]:= MeanShiftFilter[data, 1, r + $MachineEpsilon] == MeanFilter[data, 1]

Out[2]= True
```

### Neat Examples (1)

Show how ``MeanShiftFilter`` iteratively shifts values until they converge:

```wl
In[1]:= data = Join[RandomReal[NormalDistribution[0, .4], {500, 2}], RandomReal[NormalDistribution[1, .4], {500, 2}]];

In[2]:= ListPlot[data]

Out[2]= [image]

In[3]:= ms = NestList[MeanShiftFilter[#, 1000, {.6, .6}]&, data, 5];

In[4]:= ListLinePlot[Transpose[ms]]

Out[4]= [image]
```

## See Also

* [`MeanFilter`](https://reference.wolfram.com/language/ref/MeanFilter.en.md)
* [`MeanShift`](https://reference.wolfram.com/language/ref/MeanShift.en.md)
* [`BilateralFilter`](https://reference.wolfram.com/language/ref/BilateralFilter.en.md)
* [`GuidedFilter`](https://reference.wolfram.com/language/ref/GuidedFilter.en.md)
* [`TrimmedMean`](https://reference.wolfram.com/language/ref/TrimmedMean.en.md)

## Related Guides

* [Image Restoration](https://reference.wolfram.com/language/guide/ImageRestoration.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)
* [Image Filtering & Neighborhood Processing](https://reference.wolfram.com/language/guide/ImageFilteringAndNeighborhoodProcessing.en.md)
* [Video Computation: Update History](https://reference.wolfram.com/language/guide/VideoComputation-UpdateHistory.en.md)

## History

* [Introduced in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) \| [Updated in 2015 (10.2)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn102.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)