Nodes/opencv-comfyui/OpenCV decolor_0
ComfyUI Node

OpenCV decolor_0

The Grayscale Conversion That Respects Color Contrast

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV decolor_0
  • src
  • grayscale
  • color_boost
  • nparray_0
  • nparray_1

OpenCV decolor_0 is the "decolorization" algorithm - OpenCV's contrast-preserving color-to-grayscale conversion, plus a color-boosted companion. If you've ever converted a color image to grayscale and watched two distinct colors collapse into the same gray, you know the problem it solves: plain luminance averaging (cvtColor with code 6) can destroy the very contrast you needed to see. decolor optimizes the gray mapping so that color differences stay visible as tonal differences.

It's from geroldmeisinger/opencv-comfyui, the auto-generated pack of ~635 cv2 wrappers. It wraps cv2.decolor(src, grayscale, color_boost), and here's the interesting part: that function returns two images, so this node has two outputs.

How it works

decolor is the classic Contrast Preserving Decolorization method. Instead of a fixed weighted sum of R/G/B, it searches for a gray mapping that keeps the perceptual ordering of nearby colors intact - red vs. orange stay separable even when their luminance is nearly identical. The second output, color_boost, is a saturation-boosted version of the input that makes the color differences pop even harder. Practically, this is the node you reach for when you're converting a color image to grayscale for a downstream edge-detection, line-art, or masking step and the naive conversion loses important detail.

Inputs and outputs

  • src (NPARRAY) - your color image, from Image2Nparray.
  • grayscale (NPARRAY, optional) - this is an out-parameter; the README says to leave it unconnected, the function fills it and it comes back to you as output.
  • color_boost (NPARRAY, optional) - same story.
  • nparray_0 (NPARRAY, output) - the contrast-preserving grayscale image.
  • nparray_1 (NPARRAY, output) - the color-boosted image.

So the pattern to remember: the two optional inputs are OpenCV's out-parameters, and the two outputs are the results. Wire nparray_0 into Nparrays2Image (or a cvtColor with GRAY2BGR) to preview the gray result, or use it directly as a mask/gray source for other OpenCV nodes.

Install

ComfyUI Manager (search opencv-comfyui) or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python

Restart ComfyUI. No models. Use opencv-contrib-python (the README's opencv-python-contrib is the wrong name), and note that decolor lives in the OpenCV contrib build - which is exactly why this pack insists on the contrib package. If you ever see Cannot import name 'guidedFilter' from 'cv2.ximgproc', that's the conflicting-installs symptom; the README links the fix.

Gotchas

The usual pack rules apply: batch_size==1 only, and output nparrays are grayscale (single-channel) or color - if you feed nparray_0 to a node that demands CV_8UC3, you'll get an assertion error and you should run cvtColor with code 8 (GRAY2BGR) first. And remember that this is one of the pack's nodes where the output tuple matters: both nparrays come back regardless of whether you touched the optional inputs.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY
grayscaleoptNPARRAY
color_boostoptNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY