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

cv2.norm (2/2)

The difference metric for two arrays

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

cv2.norm (2/2) is the two-array overload: give it src1 and src2 and you get back one FLOAT on the float socket, describing how far apart they are. This is the node you reach for when you need a number for "did this change", "which of these candidates is closest", or "is this pass actually doing anything".

It shows up in three recurring jobs. Change detection on video - norm the difference between consecutive frames and you have a scalar motion level before you've paid for anything expensive; the pack's background-subtraction and motion-segmentation subgraphs are the neighbours that do it in 2D rather than in one number. Comparing passes or models - render two variants, norm them, and you know whether the second upscale/deblur/grain pass altered anything material. And candidate ranking - a batch of attempts against a reference, sorted by how far off each is. The KB's upscaling doc notes how easy it is to ship a pass that looks different at 100% and identical at viewing size; a difference norm is how you catch that mechanically instead of squinting.

Mechanics and the choice of norm

The value is the norm of src1 - src2, computed under whichever normType you pick. The default, NORM_L2, gives you √(Σ d²) - an RMS-like figure, dominated by the largest disagreements. NORM_L1 sums |d| and is more forgiving of a single blown pixel. NORM_INF reports the single largest difference anywhere in the pair, which is the strictest "is any pixel off" reading. NORM_L2SQR is the squared version of the default, fine for ranking and slightly cheaper.

The NORM_RELATIVE mode deserves a mention because it's the one people reach for without understanding it: it's a ratio, so it tells you how big the difference is relative to the second array's own magnitude. That makes it useful for comparing images of different exposure or scale where an absolute difference would be meaningless - and useless if you were expecting the same units as the other modes.

src1 and src2 both accept IMAGE, MASK or NPARRAY - note the type is the plain three-way one, not the format-echoing kind, because there is no output image to speak of. The optional mask restricts the comparison to a region: same size as src1, CV_8U/CV_8S/CV_Bool.

Gotchas that actually cost time

  • Two things that must match: size and type. src1 and src2 have to be the same shape and the same depth, so an 0–1 float tensor compared against an 0–255 uint8 buffer is a nonsense comparison (and a good way to get a silent number that's just the scale difference). Cast both with CV Cast Array first.
  • Pooled across channels. A colour IMAGE gives you one number over R, G and B together. For a per-channel answer, split or grayscale.
  • The absolute value is arbitrary. There's no canonical threshold - "3000" means nothing until you've measured a few pairs you consider identical and a few you consider different. Calibrate on your own data; that's the whole method.
  • Batch framing. An IMAGE link is read as frame 0, so a video-timeline use means looping frames with the batch bridges rather than hoping a batch gives you per-frame numbers.
  • It's a metric, not a decision. The node hands you the number; the branching, logging and cutoffs are yours to wire. If you want a ready-made scoring pipeline, the pack's curated quality-metric subgraph (CV Quality Metrics in the examples) is the place to look - this is the primitive underneath.

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, ComfyUI on the V3 node API. Core cv2 - no contrib submodule, no models, and both cv2.norm variants come from the same generated wrapper, so if one exists so does the other.

Categoryimage/CV/low-level/cv2 N

Inputs (4)

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.
src2NPARRAY,IMAGE,MASKsecond input array of the same size and the same type as src1. 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—