Nodes/tri3d-comfyui-nodes/Calculate max and min values for rescaling histogram v5.1.0
ComfyUI Node

Calculate max and min values for rescaling histogram v5.1.0

Find where your histogram actually starts and ends, for sane rescaling

By TRI3D-LC·Created 3 years ago·Updated about a year ago· 27
Calculate max and min values for rescaling histogram v5.1.0
  • luminosity_as_mask
  • histogram lower limit (x1) as INT
  • histogram upper limit (x2) as INT
threshold_fraction0.0010

Get histogram limits answers a question that comes up constantly when you're working with luminosity or depth-as-image: what range of values does this actually use? It takes a grayscale image (passed in as a MASK), builds its 256-bin histogram, and walks in from both ends, accumulating pixels until the cumulative fraction passes threshold_fraction. The bin it stopped at on the left is the lower limit; the one on the right is the upper limit. Both come back as INTs.

That's a roundabout way of saying "ignore the empty tails." A typical image's histogram has a few stray pixels at 0 and 255 that aren't real content - they're noise, borders, or JPEG edges. If you rescale based on the full 0–255 range, those tails eat your dynamic range and the result looks washed out. This node gives you the tight, meaningful bounds instead: the value range that actually contains your data.

Inputs

  • luminosity_as_mask - a MASK (grayscale) whose intensity you want to analyze. Works fine whether it's true luminosity, a depth map, or any single-channel signal you've stashed in a mask.
  • threshold_fraction - default 0.001 (0.1% of pixels). How much of the histogram tail to tolerate before you declare it "empty." Bigger values trim more aggressively; 0.001 is a good starting point for real images.

Outputs

Two INTs, named in the schema as histogram lower limit (x1) and histogram upper limit (x2). The x1/x2 naming is deliberate: these are designed to plug straight into the pack's tri3d-simple_rescale_histogram node, which takes x1/x2 as the source range and remaps it to a target y1/y2. Used that way, the pair becomes an automatic contrast stretch - compute the limits once, rescale, done.

When you'd reach for it

In this pack's luminosity/neck-adjustment workflows, it's the "measure first, then stretch" step: the histogram limits tell you where the tonal content sits before you equalize or remap it. The same pattern applies to any depth or lighting channel you want to normalize before feeding it to a controlnet - knowing the real min/max beats guessing.

Gotchas

  • The input is a MASK tensor, not an IMAGE. If you're coming from an RGB image, convert it (luminosity, or any single channel) before wiring it in - the node doesn't convert for you.
  • The output is a bin index (0–255), and the algorithm is a straightforward accumulator, so it reflects the actual histogram shape - a strongly bimodal image can give you surprising limits until you bump threshold_fraction up a bit.
  • Purely numpy inside the pack, no model, no extra deps. Runs instantly.

Small, boring, and exactly the kind of helper you don't appreciate until a washed-out rescale makes you wish you had the real bounds. If you're doing any kind of histogram remapping with this pack, you'll end up using it every time.

CategoryTRI3D

Inputs (2)

NameTypeDefaultDescription
luminosity_as_maskMASK
threshold_fractionFLOAT0.00100–0.5

Outputs (2)

NameTypeDescription
histogram lower limit (x1) as INTINT
histogram upper limit (x2) as INTINT