OpenCV threshold_0
Threshold
- src
- dst
- float
- nparray
Thresholding is the workhorse of image preprocessing, and it's the node in this pack most likely to actually earn its keep in a real workflow. The idea is dead simple: pick a brightness cutoff, and every pixel either becomes one value or another. That's how you turn a grayscale image into a clean binary mask, how you pull line art out of a sketch for ControlNet, and how you separate a bright subject from a dark background without any model doing heavy lifting. It's a deterministic pixel operation - the exact "reach for a filter, not a diffusion pass" move the post-processing KB keeps telling you to make.
This is threshold_0, which is cv2.threshold - the basic, per-pixel version. There's a threshold_1 sibling that is the same function from a different overload line in OpenCV's type stubs; they behave identically here, so don't stress about the number.
How it works
The node compares every pixel in src against thresh (FLOAT). What happens on each side depends on type (INT), which is an OpenCV enum you'll be typing by hand because the pack doesn't give you a dropdown:
0-THRESH_BINARY: above thresh → maxval, below → 0. The classic.1-THRESH_BINARY_INV: inverted, below → maxval.2-THRESH_TRUNC: above thresh → clamped to thresh (keeps the gradient above the line).3/4-THRESH_TOZERO/TOZERO_INV: zero out one side, keep the other's values.8/16-THRESH_OTSU/THRESH_TRIANGLE: ignore thresh and let OpenCV compute the cutoff automatically from the histogram. These are great when you don't know the right value; the actually-used threshold comes back in the float output.
You need a single-channel input for most of these - feed it BGR and you'll hit the CV_8UC1 assertion error, which is your cue to run cvtColor (code=6) first.
The inputs and outputs that matter
- src (NPARRAY) - ideally grayscale.
- thresh (FLOAT) - the cutoff.
- maxval (FLOAT) - the "on" value for BINARY modes, typically
255. - type (INT) - the enum above. This is the one that decides everything.
The outputs are float (the threshold actually applied - mostly interesting for Otsu) and nparray (the result). Wire nparray into another OpenCV node or Nparrays2Image to get back an IMAGE.
Installation
Install the pack once - ComfyUI Manager (search "OpenCV"), or git clone https://github.com/geroldmeisinger/opencv-comfyui into ComfyUI/custom_nodes, then restart. It needs opencv-contrib-python (pip install opencv-contrib-python); the README's opencv-python-contrib is a typo for that package name. No models.
Common issues
The CV_8UC1 grayscale assertion is by far the most common wall here. Also remember the pack works on NPARRAY at batch size 1 - convert with Image2Nparray in, Nparrays2Image out, and split batches with ImageFromBatch. One more trap: if you leave type at a random integer, nothing errors, you just get a wrong-looking result - the enum values are not validated for you.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| src | NPARRAY | — | |
| thresh | FLOAT | — | |
| maxval | FLOAT | — | |
| type | INT | — | |
| dstopt | NPARRAY | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| float | FLOAT | — |
| nparray | NPARRAY | — |