findCenterOfMask-badger
Where is the thing? A mask's center of mass as clean X/Y coordinates
- mask
- X
- Y
A mask tells you where something is - but only if you can turn it into numbers. findCenterOfMask-badger does that in one step: it computes the center of mass of the mask and hands you back an X and a Y coordinate. That's it. And a pair of coordinates is exactly what a surprising number of workflows want.
The most common target is segmentation. ComfyUI's SAM (Segment Anything) nodes need a point to click on, and point-based prompting is a hundred times more reliable when the point is inside the object instead of on its edge. So the pattern goes: rough mask → findCenterOfMask → feed X/Y into SegmentToMaskByPoint-badger (same pack, literally designed to take those FLOAT outputs) → clean segmentation mask. You can also use the coordinates to position a crop, drive a CropImageByMask follow-up, or build a bounding box for an inpainting target.
How it works
Center of mass, computed in pixel space. The code sums the mask's mass along each axis, weights each coordinate by the mask value, and divides by total mass - the same centroid math a physics engine would use. Since the mask is white (1.0) where the subject is and black (0.0) elsewhere, the result is the geometric middle of the bright region. For a blob-shaped subject that's a much better "here it is" point than the bounding-box center, which gets dragged around by empty corners.
Inputs and outputs
One input:
- mask - a MASK tensor. Expects it to be 2D; it squeezes the batch/channel dims automatically.
Two outputs, both FLOATs:
- X - horizontal center, in pixels.
- Y - vertical center, in pixels.
They're floats, not ints, which matters when you feed them into SAM point inputs that accept fractional coordinates. The node runs a GPU cache flush when it's done (all this pack's nodes do - the author's house style), so it's also a quiet little memory cleaner.
Installing it
From ComfyUI_BadgerTools, via ComfyUI Manager (search "BadgerTools") or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/AbyssYuan0/ComfyUI_BadgerTools
Restart after installing. No models needed. Pack-wide note: the requirements file is heavy and imports on startup even for a node this small, so the first launch after install is slower than the node itself deserves.
Gotchas
- An empty (all-black) mask returns (0, 0). The code explicitly falls back to zero instead of erroring, so if your coordinates suddenly snap to the top-left corner, your mask is empty - not the node breaking. Check upstream.
- Coordinates are in image pixels, not normalized 0–1. If the node you're feeding expects normalized values, divide by width and height yourself.
- It's one mask at a time. Batches won't behave.
No community threads exist for this node - it's a personal-pack utility with a two-line README, so you're not missing a settings wiki; there are no settings. For "give me a point inside this thing" it's the smallest possible answer, and in a graph that's usually the right size.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| X | FLOAT | — |
| Y | FLOAT | — |