Nodes/ComfyUI CV/cv2.extractChannel
ComfyUI Node

cv2.extractChannel

Pull one channel out, and the BGR/RGB trap waiting in it

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.extractChannel
  • src
  • nparray
◄coi0►

Channel surgery is one of those things you never need until a specific effect or a specific mask does, and then you need it precisely. cv2.extractChannel hands you one channel of an array as a standalone array - cheap, deterministic, and the cleanest way to answer "what does the red channel alone look like" without splitting the whole thing into three nodes.

How the index works, and the trap

coi is the channel index, and it is zero-based against the array that reaches OpenCV, not against what you see in ComfyUI's preview. That last part is the whole gotcha. ComfyUI images are RGB; this pack converts them to OpenCV's BGR ordering when it bridges them (Image → CV Array is explicit about it, and its default is BGR). So on an IMAGE link, coi 0 is blue, 1 is green, 2 is red. Get this backwards and your channel-based mask or glitch effect is wrong in a way that looks almost right, which is the worst kind of wrong.

If you want to be certain, wire the IMAGE through Image → CV Array with color_format set to RGB yourself. Then coi 0 is red, and you have a piece of paper in the graph saying so.

coi is required, defaults to 0, and an out-of-range index raises rather than wrapping.

Input and output

src accepts an IMAGE, MASK or NPARRAY. The output is a plain NPARRAY named nparray - always, even when you fed it an IMAGE, because a single channel isn't the same nature as the picture you handed in and the pack deliberately doesn't fake a format echo here. That means the practical chain almost always has a bridge at one or both ends: Image → CV Array in, and CV Array → Image out if you want to look at it. CV Array → Image will treat a single-channel array as a gray picture, which is what you want for inspection.

Where it fits

Three jobs, all real:

Isolating a channel as a mask. Luminance-ish separations, "the blue channel is basically a haze map on this shot" screening, and the classic trick of using one channel's contrast instead of a grayscale conversion. Feed the result to a threshold node and you have a mask from a channel rather than from a hand-painted region.

Per-channel effects. Chromatic aberration, channel-shift glitch, per-channel curves. Since the pack has no cv2.merge wrapper (its notes say CV Concat Arrays fills that gap - "replaces the absent cv2.merge wrapper"), the intended pattern is: extract, process each channel separately, then CV Concat Arrays them back along the channel axis. That's more nodes than a dedicated glitch pack, but it's honest, and it works on raw arrays where a lot of the friendlier packs assume IMAGE tensors.

Feeding a single-channel requirement. Anything downstream that insists on one channel - plenty of cv2 functions do, and the pack auto-converts in some cases but not all - is best served by an explicit extraction, because then your graph documents that the conversion is intentional.

Batch behaviour: channel extraction is on the pack's frame-batch-safe list, so an IMAGE batch gets processed frame by frame rather than only frame 0, which is what you want for video work.

Installing it

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

Manager users: search comfyui_cv and restart afterwards. Python ≥ 3.12, V3-API ComfyUI. The node sits under image/CV/low-level/cv2 E.

Traps

Ordering, as covered - write down which convention each end of your chain uses, because the pack bridges to BGR by default and ComfyUI shows RGB. Alpha is channel 3 if the array has four channels, and an IMAGE tensor in ComfyUI is 3-channel, so don't expect the fourth to exist unless you built the array yourself. And remember this is a selection, not a conversion: nothing is normalized, stretched or gamma-corrected. If the extracted channel looks flat, that's the channel, not a bug - that's what contrast nodes are for.

Categoryimage/CV/low-level/cv2 E

Inputs (2)

NameTypeDefaultDescription
srcNPARRAY,IMAGE,MASKinput array 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.
coiINT0-2147483648–2147483647index of channel to extract

Outputs (1)

NameTypeDescription
nparrayNPARRAY—