Nodes/opencv-comfyui/OpenCV Mahalanobis_1
ComfyUI Node

OpenCV Mahalanobis_1

OpenCV Mahalanobis_1 — the same statistical-distance node, minus the duplicate confusion

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV Mahalanobis_1
  • v1
  • v2
  • icovar
  • float

Same story as every _0/_1 pair in this pack: Mahalanobis_1 and Mahalanobis_0 are identical wrappers. OpenCV's type stubs declare cv2.Mahalanobis twice - once for MatLike, once for OpenCL UMat - and the auto-generator numbered the overloads rather than merging them. Neither variant is faster or different; there's no GPU path behind the _1. Read this as the manual for cv2.Mahalanobis and use whichever twin is closer.

What the function computes: the Mahalanobis distance between two vectors - the distance measured in the "shape" of a distribution's covariance, not in raw Euclidean units. If two points differ by the same amount but in a direction where the data varies a lot, that difference counts as smaller than one in a tight direction. That correlation-aware property is exactly why it's the standard score for outlier/anomaly detection: "how unlikely is this sample given everything I've seen."

What you get and what you need to supply

Three NPARRAY inputs, one FLOAT output - this node doesn't produce an image, which surprises people the first time.

  • v1, v2 - the two vectors to compare.
  • icovar - the inverse of the covariance matrix for your distribution. This is the whole game: you compute the covariance, invert it, and feed the inverse. Feed the non-inverted covariance and you'll get silently wrong numbers.

The output is a single float, so downstream you're in float-land: thresholds, comparison nodes, conditioners, or just a number to stare at.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python

Restart, or ComfyUI Manager → "opencv". Requirements: opencv-contrib-python, numpy, torch.

Troubleshooting

  • Shape errors with no guidance - this node gives no visual feedback. Verify v1, v2, and icovar dimensions line up (n-vectors with an n×n inverse-covariance).
  • Plausible-looking but wrong floats - almost always the inverse-covariance mistake above.
  • Batch error - wait, there isn't one here; Mahalanobis takes vectors, not batch images. The pack's batch_size==1 rule doesn't apply.

Honest verdict: this is the most "for math people only" node in the batch. If you're scoring how anomalous a generated frame is relative to a distribution, it's the right primitive. If you're not doing distribution-aware statistics, you will not need it - and that's fine. The pack wraps 600+ cv2 functions precisely so the one you do need is there.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
v1NPARRAY
v2NPARRAY
icovarNPARRAY

Outputs (1)

NameTypeDescription
floatFLOAT