Median filtering
Choose the middle value of a pixel neighbourhood instead of averaging its intensities.
The same scene stays here while the working view changes.
The orange 3 × 3 window surrounds one output location. Salt-and-pepper outliers move to the ends of the sorted window. The middle sample survives.
Salt-and-pepper outliers move to the ends of the sorted window. The middle sample survives. One output value is 172.0; the window then moves across the image.
The whole image has now been processed. The highlighted input value 174 becomes 172.0.
Write the middle value at the centre.
Computed teaching example on a 24 × 16 image. Small kernels and simplified settings keep each change visible; use the image laboratory for native OpenCV.
Try it on an image
Experiment at pixel level
Replace each colour channel with its neighbourhood median.
The engine loads on your first run. Your images stay in this browser.
Scroll over either image to zoom at the pointer. Use the scrollbars to pan both views over the same relative area. Zoom is relative to the input; pixel coordinates belong to each image. Warps can change scene correspondence.
Pixel inspector RGBA · native values · matched scale · 9 × 9 output pixels
Select a pixel
Select a pixel
When to use it
Remove isolated bright or dark specks while keeping many sharp boundaries.
How it works
- 01Gather the neighbourhood values for one channel.
- 02Order the values from dark to light.
- 03Write the middle value at the centre.
output(x,y) = median(neighbourhood(x,y))
What to tune
ksize must be odd and greater than one. Increase it only when the unwanted specks are larger.
Where it breaks down
The method removes thin lines along with noise when they occupy less than half the window. Supported depths depend on kernel size.
TypeScript API
Open an entry for its exact overloads, parameter descriptions, result ownership and pinned upstream source.
All of these calls execute on the CPU. Native objects need explicit disposal. See matrices and ownership and build compatibility.
Related methods
Gaussian blur
Replace each pixel with a weighted average that gives nearby pixels more influence.
Box filtering
Average a rectangular neighbourhood with equal weight at every position.
Bilateral filtering
Smooth nearby pixels only when their colours are also similar.
Custom correlation kernels
Apply your own linear filter to every pixel neighbourhood.