Nodes/ComfyUI_MaskSmoothing/Mask Fourier Smoothing
ComfyUI Node

Mask Fourier Smoothing

Tired of jagged mask edges? Let Fourier do the dirty work

By bemoregt·Created 7 months ago·Updated 7 months ago· 1
Mask Fourier Smoothing
  • mask
  • smoothed_mask
num_descriptors32

Every mask you generate is a little lie about its own edges. BiRefNet and SAM give you fantastic segmentation, then hand you a boundary that looks like it was drawn by someone with a very unsteady hand - steppy, pixel-quantized, occasionally frizzy around hair or fabric. Feed that into inpainting or a composite and you get seams and that tell-tale "bad photoshop" outline. Mask Fourier Smoothing is a one-trick node that fixes exactly that: it takes the jagged contour, smooths it properly in the frequency domain, and hands you back a clean silhouette. No blur softening, no edge erosion - just the shape, sanded down.

How it actually works

This is Fourier descriptor smoothing, a technique so old it predates the diffusion models you're using it on. The node binarizes your mask at 0.5, pulls the boundary with OpenCV's findContours, and encodes every boundary point as a complex number (x + j·y). Then the magic: it runs an FFT, which decomposes the contour into sine waves. Sharp corners and jaggies live in the high frequencies; the overall shape lives in the low ones. Zero out the highs, inverse-FFT back, and you've got the smooth shape. It fills the result with fillPoly and returns a MASK.

Worth knowing before you fall in love: this is contour smoothing, not feathering. The output is a hard, binary mask - 0s and 1s. If you need a soft edge for seamless inpainting, follow it with a Gaussian blur or feather node. And it's binary in on the way too: any soft gradient in your input mask gets snapped at the 0.5 threshold before smoothing even starts. A SAM mask that's already soft-edged will lose its softness. That's not a bug, it's the deal.

The knobs that matter

Just two inputs, and you'll mostly touch one:

  • mask - any MASK output. From BiRefNet, SAM, an inpainting mask, or a hand-drawn one.
  • num_descriptors (default 32, range 4–512, step 4) - how many frequency components to keep. Lower = smoother, more cartoonishly rounded. Higher = closer to the original contour. This is the whole control surface, and it's the only one you'll fiddle with.

The output is smoothed_mask, a MASK you can wire straight into inpaint, mask-to-image, or a composite. A few practical starting points: 16–24 for a visibly smoother edge that still respects the shape, 32 (the default) for a light touch, and single digits if you want something almost bubbly. One caveat from the implementation: very low counts can overshoot at corners - the reconstruction rings a bit like a compressed JPEG, so don't bottom it out expecting a miracle.

Install

It's in ComfyUI Manager - search for MaskSmoothing (or "Mask Fourier Smoothing") and install. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/bemoregt/ComfyUI_MaskSmoothing

Then restart ComfyUI; the node appears under mask/processing → Mask Fourier Smoothing. The README lists pip install -r requirements.txt (numpy, opencv-python, torch), but realistically all three are already in your ComfyUI environment - nothing heavy to download, no model files, it runs on CPU in numpy. One trap: the README's clone URL still says ComfyUI_CustomNode_MaskSmoothing.git, which is the repo's old name and 404s today. Use the current ComfyUI_MaskSmoothing name above and you're fine.

Gotchas

Two behaviors will bite you if you're not looking. First, holes get filled. The node uses findContours with external-contour retrieval only, so a donut-shaped mask - a hand with a gap between fingers, a frame with an empty interior - comes back solid. If your mask has meaningful holes, this isn't your node. Second, the binarization: if you pipe in a blurred mask expecting it to stay soft, you'll get a hard-edged result at half the intended fidelity. For masks with holes, or when you genuinely want feathered edges for compositing, the WAS Suite and Impact Pack mask tools are the ones to reach for; this node is the specialist for clean, evenly-rounded silhouettes.

It's a small pack - one node, no author reputation to speak of, no community chatter. But for the narrow job of "make this jagged segmentation mask look hand-traced," it's fast, dependency-light, and does exactly what it says. Reach for it after any automatic segmentation, before any inpaint or composite.

Categorymask/processing

Inputs (2)

NameTypeDefaultDescription
maskMASK
num_descriptorsINT324–512

Outputs (1)

NameTypeDescription
smoothed_maskMASK