Nodes/comfyui-tensorop/ForegroundMask
ComfyUI Node

ForegroundMask

Cut objects out of a photo with polygon JSON — and zero ML

By un-seen·Created 2 years ago·Updated 2 years ago· 46
ForegroundMask
  • image
  • json_data
  • IMAGE

ForegroundMask is the pack's no-model foreground extractor. You feed it an image and a JSON list of polygon outlines, and it cuts each outlined region out, scrubs the background out of the crop, and pastes the cleaned-up result back onto a full-size transparent canvas. The output is an RGBA image where the object sits isolated - useful for compositing, product shots, or just pulling one thing out of a busy scene.

The interesting part is how it cleans the background, because it does not use a learned segmentation model at all. No SAM, no BiRefNet, nothing to download. It's old-school image math: find the most common color in the crop (the "mode" color), compute each pixel's Euclidean distance from it, and call anything farther than the average distance the foreground. Those pixels get turned white and kept; the rest go transparent. If you've read up on ML-based background removal - BiRefNet, InSPyReNet, SAM and friends - this is a different, much cheaper category entirely. It's the geometric approach, and it only wins when the object sits on a solid, dominant-color background.

Where it sits in the pipeline

The JSON is a list of objects, each with a polygon field: a flat list of x, y coordinates, like [x1, y1, x2, y2, ...]. In this pack, that JSON is meant to come from its Florence-2 detection and SAM2 segmentation nodes - detect objects, get bounding polygons, hand them here. But the node doesn't care where the JSON came from. Any node that outputs a JSON list of polygons will drive it.

Inputs and outputs

  • image (IMAGE) - the photo you're cutting from.
  • json_data (JSON) - the list of {"polygon": [...]} items.
  • Output: IMAGE - full-size RGBA, each detected object cut out and pasted onto a black (fully opaque) canvas.

You can wire the output straight into a SaveImage/Composite-style node, or into further mask/image ops for compositing.

Installing it

Manager search title comfyui-tensorop, or:

cd ComfyUI/custom_nodes
git clone https://github.com/un-seen/comfyui-tensorops
cd comfyui-tensorops
pip install -r requirements.txt

Restart after. This node itself only needs numpy/PIL/torch - all already in ComfyUI - but the pack's requirements.txt installs the whole buffet including transformers and boto3 regardless. The README is empty, so the code is the documentation.

Where people get burned

  • The background must be dominant. The algorithm removes the single most common color in each crop. If the subject overlaps a busy or multi-colored background, the "background" it guesses is wrong and you get confetti, not a cutout.
  • The threshold is an average. Pixels are kept when they're farther than the mean distance from the mode color. That's a blunt instrument - high-contrast, noisy crops can blow out the subject or keep too much background.
  • Manager conflict warnings. This pack names nodes identically to Kijai's ComfyUI-Florence2, so ComfyUI Manager may warn about a clash. Kijai flagged it himself back in 2024. If you use his nodes, install deliberately.

Reach for this when you want fast, dependency-free cutouts with polygons you already have. If you need hair-level edges on a real photograph, you want an actual segmentation model - this is the cheap trick, not the SOTA one.

Categorytensorops

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
json_dataJSON

Outputs (1)

NameTypeDescription
IMAGEIMAGE