Nodes/FlowCV/OTSU自适应二值化
ComfyUI Node

OTSU自适应二值化

OTSU in FlowCV

By Bit-Walker·Created about a year ago·Updated 7 months ago· 3
OTSU自适应二值化
  • 图像输入
  • 图像输出
反转

If you're tired of babysitting a threshold slider, this is the node you want. FlowCV's OTSU node (OTSU自适应二值化) picks the threshold for you - no tuning, one input, done. Where FCV_Threshold asks you to choose a cutoff and FCV_AdaptiveThreshold asks you to pick block sizes, OTSU (named after Nobuyuki Otsu, who published the algorithm in 1979) looks at your image's histogram and computes the cutoff that best splits it into two classes.

When it shines (and when it doesn't)

Otsu assumes your image's histogram is roughly bimodal - two clear humps, one for background and one for foreground. That's exactly the situation you get with a clean scan of a page, a barcode, a document, or a product on a plain background. In that world the automatic threshold is usually as good as anything you'd hand-tune, and often better.

It falls apart on the same cases that break fixed thresholds: severe lighting gradients (that's what FCV_AdaptiveThreshold exists for) or histograms that aren't bimodal - flat gray scenes or images where foreground and background blend. If the result looks wrong, don't fight it; switch nodes rather than nudging nonexistent knobs.

The mechanism

The node converts your input to grayscale, then calls cv2.threshold(..., 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU). The 0 as threshold is a placeholder - OpenCV ignores it and instead exhaustively scans candidate cutoffs, picking the one that minimizes intra-class variance (equivalently, maximizes the separation between the two resulting pixel classes). Because it's a single pass over a histogram rather than a per-pixel computation, it's fast - faster in practice than adaptive thresholding.

Its one input, 反转 (invert), defaults to and flips the output to THRESH_BINARY_INV if set to . If your binarized result comes out as black text on white when you wanted white on black, that toggle is the entire fix. There's no max-value or type dropdown; the output is always hard black-and-white.

Wiring it up

As with the rest of the pack, in goes a CVIMAGE (OpenCV BGR numpy array) and out comes a CVIMAGE. It's a natural pre-step to FCV_FindRectangles or FCV_Canny, or to any mask-building workflow - run it through FCV_CVToIMAGE to actually view the result or hand it to a normal ComfyUI node.

Installing

It's one of the 16+ nodes bundled in FlowCV, so one install covers everything. ComfyUI Manager, search "FlowCV"; or:

cd ComfyUI/custom_nodes
git clone https://github.com/Koren-cy/FlowCV

Restart ComfyUI. Dependencies are light - opencv-python, numpy, pyserial, zero model downloads. One thing to keep in mind: the README notes the project migrated to ComfyUI_For_Academic, so this repo is effectively archived. It still works, but treat it as finished rather than under development.

Gotchas

Because the node converts its output back to 3-channel BGR (the pack's consistency choice), a "binary" image here is still three channels - that's fine for downstream FlowCV nodes. And remember the pack-wide habit: errors print to the ComfyUI console in Chinese and return the input untouched, so a node that "does nothing" usually means check the terminal.

CategoryopenCV/二值化

Inputs (2)

NameTypeDefaultDescription
图像输入CVIMAGE输入的openCV格式图像
反转COMBO是否反转二值化结果(黑白互换)

Outputs (1)

NameTypeDescription
图像输出CVIMAGEOTSU二值化处理后的图像