Nodes/opencv-comfyui/OpenCV threshold_1
ComfyUI Node

OpenCV threshold_1

Threshold_1 — a second mask maker that's the same mask maker

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV threshold_1
  • src
  • dst
  • float
  • nparray
thresh
maxval
type

Straight answer first: threshold_1 and threshold_0 are the same cv2.threshold function wearing two labels. OpenCV's type stubs declare the function as multiple overloads, the pack's generator expanded each one into its own node, and the differences don't survive the translation. If you've been staring at both in the node list wondering which one to use - pick either. Workflows that reference one will happily accept the other.

What the function itself does is worth knowing regardless of the number, because it's the cheapest mask-maker in the pack. You give it a grayscale array and a cutoff, and every pixel above or below that cutoff gets slammed to one of two values. That's how you get a binary mask for ControlNet, a clean line-art layer, or a subject-vs-background split - deterministically, in milliseconds, no diffusion pass burned. The KB's post-processing essay makes this exact point: these are thirty-year-old image operations wrapped as nodes, and they beat a generation pass for jobs they were built for.

Inputs and outputs that matter

  • src (NPARRAY) - feed it a single-channel image; BGR in makes OpenCV unhappy.
  • thresh (FLOAT) - the brightness cutoff.
  • maxval (FLOAT) - what "on" means, usually 255.
  • type (INT) - the thresholding mode, typed as a number. 0 = binary (above → maxval), 1 = binary inverted, 2 = truncate, 3/4 = to-zero variants. Add 8 for Otsu or 16 for Triangle to have OpenCV pick the cutoff for you from the histogram.

Outputs are float (the cutoff actually used - relevant when you let Otsu choose) and nparray (the thresholded image). Send nparray into Nparrays2Image to turn it back into a Comfy IMAGE, or chain it into another OpenCV node.

Installation

One pack, both nodes - install geroldmeisinger/opencv-comfyui from ComfyUI Manager (search "OpenCV"), or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

Restart ComfyUI. The dependency is OpenCV contrib (pip install opencv-contrib-python - the README's opencv-python-contrib spells the package wrong). No model files.

Common issues

The recurring trio from this pack's README applies: error: (-215:Assertion failed) img.type() == CV_8UC1 means you skipped the grayscale conversion - run cvtColor with code=6 first. Only images with batch_size==1 are supported means you fed Image2Nparray a batch - split it with ImageFromBatch. And invalid syntax (<unknown>, line 0) is the pack-wide signature for a malformed Python-literal input, though threshold itself is blessedly free of composite literals. The main thing to get right is the type enum - the pack doesn't validate it, and a wrong integer just produces a confusing result.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
srcNPARRAY
threshFLOAT
maxvalFLOAT
typeINT
dstoptNPARRAY

Outputs (2)

NameTypeDescription
floatFLOAT
nparrayNPARRAY