ComfyUI Node Runs on cloud

OtsuThreshold

Stop hand-tuning thresholds — Otsu finds the line between black and white for you

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
OtsuThreshold
  • image
  • IMAGE
threshold_typeBINARY
gaussian_blur_x4
gaussian_blur_y4
gaussian_border_typeBORDER_CONSTANT

Converting a grayscale image to a clean black-and-white mask always starts with the same question: what's the cutoff? Set it too low and everything's white; too high and your subject disappears. OtsuThreshold answers the question for you. It's a direct wrapper around OpenCV's Otsu's method, which analyzes the image's histogram and picks the threshold that best separates the two peaks - the "valley" between the bright pixels and the dark ones. You feed it an image, you get a clean binary mask, and you never touch a threshold slider.

This is the node that makes mask workflows feel like magic when you're coming from hand-set thresholds. It's why it lives under Bmad/CV/Thresholding alongside AdaptiveThresholding and FindThreshold - this pack takes the "stop guessing, let the statistics decide" approach seriously.

The inputs

  • image - anything you want binarized. It's converted to grayscale internally, so a color photo works fine.
  • threshold_type - BINARY, BINARY_INV, TRUNC, TOZERO, TOZERO_INV. You'll use BINARY (white subject, black background) and BINARY_INV (inverted) 95% of the time; TRUNC and the TOZERO variants are the less-common OpenCV outputs.
  • gaussian_blur_x / gaussian_blur_y (0–200, stepped in 2s) - an optional blur before thresholding. This is your real dial: a little blur (4–8) smooths noise so Otsu finds a cleaner split. 0 disables it.
  • gaussian_border_type - how the blur handles image edges (BORDER_CONSTANT, BORDER_REPLICATE, etc.). Leave it unless you see edge artifacts.

Output is a single IMAGE (returned as RGB, because that's what most ComfyUI mask consumers expect).

How the blur interacts with Otsu

The two steps fight each other slightly, and understanding that makes the node predictable. The blur averages pixels together, which narrows the histogram peaks - that's good, it reduces speckle. But a blur that's too big starts blending your foreground and background into one mush, and Otsu has nothing clean to split. Rule of thumb: start at 4–8, look at the result, and only raise it if the mask is noisy. If raising the blur makes the mask worse, lower it instead - the optimal blur is usually small.

Where it fits

Any time you need a mask from an image without a hand-picked threshold: edge maps from preprocessors, lighting maps, scanned/texture images, or cleaning up a noisy mask before inpainting. It also chains beautifully with the pack's morphology nodes - Otsu to binarize, then OPEN/CLOSE to tidy the result.

Installing it

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_bmad_nodes

restart, or search comfyui_bmad_nodes in ComfyUI Manager. OpenCV does the heavy lifting; no model downloads.

The one trap

Otsu assumes the histogram has two distinguishable peaks. On a flat, low-contrast image it'll still pick something, and that something will be arbitrary - a basically-gray input can produce a salt-and-pepper mask that looks like static. If your input is low-contrast, blur it harder or use a different thresholding node (FindThreshold in the same pack lets you inspect). Also remember the blur kernel needs both dimensions to work - setting one to 0 disables blur entirely rather than blurring one axis.

CategoryBmad/CV/Thresholding

Inputs (5)

NameTypeDefaultDescription
imageIMAGE
threshold_typeCOMBOBINARY5 options: BINARY, BINARY_INV, TRUNC, TOZERO, TOZERO_INV
gaussian_blur_xINT40–200
gaussian_blur_yINT40–200
gaussian_border_typeCOMBOBORDER_CONSTANT8 options: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_REFLECT101, BORDER_WRAP, BORDER_TRANSPARENT, +2

Outputs (1)

NameTypeDescription
IMAGEIMAGE