Nodes/opencv-comfyui/OpenCV findNonZero_0
ComfyUI Node

OpenCV findNonZero_0

Where is your mask actually white? findNonZero finds out

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV findNonZero_0
  • src
  • idx
  • nparray

This is the little node that answers a boring but genuinely useful question: given a single-channel image or mask, exactly which pixels are non-zero, and where are they? cv2.findNonZero scans src and returns the (x, y) coordinates of every pixel that isn't zero. OpenCV findNonZero_0 is that function in ComfyUI form, and it's a building block for all kinds of "find the thing" logic.

Why would you want a list of coordinates? Because it's the raw material for everything geometric: feed the coordinates into minAreaRect or fitEllipse to fit a shape around a blob, use them to compute a centroid, or measure how much of a mask is actually set. It turns a vague "there's stuff here" mask into concrete, computable positions.

Inputs and outputs

The schema is tiny:

  • src - a single-channel NPARRAY (grayscale, or a binary mask from threshold). Required.
  • idx - an optional output array OpenCV can write into instead of allocating. You can usually ignore it; it's an out-parameter the pack exposes for completeness.

Output is one NPARRAY: an Nx1x2 array where each row is the (x, y) coordinate of a non-zero pixel. If nothing is non-zero, you get an empty array. And note the important detail: this output is not an image. It's a point list. The 'NoneType' object has no attribute 'shape' error shows up the second you try to preview it like a picture.

The gotchas that will actually hit you

It needs single-channel input. findNonZero will happily reject a 3-channel image with an assertion failure (img.type() == CV_8UC1). Your mask has to come in as one channel - run your image through this pack's cvtColor with code = 6 (BGR→GRAY) or use the output of a threshold node. This trips up basically everyone the first time.

Batch size 1. Like everything in this pack, input flows through Image2Nparray, which refuses batches bigger than one. If you get Only images with batch_size==1 are supported! batch_size=2, drop an ImageFromBatch node (length = 1) in front.

Coordinate order. The output rows are (x, y), not (y, x). That's fine until you feed them into a node that expects the opposite; OpenCV's drawing functions take (x, y), so this pack's nodes line up correctly.

Install

Part of geroldmeisinger/opencv-comfyui. ComfyUI Manager → search opencv-comfyui → install → restart, or:

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

requirements.txt installs opencv-contrib-python, numpy, torch. The pack imports cv2 at startup - if OpenCV is missing or duplicated (the guidedFilter import error is the classic symptom), the whole pack fails to register.

Verdict

findNonZero_0 is simple, and it's genuinely useful once you're doing any mask geometry. _1 is its byte-identical UMat twin. It won't wow anyone, but it's exactly the kind of unglamorous utility that makes a blob-tracking or measurement workflow possible.

Categoryimage/OpenCV

Inputs (2)

NameTypeDefaultDescription
srcNPARRAY
idxoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY