Nodes/ComfyUI_OpenCV/Median Blur
ComfyUI Node

Median Blur

The denoiser that removes speckle without melting your edges

By PiggyDance·Created about a year ago·Updated about a year ago· 1
Median Blur
  • image
  • image
ksize5

If you've got an image with scattered speckle - salt-and-pepper noise, isolated hot pixels, JPEG crud, that dithered grain some renders leave behind - Gaussian blur will smear it and your edges in the same pass. Median blur won't. It kills the speckle and leaves the hard lines standing, which is why it's the classic cleanup tool for scans, old photos, and anything with blocky artifacts. This node is OpenCV's medianBlur wrapped up for ComfyUI, from the same three-node ComfyUI_OpenCV pack that gives you the Gaussian and color-conversion nodes.

How it works

Instead of averaging (which is what makes Gaussian smudge edges), a median filter looks at every pixel's neighborhood, sorts all the values, and takes the middle one. A single out-of-place bright or dark pixel never wins the median - it gets replaced by whatever most of its neighbors actually are - while a real edge still has a clean majority on one side, so it survives. It's nonlinear, it introduces no new colors that weren't already there (a boon for flat-color or anime-style output), and it's the textbook answer to impulse noise.

The inputs

Two, and one of them is the image wire:

  • image - your IMAGE tensor in, same IMAGE out.
  • ksize - the side length of the square kernel, default 5, slider stepping by 2. It must be odd and greater than 1 (per the tooltip), and if you type an even value the node quietly adds 1. A ksize of 1 is a no-op.

That's the whole interface. There's no sigma, no x/y split - median blur doesn't have those concepts. If you want directional or width-controlled smoothing, that's the Gaussian node's job; use them as a pair and you can cover most denoising.

Install

Shared with the other two nodes in the pack. ComfyUI Manager → search ComfyUI_OpenCV → install → restart, or:

cd ComfyUI/custom_nodes
git clone https://github.com/PiggyDance/ComfyUI_OpenCV
# then restart ComfyUI

No models, no big downloads. One caveat worth repeating from the rest of the pack: it declares no pip dependencies but imports cv2 at load, and current ComfyUI doesn't guarantee OpenCV - if the node's missing from the menu, pip install opencv-python-headless in ComfyUI's environment and restart.

Where to use it

  • Cleaning up masks - remove stray specks in a mask before it drives compositing or inpainting, without chewing the mask's boundary.
  • Pre-cleaning for ControlNet preprocessors - a small ksize (3) before lineart or depth extraction means the preprocessor sees clean edges, not noise artifacts it might hallucinate into the map.
  • Grain and artifact removal - spotty grain or JPEG blocks vanish at ksize 3–5; beyond about 9 you start to see a watercolor flattening on fine detail.

Where people get burned

  • Big kernels are slow, and this is CPU-only. Median isn't separable, so cost scales with kernel area - ksize 21 means 441 samples per pixel, per frame, in a Python loop over the batch. Great on a still at ksize 5, painful on a long video at ksize 15.
  • Don't overdo it. Because it's edge-preserving, it's tempting to crank ksize and "clean everything". Past a point you're erasing texture, and the result flattens into poster-like smoothness that's worse than the noise you started with.

It's a small, honest utility node, and for what it does - one specific kind of noise, one knob - it's genuinely the right tool, and the least-fiddly node in this pack.

CategoryOpenCV/Filters

Inputs (2)

NameTypeDefaultDescription
imageIMAGEThe input image to apply median blur
ksizeINT51–99Kernel size. Must be odd and greater than 1

Outputs (1)

NameTypeDescription
imageIMAGE