Nodes/opencv-comfyui/OpenCV norm_2
ComfyUI Node

OpenCV norm_2

How different are two images? The L2/L1 distance, as one number

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV norm_2
  • src1
  • src2
  • mask
  • float
normType

norm_2 wraps cv2.norm(src1, src2, normType, mask) - the two-array overload that measures the distance between two arrays: norm(src1 - src2). Put two images in, get one number out that says how different they are. It's the closest thing this pack has to a "similarity score", and it's genuinely the most useful of the four norm variants.

What it computes

Behind the scenes OpenCV subtracts src2 from src1 and takes the norm of the difference. With normType:

  • 4 = L2: Euclidean distance - the standard "how different" measure, squared-and-rooted.
  • 2 = L1: Manhattan distance - sum of absolute differences; a bit more forgiving of a single large outlier.
  • 1 = INF: the single biggest pixel difference - "the worst-case error".

So you can read it as: L2 for overall difference, L1 for total deviation, INF for worst-pixel error. For comparing images, L2 is the one people mean by "difference".

Why you'd reach for it

Concrete workflow uses:

  • Frame comparison. Compare frame N to frame N+1 (grab frames from a batch with ImageFromBatch) - the distance spikes at cuts/scene changes. Feed the float to a threshold and you've got a shot-detector.
  • Output vs reference. After an edit or an img2img pass, measure how far the result drifted from the input.
  • Denoise/blur sanity check. A "denoised" image that's identical to its input scores zero - you'd know your node chain isn't running.

Both inputs must be the same size and dtype (grayscale is typical). The optional mask restricts the comparison to a region - useful if only part of the image should matter.

Inputs and output

  • src1, src2 (NPARRAY) - the two arrays to compare.
  • normType (INT) - 4 L2, 2 L1, 1 INF.
  • mask (optional NPARRAY).
  • Output: float.

The number is only meaningful relative - a "difference" of 5 on a 1024×1024 grayscale image is tiny; the same value on an 64×64 crop is huge. Use it for A/B comparisons within one pipeline, not as an absolute quality metric.

Installing

Part of geroldmeisinger/opencv-comfyui. ComfyUI Manager → "opencv-comfyui", or:

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

restart. Dependency: opencv-contrib-python.

Troubleshooting

  • Size/dtype mismatch - both inputs must match; convert to grayscale (cvtColor code 6) and same resolution first.
  • "Same image gives 0" - correct, that's the point.
  • Which variant - norm_2 and norm_3 are identical duplicates (overload residue); use _2. Don't confuse them with norm_0/norm_1, which take one array.
Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
src1NPARRAY
src2NPARRAY
normTypeINT
maskoptNPARRAY

Outputs (1)

NameTypeDescription
floatFLOAT