Nodes/opencv-comfyui/OpenCV Mahalanobis_0
ComfyUI Node

OpenCV Mahalanobis_0

A statistical distance that outputs a number, not an image

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

Most nodes in this pack touch pixels. This one touches statistics. OpenCV Mahalanobis_0 wraps cv2.Mahalanobis, which computes the Mahalanobis distance between two vectors - and instead of an image it returns a single FLOAT. That makes it one of the pack's rare non-visual nodes, and also one of its most confusing, so let's be blunt about what it is and isn't.

The Mahalanobis distance is "how far apart are these two points, measured in units of the data's own spread." Unlike plain Euclidean distance, it accounts for correlation between dimensions: it divides the difference by the covariance structure, so a big deviation in a direction where the data varies a lot counts less than the same deviation in a tight direction. It's the classic tool for anomaly/outlier detection - you compare a new sample against a distribution and get a score that says "this is weird" without you hand-tuning per-dimension weights.

Where this fits in ComfyUI

Honestly, rarely. It's a research-grade node. Realistic uses:

  • Outlier scoring in a batch - if you can extract per-image statistics (means, covariances) into nparrays, you can score how unusual a frame is relative to the set, e.g. detecting corrupted or wildly off-distribution generated images.
  • Color-space anomaly detection - measure a pixel/region vector against an expected color distribution.
  • Learning/experimenting - the pack author's framing is that these nodes are for "small and quick transformations," and some of them are just educational.

The inputs

  • v1 - first vector, an NPARRAY (shape (1, n) or (n, 1)).
  • v2 - second vector, same shape.
  • icovar - the inverse covariance matrix of the distribution, shape (n, n). This is the part you must compute beforehand - nothing in the pack builds it for you, and feeding a covariance instead of its inverse gives silently wrong results.

Output: float - the distance. It wires into float consumers: conditioners, math nodes, comparison/switch logic.

Install

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

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

Troubleshooting

  • Shape errors - the vectors and icovar must be consistently shaped. Check your array dimensions; this node gives no visual feedback to guide you.
  • Wrong results but no crash - you almost certainly passed the covariance matrix instead of its inverse. Classic.
  • "What do I wire this into?" - the FLOAT output works with any float input downstream. If you don't have a consumer in mind, you don't need this node.

The _1 variant is identical (overload-numbering quirk). Verdict: skip Mahalanobis unless you specifically need distribution-aware distance. It's a faithful, correct wrapper - the curve is that the pack expects you to bring the matrix math to the table.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
v1NPARRAY
v2NPARRAY
icovarNPARRAY

Outputs (1)

NameTypeDescription
floatFLOAT