Nodes/ComfyUI-MIDI3D/RGB to Instance Mask (MIDI-3D)
ComfyUI Node

RGB to Instance Mask (MIDI-3D)

Turn a color-coded picture into the mask MIDI-3D actually wants

By PozzettiAndrea-archive·Created 9 months ago·Updated 7 months ago· 3
RGB to Instance Mask (MIDI-3D)
  • image
  • instance_mask
ignore_blacktrue

MIDI-3D doesn't understand "the chair, the table, and the lamp" from a plain image. It understands an instance mask - a grayscale image where each object has its own numeric label. The question is how you get that mask, and this node answers it the easy way: you draw or generate a color-coded image where every object is a different color, and it converts that into the labeled mask.

That's the entire job, and it's a genuinely useful one because color-coded masks are how people naturally produce segmentation: paint each object a distinct color in any image editor, or render one from a 3D tool, and you're done.

How it works

  • image - an IMAGE where each object is a different RGB color.
  • ignore_black - default true. Black (0,0,0) is treated as background (label 0). Turn it off only if one of your objects is actually black; otherwise every black pixel becomes an instance, which you do not want.

The node finds every unique color in the image, assigns labels in order of appearance (first color → instance 1, second → instance 2, …), and emits a single instance_mask MASK. Output labels are sequential integers, which is exactly the format the preprocess nodes split on - "different values = different instances."

The node's docstring calls out the intended audience for CombineMasksToInstance as the SAM path, and this one is the manual path: it's the natural companion to the pack's example workflow, which loads 00_seg.png (a color-coded segmentation) and converts it before preprocessing.

The practical trap: similar colors

Each unique RGB value becomes an instance. That sounds obvious until you paint two objects with two shades of the same blue and discover the model thinks they're the same object - or worse, an anti-aliased edge introduces a third "color" that becomes its own spurious micro-instance. Two rules save you:

  1. Use flat, widely separated colors per object (think #FF0000, #00FF00, #0000FF, not #4466AA and #4477AA).
  2. If your mask came from a renderer with anti-aliasing, flatten it first. A hard-edged mask is what this node wants.

ignore_black handles the background, but it does not deduplicate near-colors, and it won't help if the background is white - a white background becomes instance 1 unless you fill it with black first.

Where it sits in the workflow

Load Image (color-coded segmentation) → RGB to Instance Mask → MIDI-3D Preprocess → MIDI-3D Process

If you already have per-object binary masks - say from SAM, which gives you one mask per segment - you don't need this node; Combine Masks to Instance is the one for that job. Use this when your input is a single color-painted image. If you have neither, pick whichever matches the assets you actually have, because both feed the same preprocess step.

One last note: the output labels depend on color sort order, not on which object is "first" semantically. So if your workflow depends on instance 1 always being the chair, don't rely on it - recolor your mask so the chair's color sorts first, or treat instance order as arbitrary. For scene generation it rarely matters; the model handles all instances together anyway.

CategoryMIDI3D

Inputs (2)

NameTypeDefaultDescription
imageIMAGERGB image where each object is a different color
ignore_blackoptBOOLEANtrueTreat black pixels as background (label 0)

Outputs (1)

NameTypeDescription
instance_maskMASKGrayscale instance mask for MIDI-3D