---
title: "Median filter in C#"
url: https://recursiveai.org/oneimage/cookbook/remove-speckle-noise-with-median-filter/
description: "Apply a 3×3 median filter to an image in C# with OneImage. Remove isolated black and white speckles and compare pixel values on both sides of an edge."
version: "1.11.2"
---

# Median filter in C#

**OneImage** is a free, open-source image library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneImage) · [GitHub](https://github.com/recursive-ai-oss/oneimage-csharp) · [Docs](https://recursiveai.org/oneimage/docs/) · [Download example (.zip)](https://recursiveai.org/oneimage/cookbook/remove-speckle-noise-with-median-filter/remove-speckle-noise-with-median-filter.zip)

Remove isolated dots from `speckles.png` in the download.

```shell
dotnet add package RecursiveAI.OneImage
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneImage;
Image.Load("speckles.png")
    .Median(radius: 1)
    .Save("clean.png");
Console.WriteLine("Wrote clean.png");
```

Unzip [the example](https://recursiveai.org/oneimage/cookbook/remove-speckle-noise-with-median-filter/remove-speckle-noise-with-median-filter.zip), then `dotnet run`.

```text
Wrote clean.png
```

The 11×9 grayscale input has a vertical edge: columns 0–5 are gray level 64, and columns 6–10 are level 192, except for one white dot at `(2, 3)` and one black dot at `(8, 5)`. The coordinates start at zero.

## The full program

```csharp
using OneImage;

Image image = Image.Load("speckles.png");
Image clean = image.Median(radius: 1);
clean.Save("clean.png");
Console.WriteLine("Wrote clean.png");

Gray8[] before = image.ToArray<Gray8>();
Gray8[] after = Image.Load("clean.png").ToArray<Gray8>();
foreach (var (x, y) in new[] { (2, 3), (8, 5), (5, 4), (6, 4) })
{
    int i = y * image.Width + x;
    Console.WriteLine($"({x}, {y}): {before[i].V} -> {after[i].V}");
}
```

`dotnet run` prints:

```text
Wrote clean.png
(2, 3): 255 -> 64
(8, 5): 0 -> 192
(5, 4): 64 -> 64
(6, 4): 192 -> 192
```

`Gray8` is OneImage’s 8-bit grayscale pixel type; `.V` is its value from 0 to 255. Both speckles take the gray level of their surroundings, while the sampled pixels on either side of the edge stay the same. The radius must be 1–127; a value outside that range throws `ArgumentOutOfRangeException`.

## Variations

**Try a 5×5 window.** Set the radius to 2 if a 3×3 window leaves clusters of speckles behind. Compare fine details before keeping the larger window.

```csharp
using OneImage;
Image.Load("speckles.png")
    .Median(radius: 2)
    .Save("clean-5x5.png");
```

## Related recipes

- [Blur an image](https://recursiveai.org/oneimage/cookbook/blur-image/)
- [Detect image edges with Sobel](https://recursiveai.org/oneimage/cookbook/detect-image-edges/)

---

## The short answer

Call `Image.Load(path).Median(radius: 1)` and save the result. The radius is how far the filter looks in each direction, so 1 makes a 3×3 window. The operation returns a new image; saving it under a different name leaves the input file alone.

## What is a median filter in image processing?

The median is the middle value when a list is put in order. For a radius of 1, take the nine samples centered on a pixel, sort their values and use the fifth one. Around the white dot in the example, eight samples are 64 and only one is 255, so the dot becomes 64. You can apply the same reasoning to the black dot on the lighter side. OneImage computes the median separately for each channel, using the stored values. It filters alpha as well as color, so check transparency edges if your input has them.

## How does a median filter remove salt-and-pepper noise?

Salt-and-pepper noise is made of outliers: very bright or very dark pixels among otherwise similar ones. If enough neighbors are unaffected, the outlier doesn’t become the middle value. A Gaussian blur, by contrast, uses a weighted average, so the extreme sample still influences the result. In the example’s straight edge, the window centered just left of the edge has six dark-side samples and three light-side samples. The median keeps the dark value rather than mixing the two. That explains the two unchanged pixels at the edge, not a guarantee for every edge: thin lines, small corners, and texture can change when they occupy a minority of the window.

## How do you choose a median filter window size?

Start with radius 1 for isolated one-pixel dots. Move to radius 2 only if you still see clusters, and check that the output hasn’t erased detail you wanted to keep. At the image border the default, `EdgeMode.Clamp`, repeats the nearest edge pixel for any sample outside the image. That choice affects the median near the border, not pixels whose window fits entirely inside the image.
