SEGS Refine (K-Means)
Grow masks along real color boundaries
- segs
- image
- refined_segs
Automatic segmentation is rarely pixel-perfect. SAM and the YOLO crowd produce masks that are under-segmented - they capture the core of the subject but stop a few pixels short, slicing through hair, fur, or fabric where the true boundary runs. Blowing the mask up blindly fixes that but bleeds into the background. XJSegsRefineKMeans is the smarter version: it grows the mask but lets it follow the actual color structure of the image, using k-means clustering so the growth stops when the color stops belonging to the subject.
Here's how it actually works. For each segment, the node crops the image region, runs k-means on the crop's colors to split it into clusters, then decides which clusters to absorb into the mask. The similarity_threshold is the strictness dial - higher means only clusters very close to the original mask's colors get included, giving you a tight, boundary-following growth; lower means it grabs more, at the risk of eating background. grow_pixels caps how far the mask can spread, so even a loose threshold can't run away across the whole image.
The interface:
segs- SEGS in.image- the full image the segments came from (it uses the first frame of the batch for refinement).n_clusters- 2–10, default 3. More clusters capture finer color variation but slow the pass down.grow_pixels- 0–200, default 50. Maximum growth in pixels.similarity_threshold- 0–1, default 0.5. Higher = stricter inclusion.- Output:
refined_segs.
Where it shines: cleanup before a detail pass. Feed it the output of a SAM or BBOX detector whose masks are too tight, and the refined mask that comes out is the one you actually want the Detailer to work from - one that includes the full face, the whole head of hair, the entire body. It's the "fix the under-segmentation" step, and it pairs well with the pack's Refine (Morph) node when you need to switch strategies per subject.
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 model files; requirements.txt is empty. It does import scikit-learn (KMeans) and scipy, both of which stock ComfyUI installs carry in practice - if you ever see a missing-import error, install them into the same Python environment and you're done.
Honest caveat: personal-use pack, zero tutorial footprint, and k-means refinement is inherently a tuning exercise - start with the defaults, then move similarity_threshold in small steps and inspect the mask with a preview node before you trust it.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| segs | SEGS | — | |
| image | IMAGE | — | |
| n_clusters | INT | 32–10 | Number of k-means clusters to use for color segmentation. More clusters can capture more color variation but may be slower. |
| grow_pixels | INT | 500–200 | Maximum pixels to grow the mask. Use with similarity_threshold to control how far the growth extends. |
| similarity_threshold | FLOAT | 0.500–1 | Threshold for including similar clusters (0-1). Higher = more strict. Use to include clusters that are close in color to the original mask, for smoother boundaries. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| refined_segs | SEGS | — |