Nodes/comfyui_bmad_nodes/Contour To Mask
ComfyUI Node Runs on cloud

Contour To Mask

Turn one contour back into a filled mask

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
Contour To Mask
  • image
  • contour
  • IMAGE
output_formatRGB

Contours finds the shapes; this node closes the loop. Contour To Mask takes a single contour and a reference image, and draws that contour filled into a blank canvas the same size as the reference - giving you back a solid binary silhouette you can use as a mask or wire into any image-based workflow.

If your pipeline is "segment a photo, find contours, then act on one specific shape," this is the node that turns a boundary back into something usable.

How it works

Mechanically it's OpenCV's drawContours with thickness -1 (fill) onto a black image sized like the reference image input:

  • image - used for two things: its dimensions define the output canvas, and it's the context you're masking against. Pass the original image here, not a shrunken version, or the mask will be the wrong size.
  • contour - a single CV_CONTOUR, from Contours' list output or GetContourFromList.
  • output_format - RGB (default) or GRAY. Both produce the same filled silhouette; RGB gives you a white shape on black across three channels, GRAY a single-channel image. If the downstream node wants a proper mask (single channel), pick GRAY.

Output: one IMAGE. Note it's still an IMAGE, not the core MASK type - ComfyUI's mask-typed ports are separate. If you need a genuine MASK, many ComfyUI nodes accept a grayscale image as mask-like input, or you can feed the GRAY output through a converter.

Where it slots in

A typical contour workflow in this pack:

  1. Threshold a grayscale version of your image.
  2. Contours → get CV_CONTOURS and CV_CONTOUR.
  3. Use Filter Contour or GetContourFromList to pick the shape you care about.
  4. Contour To Mask to get the filled mask.
  5. Composite, inpaint with it, or stack it with other masks.

The gotcha that bites people: canvas size comes from the image input, not from the contour. A contour from a downscaled source fed with a full-size reference gives you a correctly-scaled canvas but a contour whose coordinates no longer line up. Keep the source image dimensions consistent between Contours and Contour To Mask.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_bmad_nodes
cd comfyui_bmad_nodes
pip install -r requirements.txt

or install "comfyui_bmad_nodes" via ComfyUI Manager and restart. OpenCV does the drawing; no models, no downloads beyond the pack's shared requirements. If you're already using Contours, this is the natural next step in the chain.

CategoryBmad/CV/Contour

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
contourCV_CONTOUR
output_formatCOMBORGB2 options: RGB, GRAY

Outputs (1)

NameTypeDescription
IMAGEIMAGE