cv2.randShuffle
Shuffle a list of points or indices, not a picture
- dst
- result
This is the least glamorous node in the pack and that's fine: it shuffles a numerical array in place, which is exactly what you need before you split a point set into train and test halves, or before you sample a set of correspondences without accidentally keeping them in spatial order.
What it does
cv2.randShuffle(dst, iterFactor) performs random swaps across the elements of an array. iterFactor is the number of swap passes as a multiple of the element count - the default 1.0 means one swap per element, which is plenty for a decent shuffle. Lower it for a partial scramble, raise it if you're superstitious.
OpenCV describes the expected input as a numerical 1D array, and that's the honest framing of what this node is for: point lists, index vectors, label arrays, feature rows. It shuffles elements, so if you hand it an image you're shuffling pixels along one axis, which is not a visual effect anybody wants. Reach for it in data preparation, not in the render path.
The RNG caveat
Like the other random functions in this pack, randShuffle draws from cv2's one global RNG. That has a consequence you will notice the second time you run a graph: what comes out depends on what else drew from the generator, and ComfyUI's execution order follows the graph's links, not the node's position. The pack ships CV Random Seed for this - it seeds the global RNG and passes a value through unchanged, so inserting it on the wire immediately upstream of the shuffle is what makes a seed reproducible. Wiring the seed node into nothing doesn't work; it has to sit in the data path to be guaranteed to run first.
Inputs and outputs
dst is the array. It accepts an IMAGE, MASK or NPARRAY link - in practice you'll wire an NPARRAY from a points or matrix node: CV Random Points for a synthetic cloud, the output of a feature matcher, or a parsed literal. The wrapper passes cv2 a private copy, so the upstream array is left alone; the shuffled result comes out of the node.
Optional iterFactor (float, default 1.0) as above.
The output is result, and it echoes the input's format - NPARRAY in, NPARRAY out; if you fed it an IMAGE batch you get an IMAGE batch back (it's in the pack's per-frame set, so each frame is shuffled independently). Downstream that's typically CV Stack Feature Classes, a training node, or a splitter that cuts the array at a fixed row.
One thing worth internalising: shuffling pairs two arrays together only if you shuffle them the same way. cv2 shuffles one array with its own internal RNG state, so shuffling points and their labels in two separate nodes gives you pairs that no longer correspond. Shuffle an index vector once, then use that vector to reorder both arrays - or keep the pairs in one array to begin with.
Install
From ComfyUI CV by bmad4ever - ~470 auto-generated cv2.* wrappers plus curated nodes. ComfyUI Manager, search comfyui_cv, or:
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"
Restart afterwards. Python ≥ 3.12 and a recent ComfyUI on the V3 node API. No models.
Common issues
"Nothing changes between runs." Expected - without a seed on the wire, the global RNG starts from a fixed state and you get the same shuffle every time. If you want variation, that's a different problem (cv2's global RNG isn't reseeded per run); the pack's seed node is the supported way to control it, and a fresh state means changing the seed.
"My labels no longer match my points." Two independent shuffles, as described above.
"The output looks like static." You shuffled an image. Wrong tool - this one is for arrays of data.
OpenCV complains about dimensions. It wants a numeric array it can treat as a sequence; a ragged or object-dtype array (a list of variable-length point lists, say) won't work. Flatten or pad first.
Missing nodes from the pack generally. If several of these raw wrappers are absent, check that you're on the contrib OpenCV wheel - tools/repair_opencv_contrib.py --check, then --apply.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| dst | COMFY_MATCHTYPE_V3 | input/output numerical 1D array. The image output(s) echo this input's format. The low-level cv2 function writes its result into this array in place, but this wrapper passes cv2 a private copy, so your input array is never modified. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size. | |
| iterFactoropt | FLOAT | 1.0000-1e+38–1e+38 | scale factor that determines the number of random swap operations (see the details below). Preset to the OpenCV default (1.0). |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| result | COMFY_MATCHTYPE_V3 | Echoes the 'dst' input's format: an IMAGE link comes back as IMAGE, MASK as MASK, NPARRAY stays NPARRAY. |