Nodes/opencv-comfyui/OpenCV filterSpeckles_0
ComfyUI Node

OpenCV filterSpeckles_0

Cleaning the snow out of a disparity map

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV filterSpeckles_0
  • img
  • buf
  • nparray_0
  • nparray_1
newVal
maxSpeckleSize
maxDiff

This is one of those nodes that makes you remember the pack's README warning: some OpenCV functions simply don't map onto "process my pretty image." filterSpeckles_0 wraps cv2.filterSpeckles, and it is not a general-purpose photo filter. It's a cleanup pass for disparity maps - the output of stereo matching (SGBM), where the "image" is actually per-pixel depth differences between two camera views.

Here's the problem it solves. Stereo matching is noisy in a particular way: it produces little isolated blobs - speckles - of wrong disparity scattered over otherwise-smooth regions. Those speckles are exactly what makes depth maps look like they were sprinkled with static. filterSpeckles finds connected regions of a disparity image smaller than a size threshold and repaints them with a single value, smoothing away the snow while leaving the real disparity gradients alone. It's the polish step between "stereo match" and "usable depth," and if you're not doing stereo depth, this node has nothing for you.

The inputs that matter

  • img (NPARRAY) - the disparity map, not a normal photo. It should be the single-channel disparity output of a stereo matcher, in a float or short format.
  • newVal (FLOAT) - the value that speckle regions get repainted with. -1 is the conventional "invalid disparity" marker, so OpenCV's default behavior of filling with a large negative is what most pipelines want.
  • maxSpeckleSize (INT) - the size threshold. Any connected component smaller than this (in pixels) counts as a speckle and gets removed. Too low and you keep noise; too high and you eat genuine small details.
  • maxDiff (FLOAT) - how close neighboring pixels must be in value to count as the same region. It's the "is this really a connected blob" tolerance.
  • buf (NPARRAY, optional) - OpenCV's buffer out-parameter. Leave it unplugged.

Two outputs, and which is which

Unlike most nodes in this pack, filterSpeckles_0 returns two nparrays: nparray_0 and nparray_1. In OpenCV's signature the function returns both the filtered image and the scratch buffer it used - so output 0 is your cleaned disparity map, output 1 is the buffer you almost certainly don't need. If you're piping to Nparrays2Image, feed it output 0 and ignore the second.

Installing it

Part of the ~600-node geroldmeisinger/opencv-comfyui pack. ComfyUI Manager → search opencv-comfyui, or:

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

then restart. Needs opencv-contrib-python, numpy, torch. If startup fails with Cannot import name 'guidedFilter' from 'cv2.ximgproc', conflicting OpenCV installs - consolidate to one.

House rules: nparrays in/out (Image2Nparray / Nparrays2Image), batch size 1.

The honest take

If your pipeline produces disparity maps - you're doing stereo depth, feeding SGBM, building a real-time 3D reprojection - this is the exact right tool and there isn't a cleaner drop-in alternative in ComfyUI. If you're not, this node is a curiosity: it's the pack's own best example of a function whose input isn't a photo and whose outputs aren't images in the ComfyUI sense. Don't force it. Depth-from-stereo is a niche, and this node lives entirely inside that niche.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
imgNPARRAY
newValFLOAT
maxSpeckleSizeINT
maxDiffFLOAT
bufoptNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY