Nodes/ComfyUI CV/CV Stack Batch
ComfyUI Node

CV Stack Batch

Turning a list of arrays into one batched array

By bmad4ever·Created 4 months ago·Updated 16 days ago· 1
CV Stack Batch
  • arrays
  • batch

ComfyUI has two ways of representing "several things": a list, which makes a node execute once per item, and a batch, which is one array with a leading dimension and runs downstream once. Both are legitimate, they look identical on the canvas, and moving between them is the step people get wrong.

CV Stack Batch is the list-to-batch direction, for NPARRAY data. It's the documented inverse of CV Unstack Batch, so the pair gives you a round trip.

How it works

It takes a list of arrays and merges them along a new first axis. N arrays of (H, W, C) become one (N, H, W, C) array, where N is the list length. That's the whole transformation - one np.stack with axis=0 and a contiguity pass.

Two things about the list input are worth understanding, because they're the reason a node with this little logic exists at all:

It's a list input, not a batch input. The arrays socket is declared as an input list, which is what lets you wire a list output from another node straight in - most obviously the frame list from CV Unstack Batch - and have the node receive all of them as one call instead of running once per element. On the canvas that distinction is invisible; in execution it's the difference between one stacked array and N separate arrays.

Every array must have identical shape, and the same number of dimensions. Not broadcastable - identical. Different shapes raise a ValueError that names item 0's shape and the offending item's shape, which is more helpful than numpy's own error and worth reading rather than skimming. No silent broadcasting happens here: a (4,4) and a (4,4,1) will not be quietly reconciled.

One input, one output: batch, the stacked array. An empty list gives you an empty float32 array rather than an error, in keeping with the pack's habit of returning a valid empty result instead of killing the run.

Where it goes

Anywhere a single array is more useful than a list. Stack a loop's worth of processed frames before a downstream node that expects one batch - the per-frame vs whole-batch question that also shows up in the pack's crop policy modes. Or reassemble what CV Unstack Batch took apart, so you can do list-shaped work in the middle of a batch-shaped graph: unstack, process per item, stack, continue.

One caution that's specific to this pack's own design: CV Stack Batch only adds a new leading axis. It does not concatenate along an existing one. If what you want is "append these two (N,4) detection tables into an (N+M,4) table", that's CV Concat Arrays, and the author's own comment says why it exists - cv2.hconcat/vconcat/merge/split take a vector<Mat>, which the generator can't express, so none of them made it into the auto-generated wrapper set.

Install

ComfyUI Manager → ComfyUI CV → install → restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"

Python ≥ 3.12, a recent ComfyUI on the V3 node API, and a numpy-containing environment (numpy is a declared dependency; torch comes along with ComfyUI anyway). No models. It lives under image/CV/low-level, alongside ~470 auto-generated cv2.* wrappers.

Common issues

  • "Cannot stack arrays of different shapes". Read the two shapes in the message. Usually one frame in the list went through a resize or a crop that the others didn't. Fix it upstream - padding everything to a common size is the usual answer, and the crop policy nodes are one place that can enforce it.
  • It ran once per list item instead of once on the whole list. You wired a batch where the node expected a list, or vice versa. Check whether the upstream output is a list output (the socket renders differently for lists in most frontends) - the node needs the list form.
  • You wanted concatenation, not stacking. CV Concat Arrays.
  • Everything CV-related vanished from the menu. A non-contrib OpenCV wheel was installed over the contrib one; all four OpenCV distributions share one site-packages/cv2, so contrib submodules empty out. python tools/repair_opencv_contrib.py --check, then --apply.
Categoryimage/CV/low-level

Inputs (1)

NameTypeDefaultDescription
arraysNPARRAYList of NPARRAYs, one per batch element, e.g. the 'frame' list output of 'CV Unstack Batch'. All arrays must share the same shape.

Outputs (1)

NameTypeDescription
batchNPARRAYBatched array (N, ...): the first axis is the list length, each element along it is one input array.