Nodes/opencv-comfyui/OpenCV moments_1
ComfyUI Node

OpenCV moments_1

The duplicate you can ignore, and the same stringified stats

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV moments_1
  • array
  • literal
binaryImage

moments_1 is the identical twin of moments_0. Same inputs - array (NPARRAY) plus a binaryImage toggle - same single literal output, same underlying cv2.moments call. If you've read the other article, you know everything this node does. The reason both exist is the pack's generator emitting one node per OpenCV overload (MatLike vs UMat) and then collapsing both array types onto the same NPARRAY slot. The numbering is parser residue, not a behavioral difference.

What it's for

cv2.moments computes the spatial, central, and normalized moments of a binary shape: m00 is the area, m10/m00 and m01/m00 give the centroid, and the Hu moments are rotation/scale-invariant shape signatures. In a ComfyUI workflow you reach for it when you've produced a mask and want hard numbers about the blob - how big, where its center is, how it's oriented.

The one thing to remember: the output is a single STRING containing the stringified Moments object (m00=..., mu20=..., ...). It's a read-it-off-the-widget tool, not a source of floats you can wire into math. If you need actual numbers downstream, you'll be parsing text.

Inputs that matter

  • array - single-channel NPARRAY. Grayscale or a binary mask only; 3-channel input throws the img.type() == CV_8UC1 assertion error.
  • binaryImage - set True to treat all non-zero pixels as 1. That's what you want for a mask: exact pixel counts rather than intensity-weighted values.

Installing

Via ComfyUI Manager (search "opencv-comfyui") or:

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

restart, done. opencv-contrib-python is the only real dependency and it's almost certainly already in your environment.

Pipeline

IMAGE → Image2Nparray → moments_1. Image2Nparray does the RGB→BGR flip and only accepts batch_size==1 (use ImageFromBatch for bigger batches). The output is text, not an image, so it ends the chain - don't route it into Nparrays2Image or you'll hit the classic 'NoneType' object has no attribute 'shape' error.

Troubleshooting

  • CV_8UC1 assertion - your input is 3-channel; make it grayscale first.
  • Long string output - that's the Moments object repr, expected.
  • Which variant? _0 or _1, identical. Pick _0 for consistency with online docs.
Categoryimage/OpenCV

Inputs (2)

NameTypeDefaultDescription
arrayNPARRAY
binaryImageBOOLEAN

Outputs (1)

NameTypeDescription
literalSTRING