Nodes/Mosaica/Apply LUT To Label Image
ComfyUI Node

Apply LUT To Label Image

Turn a label map into a colored image with any LUT

By Mason-McGough·Created 2 years ago·Updated 2 years ago· 6
Apply LUT To Label Image
  • label_image
  • lut
  • image
scale_labels_to_lut_rangefalse

Apply LUT To Label Image is the node that makes Mosaica's whole "label map" idea useful. The three Analyze nodes (KMeans, MeanShift, Watershed) each hand you a label_image - a single-channel image where every pixel stores a cluster index instead of a color. That's not a picture you can look at. This node is the translator: it takes that label map, looks each index up in a lookup table, and returns a real RGB image.

You might wonder why this exists when the Analyze nodes already output a colorized image. The point is that those auto-generated colors are only one option. Maybe you want a Matplotlib colormap instead of the cluster averages, or a randomly generated palette that's purely aesthetic. That's the workflow this node exists for: keep the segmentation, swap the palette.

How it works

The mechanism is dead simple and worth understanding because it explains the failure modes. The node flattens the label image into a list of integers and indexes the LUT with them: lut[labels]. Label 0 becomes LUT row 0, label 1 becomes row 1, and so on. That's it.

The third input, scale_labels_to_lut_range, exists because that naive indexing can break. If your label map has, say, 40 distinct labels but your LUT only has 20 rows, indexing past the end errors out. When you enable it, the node remaps the labels onto the LUT's row range - label values are stretched so the lowest label hits row 0 and the highest hits the last row. The default is off, so you need to remember it's there when mixing a small LUT with a large label set.

The node validates its inputs aggressively, which is actually friendly: it raises clear errors if the label_image isn't a 4D single-channel tensor or the lut isn't a 2D three-channel one. In practice that means "you wired the wrong output in," because a full-color image won't slot into the label_image socket.

The inputs and a wiring example

  • label_image - take the label_image output from a Mosaica Analyze node.
  • lut - any LUT: the matching lut output from an Analyze node, a LoadLUTFromMatplotlib, or a RandomLUT.
  • scale_labels_to_lut_range - enable when LUT rows and label count don't line up.

A minimal flow: MeanShiftlabel_imageApplyLUTToLabelImage, plus LoadLUTFromMatplotliblut, then out to a save node. Swap the colormap and the whole look changes without re-running the expensive clustering.

Install

It ships in the same pack, so the install is identical to the rest:

cd ComfyUI/custom_nodes
git clone https://github.com/Mason-McGough/ComfyUI-Mosaica

then restart, or use ComfyUI Manager and search "Mosaica". Dependencies are scikit-learn, opencv-python, and matplotlib - all CPU-side, no models to download.

The trap worth knowing

The one thing that trips people up is label 0. It's the background, and it gets whatever color sits in the first row of your LUT. On a viridis-style colormap the first row is usually a deep purple, so expect your background to tint toward that - that's the palette speaking, not an error. If you want a clean white or black background, either pick a colormap that starts there or swap LUTs until the background looks right. It's a two-second fix once you know it's happening, and it's the only real gotcha in an otherwise glue-simple node.

CategoryMosaica/LUT

Inputs (3)

NameTypeDefaultDescription
label_imageIMAGE
lutIMAGE
scale_labels_to_lut_rangeBOOLEANfalse

Outputs (1)

NameTypeDescription
imageIMAGE