Nodes/ComfyUI CV/cv2.ximgproc.PeiLinNormalization
ComfyUI Node

cv2.ximgproc.PeiLinNormalization

The 'make this shape canonical' maths node

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.ximgproc.PeiLinNormalization
  • I
  • nparray

This one is a maths node wearing an image node's clothes. You give it a single-channel image; it gives you a 2×3 affine matrix - no pixels, no transform applied, just the matrix that would normalise what you just handed it. One input, one output, zero parameters. It's the smallest node in this batch and the one whose purpose is hardest to guess from the name, so: it computes the second-order moment normalisation from Pei and Lin's 1995 work, i.e. the transform that moves the shape's centroid to the origin and makes its covariance matrix the identity. Translation, rotation and shear/anisotropic scale get flattened in one go.

Why you'd care: this is the missing step in front of any shape-comparison pipeline. Fourier descriptors and moment matching are invariant in theory, but the descriptors you actually get from a sloppily cropped photo carry the crop's offset, rotation and stretch. Normalise first, then compare, and suddenly two photos of the same printed symbol - one tilted, one slightly squashed by lens perspective - land on top of each other.

The author's own tooltip says it in one line: "Returns the 2x3 affine transform that maps the shape to its canonical (Pei-Lin normalized) pose - feed it to cv2.warpAffine to undo skew/rotation/scale."

Inputs and outputs

  • I - a single-channel image. Not a colour photo: greyscale it first, or better, binarise it (the mask of the object you care about) so the moments are computed over the shape rather than over the lighting.
  • nparray output - the 2×3 affine, as an NPARRAY. Do not send this to CV Array → Image; it's six numbers, not a picture. Use Inspect CV Data to read it, then wire it into the M input of the generated cv2.warpAffine wrapper to actually apply it - that wrapper wants src, M and a dsize (the size-typed output dimensions, authored as a (width, height) literal).

What it isn't

It has no idea what your object is. It computes moments over the whole frame you give it, so a photo with three objects normalises the mixture, and a photo with a bright background normalises the background. Crop to the shape (or pass its mask) or you'll get a mathematically correct transform of entirely the wrong thing.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv

Restart ComfyUI, or install ComfyUI CV from ComfyUI Manager. Python ≥3.12, recent ComfyUI on the V3 node API, opencv-contrib-python-headless~=5.0.0.93. Nothing to download. It's a contrib function, so a plain opencv-python install sitting on top of the contrib one hides this node from the menu; tools/repair_opencv_contrib.py --check reports that, --apply repairs it.

Common issues

"The output is not an image." Correct - it's a transform. Every consumer needs to be a geometry node, not a display node.

Nonsense matrix. Almost always a multi-channel or near-uniform input. An empty or flat image has a singular covariance and the normalisation degenerates. Binarise, crop, then normalise.

Rotation still doesn't match. Moment normalisation fixes the second-order pose; it can't distinguish a shape from its 180° rotation or its mirror, because both have identical covariance. If your comparison needs chirality, measure it separately - the pack's shape-moments node reports signed chirality and canonical 0–360 orientation for exactly this reason.

Expect a dead end in search results. This is one of the ~470 auto-generated raw cv2.* wrappers in the pack, and OpenCV's own documentation for PeiLinNormalization is a single paragraph. The README says the generated wrappers are uncurated and the pack isn't production-ready without your own review; for this node that's less a disclaimer and more a description of the situation.

Categoryimage/CV/low-level/ximgproc

Inputs (1)

NameTypeDefaultDescription
INPARRAY,IMAGE,MASK - - - 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
nparrayNPARRAY—