OpenCV EMD_1
Earth Mover's Distance, overload two — same metric, same setup cost
- signature1
- signature2
- cost
- flow
- float_0
- float_1
- nparray
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; forDIST_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.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| signature1 | NPARRAY | — | |
| signature2 | NPARRAY | — | |
| distType | INT | — | |
| costopt | NPARRAY | — | |
| flowopt | NPARRAY | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| float_0 | FLOAT | — |
| float_1 | FLOAT | — |
| nparray | NPARRAY | — |