AGSoft Mask Fix
Erode, fill, feather and binarize in one node
- mask
- mask_out
- convert_mask_to_image
Raw masks are messy. Segmenters hand you specks, holes where there shouldn't be holes, and edges that look like a staircase. Fixing that used to mean three or four separate nodes. AGSoft Mask Fix is the AGSoft pack's all-in-one mask janitor: it erodes or dilates, fills holes, sweeps out isolated specks, smooths, feathers, binarizes, and can invert - all in one pass. If you've got a mask pipeline, this is the node you'll reach for most.
How it works
The function is a pipeline, and it runs in a sensible order. The mask is scaled to 0–255, then:
- erode_dilate - positive expands the mask (dilation), negative shrinks it (erosion). The classic way to fix a mask that's too tight around a subject.
- fill_holes - closes black holes inside white regions (eyes inside a face mask, for instance).
- remove_isolated_pixels - deletes small stray white specks; higher = more aggressive sweeping.
- smooth - repeated smoothing with re-binarization, which the tooltip calls out for killing "staircase" edges.
- expand - a second grow/shrink dial, with tapered_corners (default on) making it smooth and rounded rather than blocky.
- feather - the gradient soften on the final edges, same idea as a dedicated blur.
- threshold - binarizes at the end; values at/above it become white (1.0), below become black. Default 0.5.
- invert - flips the whole result if you need the background instead of the subject.
That ordering is the useful bit: operations compound before the final threshold, so you can dilate then clean, and feather last so the blur isn't re-clipped by later steps. It outputs mask_out (MASK) plus convert_mask_to_image (IMAGE) for previewing.
The inputs a beginner actually sets
- erode_dilate - start here. +a few px if the mask clips your subject, −a few px if it bleeds into the background.
- feather - 0–256, default 0. The single most common fix for inpaint seams; 2–8 px is usually plenty.
- fill_holes - flip on when your mask has donut holes.
- threshold - leave at 0.5 until you see noise at the edges.
The rest are refinements. device (cpu/cuda, default cpu) is safe to leave alone.
Installing and troubleshooting
cd ComfyUI/custom_nodes
git clone https://github.com/Art-xmaster/comfyui-AGSoft
Restart (or ComfyUI Manager → "comfyui-AGSoft"). The pack pulls in numpy and opencv-python; no model files. If a setting seems to do nothing, check the order-of-operations fact above - a huge smooth then a 0.5 threshold can erase an earlier feather. And if the mask comes out with harsh pixel steps no matter what, it's usually because threshold is doing its final hard cut after your soft feathered edge: raise feather or lower threshold slightly rather than fighting it. For everything else, this node genuinely replaces three others in the graph.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| erode_dilate | INT | 0-256–256 | Положительное значение — расширение маски (дилатация), отрицательное — сужение (эрозия). |
| fill_holes | BOOLEAN | false | Заполняет чёрные дыры внутри белых областей маски (например, глаза внутри лица). |
| remove_isolated_pixels | INT | 00–32 | Удаляет мелкие изолированные области (шум). Чем выше — тем агрессивнее очистка. |
| smooth | INT | 00–256 | Многократное сглаживание с бинаризацией. Эффективно убирает 'лесенки'. |
| expand | INT | 0-256–256 | Дополнительное расширение/сужение маски. При 'tapered_corners=True' — плавное. |
| tapered_corners | BOOLEAN | true | Если включено — расширение/сужение происходит с плавными (скруглёнными) краями. |
| feather | INT | 00–256 | Растушёвка краёв маски. Создаёт градиентный переход на указанное количество пикселей. |
| threshold | FLOAT | 0.500–1 | Порог бинаризации после всех операций. Значения >= порога → белые (1.0). |
| invert | BOOLEAN | false | Инвертирует финальную маску: чёрное становится белым и наоборот. |
| device | COMBO | cpu | Устройство для вычислений. 'cuda' ускоряет обработку на GPU, но требует совместимой видеокарты. |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| mask_out | MASK | — |
| convert_mask_to_image | IMAGE | — |