A ComfyUI custom node that smooths mask contours using the Fourier Descriptor algorithm. It takes a mask as input, applies low-pass filtering in the frequency domain, and outputs a smoothed mask.
| Input Mask | Smoothed Mask (descriptors=32) |
|---|---|
| Jagged, noisy contour | Clean, smooth contour |
- Clone or copy this folder into your ComfyUI
custom_nodes/directory:
cd /path/to/ComfyUI/custom_nodes
git clone https://github.com/bemoregt/ComfyUI_CustomNode_MaskSmoothing.git- Install dependencies:
pip install -r custom_nodes/ComfyUI_CustomNode_MaskSmoothing/requirements.txt- Restart ComfyUI.
The node will appear under mask/processing → Mask Fourier Smoothing.
Connect a MASK output to the node's mask input. Adjust num_descriptors to control the smoothing strength.
[Load Image] ──► mask ──► [Mask Fourier Smoothing] ──► smoothed_mask ──► [Preview Mask]
| Parameter | Default | Range | Description |
|---|---|---|---|
mask |
— | — | Input mask (MASK type) |
num_descriptors |
32 | 4 – 512 | Number of Fourier frequency components to keep. Lower = smoother contour. Higher = closer to original. |
The algorithm follows these steps:
- Binarize the input mask (threshold at 0.5)
- Extract contours using
cv2.findContours - Encode contour points as complex numbers:
z = x + j·y - Apply FFT to obtain the Fourier Descriptors
- Low-pass filter: keep only the lowest
num_descriptorsfrequency components (zero out the rest) - Apply inverse FFT to reconstruct the smoothed contour
- Fill the reconstructed contour to produce the output mask
The key insight is that high-frequency Fourier components correspond to sharp corners and jagged edges, while low-frequency components capture the overall smooth shape. By discarding high-frequency components, the contour is effectively smoothed.
numpyopencv-pythontorch
MIT License
