Nodes/ComfyUI CV/cv2.norm (1/2)
ComfyUI Node

cv2.norm (1/2)

How big is this array, as one number

By bmad4ever·Created 4 months ago·Updated 14 days ago· 1
cv2.norm (1/2)
  • src1
  • mask
  • float
◄normTypeNORM_L2►

cv2.norm in OpenCV is two functions wearing one name: the norm of a single array, and the norm of the difference between two arrays. The pack splits the overloads into two nodes - this one, displayed as cv2.norm (1/2), is the single-array form, and it returns one FLOAT.

What is that number good for? "How much energy is in this array" is a real question in a CV graph. It's how you check whether a mask is nearly empty before you spend a diffusion pass on it, how you compare the level of two processing paths without looking at them, how you turn a whole image into a scalar you can branch on, and how you size a normalizing divisor for a downstream operation.

The norm types, and which one you want

normType is a dropdown of the NORM_* family, defaulting to NORM_L2:

  • NORM_L2 - √(Σ x²), the Euclidean magnitude. The default and usually the right answer.
  • NORM_L1 - Σ |x|, the sum of absolute values. Less swayed by a few large pixels than L2, and cheap.
  • NORM_INF - max |x|. The peak, not the total. If you want to know "is anything in here clipping", this is it.
  • NORM_L2SQR - Σ x² with no square root. Same ordering as L2, one less sqrt; use it when you're only comparing.
  • NORM_HAMMING / NORM_HAMMING2 - bit-count distances for packed binary descriptors. Meaningless on ordinary pixel data.

The one trap in that list: the pack reuses the same NORM_* dropdown for cv2.normalize's norm_type, so the list you see contains entries that only make sense on one of the two nodes. NORM_MINMAX is a normalize thing, not a norm thing.

Inputs and the output

src1 is required and accepts an IMAGE, a MASK or an NPARRAY. The optional mask (same size as src1, CV_8U/CV_8S/CV_Bool) restricts what's summed - which is how you ask "what's the L2 energy inside this region" rather than across the whole frame.

The output is a single FLOAT on the float socket. Wire it into anything that takes a FLOAT: a note or text node to log it, a comparison to branch on, or the pack's ratio-ish arithmetic if you're normalizing by hand.

Two behaviours to internalize. First, it pools the whole array - a 3-channel IMAGE gives you one number with R, G and B mixed together. If you care about one channel's level, convert first (Image → CV Array plus cv2_extractChannel) rather than reading a pooled figure. Second, the number is scale-dependent and dtype-dependent: an image that's 0–1 float and the same image as 0–255 uint8 differ by a factor of 255 in L1 and 255² in L2SQR. Comparing two norms is only meaningful when both sides live in the same depth and range; CV Cast Array is how you force that.

Install

ComfyUI Manager → search ComfyUI CV → install → restart, or:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"

Python ≥ 3.12 and a recent ComfyUI on the V3 node API. Core cv2: no contrib submodule, no model files, nothing to download.

Practical notes

  • Use it as a gate, not a gauge. The absolute value means little; the value relative to your own earlier runs means a lot. Log a few, pick a threshold, then automate.
  • Pair it with cv2_norm_1 when you want a difference metric instead of a magnitude - that's the two-array overload, and for "how different are these two images" it's the one you want.
  • Batch framing applies: an IMAGE link is read as frame 0, so per-frame magnitudes mean looping over the batch with the pack's batch bridges.
  • If cv2_norm_1 is in your menu but this one isn't, something is badly wrong with your install - they're two variants of the same generated wrapper and always appear together. A missing pair means the installed cv2 doesn't expose norm at all; CV Build Information will tell you what build you actually have.
Categoryimage/CV/low-level/cv2 N

Inputs (3)

NameTypeDefaultDescription
src1NPARRAY,IMAGE,MASKfirst input array. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size.
normTypeoptCOMBONORM_L2type of the norm (see #NormTypes).
maskoptNPARRAY,IMAGE,MASKoptional operation mask; it must have the same size as src1 and type CV_8UC1, CV_8SC1 or CV_BoolC1. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size.

Outputs (1)

NameTypeDescription
floatFLOAT—