Nodes/opencv-comfyui/OpenCV EMD_0
ComfyUI Node

OpenCV EMD_0

A real similarity metric, with setup work

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV EMD_0
  • signature1
  • signature2
  • cost
  • flow
  • float_0
  • float_1
  • nparray
distType

EMD_0 wraps cv2.EMD, the Earth Mover's Distance - the metric that treats two distributions as piles of dirt and measures how much work it takes to shovel one into the other. It's a genuinely better similarity measure than a raw histogram correlation when you care about how far one distribution has shifted, not just whether it matches. In ComfyUI terms: if you're building image-similarity, reference-matching, or color-histogram comparison logic, this is one of the few real "distance" primitives the pack exposes.

How it works and the inputs

EMD compares two "signatures": arrays where each row is [weight, feature_1, feature_2, ...] - the first column is the mass, the rest are the coordinates of that mass in your feature space. For color comparison you'd build signatures from histogram bins (e.g. weight = bin count, features = color values) via other nodes, then compare. The inputs:

  • signature1, signature2 - the two signatures as NPARRAYs.
  • distType - the ground distance between features: 1 (DIST_L1), 2 (DIST_L2), or 3 (DIST_C). L1 is cheap and common; L2 is the default intuition for spatial distance.
  • cost - optional; only relevant if you're using DIST_USER (4) with a custom cost matrix. You almost certainly aren't.
  • flow - optional out-parameter. Skip it.

Outputs: float_0 (the lower bound of the distance), float_1 (the actual EMD distance), and nparray (the flow matrix showing how mass moved). For most uses, float_1 is the number you wire into a comparison or threshold.

Install

opencv-comfyui (geroldmeisinger). ComfyUI Manager → search OpenCV, or:

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

Restart. requirements.txt: opencv-contrib-python, numpy, torch. No downloads. _0/_1 are the usual identical overloads.

The honest cost

Here's the thing nobody tells you: EMD is only as good as the signatures you feed it, and building signatures is the actual work. This node is a clean wrapper of a real algorithm, but there's no helper in the pack that turns a histogram into a signature array for you - the README is upfront that this is a library of raw functions, not conveniences. If you need signature construction, you'll be composing calcHist, array reshaping, and literals yourself, and at that point you may be faster writing a small custom Python node. But if you've already got the signatures, EMD_0 is a solid, correct implementation of a legitimately useful metric.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
signature1NPARRAY
signature2NPARRAY
distTypeINT
costoptNPARRAY
flowoptNPARRAY

Outputs (3)

NameTypeDescription
float_0FLOAT
float_1FLOAT
nparrayNPARRAY