OpenCV batchDistance_0
BatchDistance_0 — the nearest-neighbor distance node nobody expects
- src1
- src2
- dist
- nidx
- mask
- nparray_0
- nparray_1
Okay, this one is weird, and that's not a criticism - it's the most honest label for the node. batchDistance_0 wraps cv2.batchDistance, which computes distances between points in two sets: for every point in src1, find the K nearest neighbors in src2 and report both the distances and the indices of those neighbors. It's the math primitive behind nearest-neighbor matching and k-NN classification, exposed as a ComfyUI node because this pack auto-generates a wrapper for (almost) every top-level OpenCV function.
If that description makes your eyes glaze over, that's fair. This is not a node for casual image tweaking. It's for people doing feature matching, point-cloud work, or descriptor comparison inside a graph - and it's the kind of node that exists so you don't have to drop out of ComfyUI and write Python. The author's README says it plainly: not every function is useful inside ComfyUI "without further processing." This is one of those.
The inputs (this is where the dragons live)
- src1 / src2 - two
NPARRAYs of points (or descriptors), one row per point/descriptor. - dtype - the output array type as an OpenCV type int.
5isCV_32F(32-bit float), the usual choice for distances. - normType - the distance norm.
4isNORM_L2(Euclidean), the everyday default. - K - how many nearest neighbors to return per query point.
- update - an int flag; when nonzero, the node updates existing
dist/nidxarrays instead of allocating new ones. Leave it0unless you know why you're setting it. - crosscheck -
Trueto keep only mutual nearest neighbors (point A's closest is B and B's closest is A). This is the standard filter for reliable feature matching. - mask (optional) - restricts which pairs are considered.
- dist / nidx (optional) - OpenCV's out-parameters. Leave unwired; the node allocates and returns them.
- Outputs: nparray_0 (distances, one
K-column row per query point) and nparray_1 (the corresponding neighbor indices).
The honest reality check
Three of those inputs are integer constants you'll have to look up (that's what the OpenCV docs are for), the outputs are raw arrays you then need to do something with, and nothing here is an image. Nparrays2Image will hit the README's 'NoneType' object has no attribute 'shape' if you feed it a distance table, because it's not a picture. This node rewards people who already understand k-NN; for everyone else, it's mostly a way to discover what batchDistance is for. If you just want to compare two images, look at compareHist or absdiff instead - they're far friendlier.
Install
Part of opencv-comfyui, same as all its siblings:
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-python-contrib
or via ComfyUI Manager (search "opencv-comfyui"), then restart. No model downloads. The pack's known install trap is conflicting OpenCV builds (Cannot import name 'guidedFilter' from 'cv2.ximgproc'); a clean reinstall fixes it.
And yes - batchDistance_1 is the identical twin, generated from the UMat overload. Same function, same behavior. Pick one.
Inputs (10)
| Name | Type | Default | Description |
|---|---|---|---|
| src1 | NPARRAY | — | |
| src2 | NPARRAY | — | |
| dtype | INT | — | |
| normType | INT | — | |
| K | INT | — | |
| update | INT | — | |
| crosscheck | BOOLEAN | — | |
| distopt | NPARRAY | — | |
| nidxopt | NPARRAY | — | |
| maskopt | NPARRAY | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| nparray_0 | NPARRAY | — |
| nparray_1 | NPARRAY | — |