Nodes/opencv-comfyui/OpenCV HuMoments_0
ComfyUI Node

OpenCV HuMoments_0

Seven numbers that describe a shape no matter how you flip it

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV HuMoments_0
  • hu
  • nparray
m

HuMoments_0 computes the seven Hu moments of a shape - a set of numbers that describe a shape's geometry in a way that's invariant to translation, scaling, and rotation. It's a classic computer-vision tool for shape recognition: the same object viewed from different sizes or angles produces (roughly) the same seven values, so you can compare shapes by comparing vectors instead of pixels. It's niche, it's old-school, and it has a genuinely weird wiring in this pack that you should understand before you touch it.

How it works - and the wiring gotcha

Under the hood it's cv2.HuMoments(m), where m is a Moments object - the thing cv2.moments() returns, which bundles raw image moments like m00, m10, m01 etc. Hu moments are normalized combinations of those raw moments, which is what gives them the translation/scale/rotation invariance.

Now the pack-specific weirdness: this auto-generated node types m as a STRING. Composite objects like Moments can't be typed as a Python literal by hand, so the generator's convention is to pass them around as strings that get ast.literal_eval()'d. The intended pairing is moments_0 → this node: moments_0 computes the raw moments from your image and outputs them as a STRING "literal", which HuMoments_0 then parses back. If you try to type a Moments value into the m field yourself, you're fighting the generator. Feed it the output of moments_0 (which takes your nparray plus a binaryImage boolean).

The optional hu input is the generator's out-parameter for the result; leave it disconnected and use the nparray output - a 7×1 array, one value per Hu moment.

What you'd use it for

Shape classification is the honest use case: threshold a mask into a silhouette, compute moments, and you have a compact fingerprint you can compare against known shapes or cluster. The catch in ComfyUI: comparing the 7-vectors means math nodes downstream, and this pack doesn't hand you a ready-made "compare two fingerprints" node. So it's really for people already building CV-pipeline-style workflows, not for casual use.

Install

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

or "OpenCV" via ComfyUI Manager, plus pip install opencv-contrib-python. No model files.

Gotchas

The string-literal wiring is the trap - HuMoments_0 in isolation looks like it should take a number, and it takes a parsed Moments object instead. Pair it with the pack's moments_0 node and don't hand-type m. Also, Hu moments are only invariant if the shape is normalized properly - a barely-segmented mask gives garbage fingerprints. If your outputs look like noise, check the segmentation, not the node. This one is a "dragons" node; expect to read the OpenCV docs alongside it.

Categoryimage/OpenCV

Inputs (2)

NameTypeDefaultDescription
mSTRING
huoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY