Nodes/tri3d-comfyui-nodes/Get threshold for bg swap v5.1.0
ComfyUI Node

Get threshold for bg swap v5.1.0

Let the gradient find the background cut point for you

By TRI3D-LC·Created 3 years ago·Updated about a year ago· 27
Get threshold for bg swap v5.1.0
  • subject_image
  • output histogram threshold
gradient_threshold0.0001

Get threshold for bg swap does the fiddly measurement part of a background swap so you don't have to eyeball it. It takes your subject image, converts it to LAB, pulls the L (lightness) channel, inverts it, and computes a histogram. Then it walks through the bins and watches for the first place where the histogram jumps - a bin whose value differs from its neighbor by more than gradient_threshold. That jump is where the subject's brightness ends and the background's brightness begins, and the bin index is returned as an INT.

The logic: most product shots have a background that's clearly separated in brightness from the subject. The brightness histogram is roughly two clusters, and the steepest gradient between them is the natural cut point. By returning that single threshold instead of a mask, this node is meant to feed into the pack's tri3d-simple_bg_swap node, which does the actual separation using the threshold you (or this node) provide. It turns "where do I cut?" from a guess into a measurement.

Inputs

  • subject_image - the image you plan to swap the background of.
  • gradient_threshold - default 0.0001, and it's the one knob. How large an adjacent-bin jump counts as "the edge of the subject." Too small and noise trips it; too large and it skips past the real boundary. For clean studio shots, the default is fine; for noisy or textured backgrounds, nudge it up.

Output

One INT, labeled output histogram threshold. Wire it to the threshold input of the bg-swap node (or any threshold-driven mask operation) and let the value drive the cut.

When it works - and when it won't

This is a heuristic built for a specific world: the flat, bright, evenly-lit backgrounds of catalog photography. On those, it's genuinely clever - the brightness boundary is real and measurable, and it beats hand-picking a threshold every time. The same approach falls apart the moment the background is textured, the subject is partially transparent, or the lighting spills across the boundary. If the swap starts mangling hair or clothing edges, the threshold isn't the problem - the assumption of a clean brightness split is.

Install and gotchas

Same pack story:

cd ComfyUI/custom_nodes
git clone https://github.com/TRI3D-LC/tri3d-comfyui-nodes
# restart ComfyUI

ComfyUI Manager (search "tri3d") also works. Keep the folder named tri3d-comfyui-nodes.

No model downloads, no GPU. It's a single LAB-histogram pass over one image, effectively instant. The main trap is the inverted-L step - don't be surprised that a "bright subject on dark background" gives a different cut point than the reverse; the node assumes the subject is brighter than the background. If your setup inverts that, pre-invert the image and the logic follows.

For the catalog-shot workflow this pack targets, it's a solid little automator. Just know it's measuring a brightness cliff, and it only helps where a cliff exists.

CategoryTRI3D

Inputs (2)

NameTypeDefaultDescription
subject_imageIMAGE
gradient_thresholdFLOAT0.00010–1

Outputs (1)

NameTypeDescription
output histogram thresholdINT