Nodes/opencv-comfyui/OpenCV medianBlur_0
ComfyUI Node

OpenCV medianBlur_0

The noise killer Gaussian blur can't be

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV medianBlur_0
  • src
  • dst
  • nparray
ksize

Most blurs you've used smear. Gaussian blur, box blur - they average a neighborhood of pixels, which softens noise but also softens the edges you might have wanted to keep. medianBlur_0 does something different: for each pixel it takes the median value in the surrounding window, not the mean. Outliers get ignored, which makes this the single best node in the pack for killing salt-and-pepper noise - those stray bright and dark specks that a Gaussian just turns into a gray smear. And because the median preserves edges far better than an average, it's the blur you want when you must smooth and keep boundaries crisp.

It's part of opencv-comfyui, the auto-generated wrapper around ~635 standalone OpenCV functions. In a diffusion context it earns its keep as a preprocessor: run medianBlur_0 on a rough mask before feeding it to an inpaint, and the little speckles and holes in the mask vanish without eroding the outline you care about. Same idea if you're cleaning a noisy edge/depth map before it drives a ControlNet preprocessor. It's a milliseconds-fast, deterministic operation - exactly the kind of thing the post-processing layer exists for, versus burning a diffusion pass to do a job a neighborhood statistic handles perfectly.

Inputs and outputs

  • src - one NPARRAY (raw numpy array, BGR uint8 by default).
  • ksize - an INT, and the only real knob. It must be an odd, positive integer: 3, 5, 7... ksize is the size of the square window, so 5 means a 5×5 neighborhood. Odd requirement isn't a suggestion - even values throw an assertion. Larger = smoother but slower, and eventually detail-flattening.
  • dst - optional, ignore it (out-parameter; the README's standing rule).
  • Output nparray - the blurred array, back to Nparrays2Image for ComfyUI or on to the next OpenCV stage.

Getting it running

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

or search "opencv-comfyui" in ComfyUI Manager, install, restart. Needs opencv-contrib-python, numpy, torch. Plumbing: Image2Nparray → medianBlur_0 → Nparrays2Image, and Image2Nparray only handles batch_size==1 - slice your batch if it complains.

The two classic trips: an even ksize (assertion failure - error: (-215:Assertion failed), same family as the grayscale requirement), and OpenCV version clashes from other custom nodes (Cannot import name 'guidedFilter' from 'cv2.ximgproc').

Where people overdo it: median blur has no "strength" slider, only window size, and a 9 or 11 window on a clean image starts flattening real texture into a plastic look. For most mask-cleaning and edge-preprocessing jobs, ksize=5 is the sweet spot. Start there, and remember the _1 twin next to it is the same node - the pack just emitted one per cv2 overload.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY
ksizeINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY