Nodes/ComfyUI CV/cv2.applyColorMap (1/2)
ComfyUI Node

cv2.applyColorMap (1/2)

Make a grey score map readable

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.applyColorMap (1/2)
  • src
  • result
◄colormapCOLORMAP_VIRIDIS►

Depth maps, disparity maps, optical-flow magnitudes, saliency maps, decision-boundary scores: all of them are single-channel data that looks like grey mush on screen. A colormap turns 8-bit grey into a colour ramp, and suddenly you can see that the near plane is at 40 and the far one at 120.

This is the colormap overload - pick one of OpenCV's named maps and go. The second overload takes your own 256-entry ramp.

How it works

A colormap is a 256-entry lookup table: value 0 → colour A, value 255 → colour Z, everything in between interpolated by the palette. OpenCV ships around twenty of them (COLORMAP_VIRIDIS, COLORMAP_INFERNO, COLORMAP_JET, COLORMAP_TURBO, COLORMAP_HOT, COLORMAP_BONE, the COLORMAP_*_R reverses, and more), and the colormap widget here is a combo defaulting to COLORMAP_VIRIDIS.

The input must be CV_8UC1 (or CV_8UC3, in which case OpenCV converts it to grey internally, with BGR weights, then colorizes). The output is 3-channel BGR, and because the pack's echo table lists applyColorMap as type-preserving-but-colour-only, an IMAGE in gives an IMAGE out that you can preview directly. A MASK input is refused for this one - a single-channel binary canvas has no meaningful colour semantics, and the pack's socket typing says so.

Also worth knowing: it does not normalize. It maps existing 0–255 values through the LUT, so a disparity map spanning only 0–40 uses the bottom sixth of the palette and looks flat. Normalize first when that's not what you want.

Inputs and outputs

  • src (COMFY_MATCHTYPE_V3) - 8-bit grey or colour. Float or 16-bit data raises; cast or normalize it first.
  • colormap - the named palette.
  • result - the colorized image, echoing src.

Where it earns its place

  • Depth and disparity visualisation. The pack has depth-occlusion and stereo workflows where the whole point is seeing the map.
  • Optical flow magnitude. Colorize the magnitude, and use the flow's direction for hue if you're building the classic flow colour wheel - though for that, the pack's curated CV Flow To Color does the full HSV encoding rather than a straight 8-bit ramp.
  • Score and saliency heat maps. Anything that comes out of the saliency, blob or classifier nodes.
  • Data plots. The decision-boundary workflow uses exactly this node to colour the score surface under a classifier's boundary.

Palette advice, since you're going to ask: viridis (the default) is perceptually uniform and survives being printed in grey or read by a colour-blind colleague. COLORMAP_JET is the old scientific default and lies to you - it has a fake bright band in the middle that the eye reads as a feature. COLORMAP_TURBO is the modern fix for people who want jet's punch without jet's lie.

Install

ComfyUI Manager → comfyui_cv (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 ComfyUI. Python ≥ 3.12, recent V3-API ComfyUI, behaviour pinned to OpenCV 5.0.0.93. Keep the contrib wheel - a non-contrib opencv-python on top of it silently empties the contrib submodules.

Where people get burned

Feeding float data. Depth from a model, disparity from SGBM, flow magnitudes - these are often float or 16-bit, and applyColorMap wants 8-bit. Normalize and cast: cv2_normalize then CV Cast Array to uint8. Skipping this is the most common error with this node.

All-black or all-one-colour output. Your data doesn't span 0–255 (or is entirely zero). There's no auto-stretch here - the pack's curated CV Color Map node is the one that grayscales and normalizes for you, and it's often the better first choice for a raw float score map.

Channels look wrong on a colour input. A 3-channel input gets greyed internally with BGR weights, so an RGB image goes in with the red and blue swapped in the weighting. Feed it grey and be done with it.

Expecting to get grey back. The output is 3-channel from here on. If a downstream node wants single-channel, convert explicitly rather than hoping.

Categoryimage/CV/low-level/cv2 A

Inputs (2)

NameTypeDefaultDescription
srcCOMFY_MATCHTYPE_V3The source image, grayscale or colored of type CV_8UC1 or CV_8UC3. If CV_8UC3, then the CV_8UC1 image is generated internally using cv::COLOR_BGR2GRAY. The image output(s) echo this input's format. 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.
colormapCOMBOCOLORMAP_VIRIDISThe colormap to apply, see #ColormapTypes

Outputs (1)

NameTypeDescription
resultCOMFY_MATCHTYPE_V3Echoes the 'src' input's format: an IMAGE link comes back as IMAGE, MASK as MASK, NPARRAY stays NPARRAY.