Nodes/comfyui-AGSoft/AGSoft Mask Fix
ComfyUI Node

AGSoft Mask Fix

Erode, fill, feather and binarize in one node

By Art-xmaster·Created 12 months ago·Updated a day ago· 23
AGSoft Mask Fix
  • mask
  • mask_out
  • convert_mask_to_image
erode_dilate0
fill_holesfalse
remove_isolated_pixels0
smooth0
expand0
tapered_cornerstrue
feather0
threshold0.50
invertfalse
devicecpu

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:

  1. 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.
  2. fill_holes - closes black holes inside white regions (eyes inside a face mask, for instance).
  3. remove_isolated_pixels - deletes small stray white specks; higher = more aggressive sweeping.
  4. smooth - repeated smoothing with re-binarization, which the tooltip calls out for killing "staircase" edges.
  5. expand - a second grow/shrink dial, with tapered_corners (default on) making it smooth and rounded rather than blocky.
  6. feather - the gradient soften on the final edges, same idea as a dedicated blur.
  7. threshold - binarizes at the end; values at/above it become white (1.0), below become black. Default 0.5.
  8. 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.

CategoryAGSoft/Mask

Inputs (11)

NameTypeDefaultDescription
maskMASK
erode_dilateINT0-256–256Положительное значение — расширение маски (дилатация), отрицательное — сужение (эрозия).
fill_holesBOOLEANfalseЗаполняет чёрные дыры внутри белых областей маски (например, глаза внутри лица).
remove_isolated_pixelsINT00–32Удаляет мелкие изолированные области (шум). Чем выше — тем агрессивнее очистка.
smoothINT00–256Многократное сглаживание с бинаризацией. Эффективно убирает 'лесенки'.
expandINT0-256–256Дополнительное расширение/сужение маски. При 'tapered_corners=True' — плавное.
tapered_cornersBOOLEANtrueЕсли включено — расширение/сужение происходит с плавными (скруглёнными) краями.
featherINT00–256Растушёвка краёв маски. Создаёт градиентный переход на указанное количество пикселей.
thresholdFLOAT0.500–1Порог бинаризации после всех операций. Значения >= порога → белые (1.0).
invertBOOLEANfalseИнвертирует финальную маску: чёрное становится белым и наоборот.
deviceCOMBOcpuУстройство для вычислений. 'cuda' ускоряет обработку на GPU, но требует совместимой видеокарты.

Outputs (2)

NameTypeDescription
mask_outMASK
convert_mask_to_imageIMAGE