π Mask to Points
π Mask to Points β free SAM2 point prompts from any mask
- mask
- coordinates_positive
- coordinates_negative
SAM2's native language is points. Click inside an object to keep it, click a spot to exclude, done - it's how the original SAM was meant to be used. But hand-clicking gets old the moment you're segmenting a batch of frames or driving things from a script. If you've already got a mask, you don't need to click anything: π Mask to Points derives the positive and negative point prompts straight from the mask. That's the whole point of the pack.
It's from Glidias/mask2sam, the same repo as π¦ Mask to BBoxes, and it exists because the author (the maintainer of a Krita AI Diffusion fork) wanted to run SAM2 segmentations from Krita transparency-mask layers - paint a mask, get SAM2 points, no manual pointing. The outputs feed kijai/ComfyUI-segment-anything-2, which is the SAM2 node pack everyone actually uses.
The clever bit: the centroid problem
Naively, you'd place a point at each region's centroid and call it a day. But a C-shape, a donut, or any concave blob has its centroid floating in empty space - and a point outside the shape is a worthless prompt. So this node does something smarter: it traces each region's contour, runs Bayazit polygon decomposition to split the shape into convex pieces, and picks the centroid of the largest convex piece. The result is a point guaranteed to sit inside the shape, even for concave masks. That's the one real piece of engineering in this pack, and it's why the README brags about "suitable in-region" points.
What your mask colors mean
Same inverted convention as the sibling node, so read carefully:
- Black (0) - a shape for a positive point (keep this region)
- White (255) - do nothing, ignored
- Gray (1β240) - a shape for a negative point (exclude this region)
You get two outputs: coordinates_positive and coordinates_negative, both STRINGs holding JSON arrays of {"x": ..., "y": ...} pairs collected across the whole batch. Wire them into Kijai's SAM2 node as your point prompts.
The input that matters
- mask - the batch of
MASKtensors. - individual_objects (default true) - pairs every positive point with its nearest negative point. Turn this on when SAM2 is in "individual objects" mode, which expects positive/negative pairs per object. Caveat: multiple positive shapes can end up paired to the same nearest negative point, which is fine but worth knowing when you look at the output.
The trap: your colors are flipped
Yes, again: black is the object, white is nothing. Most ComfyUI mask tools hand you white-foreground masks, and this node will happily read all that white as "ignore" and treat the black background as your subject. If your points land on the background, invert the mask first - or better, paint black for what you want, like the Krita convention the author works in.
Two more gotchas from the source: it only works well on lossless masks, because the exact gray levels (1β240) are what define negative regions - no JPEG, no recompression. And regions under ~9 pixels fall back to a plain centroid, so don't expect the convex-decomposition magic on tiny blobs; clean those out of your mask if you can.
Install
ComfyUI Manager: search "mask2sam" and install, or do it by hand:
cd ComfyUI/custom_nodes
git clone https://github.com/Glidias/mask2sam
Restart ComfyUI afterwards. Dependencies are just scikit-image and shapely - both light and CPU-only. No model downloads; the SAM2 checkpoints live in Kijai's pack, so grab those there. This is a small, focused utility, and it does its one job well.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | β | |
| individual_objects | BOOLEAN | true | β |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| coordinates_positive | STRING | β |
| coordinates_negative | STRING | β |