Nodes/opencv-comfyui/OpenCV insertChannel_0
ComfyUI Node

OpenCV insertChannel_0

Swap one channel into an image

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV insertChannel_0
  • src
  • dst
  • nparray
coi

OpenCV insertChannel_0 wraps cv2.insertChannel, and it does exactly one thing: it takes a single-channel image, jams it into a slot of a multi-channel image, and hands you the result. src supplies the channel, dst is the image being modified, coi is the zero-based channel index you're replacing. That's the whole job - and it's a surprisingly handy primitive once you're doing per-channel processing.

When would you actually want this? The typical case: you processed one channel on its own - a denoised luminance, a brightness-equalized blue channel, a thresholded alpha - and you want to put it back into the image it came from without touching the other two. The pack's extractChannel_0 is the natural partner: pull a channel out, do OpenCV surgery on it, insertChannel_0 it back in. It's the OpenCV equivalent of a channel mixer, and it's part of why this pack is worth keeping around for the day you need to fiddle with a single color plane.

How it works

Under the hood the node calls cv2.insertChannel(src, dst, coi). OpenCV copies src's channel data into dst's channel coi - and note that in OpenCV this modifies dst in place. The pack's generator handles out-parameters by exposing them as inputs and returning them anyway, but this particular one was typed without the | None escape hatch, so dst shows up as a required NPARRAY input, not an optional. That means you have to supply the target image even though you're about to overwrite one of its channels - a quirk worth knowing before you wonder why dst won't disconnect.

You also need a real single-channel src: the pack converts everything to/from BGR uint8 nparrays, so a "single channel" here means (H, W) or (H, W, 1), and coi indexes into the BGR planes - 0 is blue, 1 is green, 2 is red in BGR order. Get the order wrong and your colors silently swap.

Inputs and outputs

  • src - the single-channel image to copy from (NPARRAY).
  • dst - the multi-channel image to modify (NPARRAY, required - remember the quirk above).
  • coi - integer, 0-based channel index in dst to replace.
  • Output nparray - dst with the channel swapped.

Wire src and dst from Image2Nparray conversions, and send the output through Nparrays2Image when you want to preview or continue in normal Comfy land. Keep batch_size == 1 - the pack only supports single images, so use ImageFromBatch (length=1) if your image comes in a batch.

Installing

insertChannel_0 lives in opencv-comfyui, the auto-generated OpenCV pack. Install once, get all ~635 nodes:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
cd opencv-comfyui
pip install -r requirements.txt

Dependencies are just opencv-contrib-python, numpy, torch - no models to download. ComfyUI Manager users can search "opencv-comfyui" instead of cloning.

Gotchas

The dst-modifies-in-place behavior is the thing that bites most. Since the node returns the modified array, always take the output socket - don't assume the dst you wired in will somehow be updated back up the graph (it won't; ComfyUI is a DAG, values flow forward). And if src and dst don't match in size or channel count, expect an OpenCV assertion error rather than a friendly message - that's the nature of these auto-generated wrappers. Finally, remember cv2.insertChannel expects src to be single-channel; a full-color src will throw.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY
dstNPARRAY
coiINT

Outputs (1)

NameTypeDescription
nparrayNPARRAY