Nodes/opencv-comfyui/OpenCV hasNonZero_0
ComfyUI Node

OpenCV hasNonZero_0

Is there anything in this image?

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV hasNonZero_0
  • src
  • bool

hasNonZero_0 answers one question: does this nparray contain any pixel that isn't zero? It's the boolean version of countNonZero_0 (which gives you the integer count). One input, one bool output, nothing else. In a graph full of node spaghetti, it's the closest thing to a plain if-statement.

Why would you care? Because it's a gate. Feed it a mask and you can branch a workflow on whether the mask actually found anything - did the edge detector produce output, is this frame all black, did the segmentation come back empty? In the pack's world you're always one step away from a downstream node that explodes on an empty input, so a cheap "is this real?" check before you spend compute on it is genuinely useful.

How it works

Straightforward: OpenCV's cv2.hasNonZero() scans the array and returns True if any single element is non-zero. That's it - there's no threshold, no tolerance, no "meaningful amount". A single stray pixel from noise flips it to True. So it's a sanity check, not a quality metric. If you need "is there enough", count the pixels with countNonZero_0 and compare against a threshold downstream.

The src input takes any NPARRAY. That includes masks, single-channel arrays, or the raw BGR image itself - hasNonZero doesn't care about channel count. If your image came straight out of Image2Nparray, remember you're handing it a BGR uint8 array, not the Comfy RGB tensor.

The one input, the one output

  • src (NPARRAY) - anything you want to test.
  • bool (BOOLEAN) - True if any element is non-zero, False if the array is entirely zeros.

That bool output wires into anything that accepts a boolean - a switch node, a conditional, an if-style router. If your graph doesn't have one of those handy, the boolean will at least show up in the UI so you can watch what's happening while you debug.

Installing it

It's one of ~635 nodes in the auto-generated opencv-comfyui pack, so you install the pack:

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

or find "OpenCV" in ComfyUI Manager. Then make sure OpenCV exists:

pip install opencv-contrib-python

No models, no downloads beyond that.

Gotchas

The only real trap is expecting it to be a threshold. It isn't. hasNonZero is binary and naive - one hot pixel in a 4K image returns True. If your gate keeps passing when you thought it should fail, that's why. Also note it works on the nparray representation: if you feed it a Comfy IMAGE tensor by mistake you'll get a type error, because this pack only speaks NPARRAY. Convert first.

Honestly, hasNonZero_0 is a thin node - but it's the kind of thin node that saves you a crash three nodes downstream.

Categoryimage/OpenCV

Inputs (1)

NameTypeDefaultDescription
srcNPARRAY

Outputs (1)

NameTypeDescription
boolBOOLEAN