Nodes/opencv-comfyui/OpenCV EMD_1
ComfyUI Node

OpenCV EMD_1

Earth Mover's Distance, overload two — same metric, same setup cost

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

EMD_1 is the second overload of cv2.EMD - MatLike vs UMat, generated into two identical nodes. Everything below applies to both, and you can use either.

The function is OpenCV's Earth Mover's Distance, a similarity metric between two distributions: think "how much work to move one pile of dirt into the other's shape" rather than "do these two histograms overlap." It's a stronger notion of distance than correlation-based histogram comparison, because it accounts for how far the mass moved - a red image that's shifted slightly toward orange will score closer to the reference than an unrelated color, which is what you want for matching.

Inputs and outputs

  • signature1, signature2 - NPARRAYs in signature format: each row is [weight, feature...], first column mass, remaining columns coordinates.
  • distType - ground distance: 1 (DIST_L1), 2 (DIST_L2), 3 (DIST_C).
  • cost - optional; for DIST_USER (4) with a custom cost matrix. Skip unless you know you need it.
  • flow - optional out-parameter, leave unplugged.

Outputs: float_0 (lower bound), float_1 (the EMD distance - the number you'll actually use), and nparray (flow matrix).

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 model downloads.

Where people bounce off it

EMD is a two-part problem and this node only solves one part. The other part - turning images or histograms into properly weighted signature arrays - has no helper in the pack, so you'll be constructing NPARRAYs yourself. That's real work, and the README's "not every function is useful within ComfyUI without further processing" applies squarely here. If your signatures are already built (or you're comparing simple feature sets by hand), EMD_1 delivers exactly what cv2.EMD promises: a correct, useful distance. If you were hoping to plug two images straight in and get a similarity score, manage your expectations - this is a math primitive, not a convenience node.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
signature1NPARRAY
signature2NPARRAY
distTypeINT
costoptNPARRAY
flowoptNPARRAY

Outputs (3)

NameTypeDescription
float_0FLOAT
float_1FLOAT
nparrayNPARRAY