SEGS Refine (Morph)
Grow masks with edges and color keeping them honest
- segs
- image
- refined_segs
The sibling to the pack's K-Means refine, XJSegsRefineMorph takes the other route to fixing under-segmented masks: it grows them with morphological dilation, but uses image features to decide where the growth is allowed to go. Where K-Means reasons about color clusters, this node watches for edges - the classic sign that the mask is about to cross from subject into background - and stops the growth there.
The pipeline per segment is: dilate the mask by grow_pixels, smooth the result with a Gaussian (smooth_pixels, which doubles as the sigma), and gate the growth using either edge detection or a color check. The edge_threshold controls how sensitive that gate is - lower values trigger on fainter edges, so the growth stops sooner. The use_edge_detection toggle is the escape hatch: if edges in your image are too aggressive (busy clothing, fine detail everywhere) and the mask keeps stalling early, switch it off and lean on color_threshold instead - that stops growth only when a pixel's color drifts more than the threshold from the mask's average color, which is the right tool for soft, samey subjects like fur.
The interface:
segs- SEGS in.image- the source image (first frame of the batch is used).grow_pixels- 0–200, default 50. How far to dilate.smooth_pixels- 0–20, default 3. Gaussian sigma; higher = smoother, less detailed edges.edge_threshold- 0–1, default 0.3. Lower = more sensitive to edges.use_edge_detection- boolean, default on. Disable when edges are too aggressive.color_threshold- 0–1, default 0. If > 0, stops growth past a color distance instead of relying on edges.- Output:
refined_segs.
When to pick Morph over K-Means: when your subject is defined more by shape than by flat color regions. Hair against a clean background, a person in front of a wall - edge-based growth tracks the outline precisely. When the subject and background share a palette, K-Means is often the better call. It also prints progress info to the console per segment (mask shapes, sizes), which is genuinely handy for debugging, if a little chatty.
Install is the pack-wide routine:
cd ComfyUI/custom_nodes
git clone https://github.com/alexjx/ComfyUI-XJNodes
Restart ComfyUI, find it under XJNodes/segs, or install via ComfyUI Manager by searching "ComfyUI-XJNodes". No models; requirements.txt is empty, and it leans on scipy's ndimage, which stock ComfyUI ships.
Standard personal-pack caveats: no tutorials out there, and morphological refinement rewards experimentation. Change one knob at a time, and preview the mask before committing a whole batch to it.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| segs | SEGS | — | |
| image | IMAGE | — | |
| grow_pixels | INT | 500–200 | Number of pixels to dilate/grow. Use with edge_threshold and color_threshold to control growth. |
| smooth_pixels | INT | 30–20 | Sigma for Gaussian smoothing. Higher values create smoother but less detailed edges. |
| edge_threshold | FLOAT | 0.300–1 | Threshold for edge-based refinement (0-1). Lower values are more sensitive to edges. Use with use_edge_detection to control growth at edges. |
| use_edge_detection | BOOLEAN | true | Whether to use edge detection to limit growth. Disable for similar colors or when edge detection is too aggressive. |
| color_threshold | FLOAT | 0.000–1 | If > 0, stop growth when pixel color differs from mask average by more than this threshold (0-1). Use instead of edge detection for similar colors. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| refined_segs | SEGS | — |