ComfyUI Node Runs on cloud

Contours

Find the shapes in your mask, the OpenCV way

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
Contours
  • image
  • CV_CONTOURS
  • CV_CONTOUR
  • CV_CONTOURS_HIERARCHY
retrieval_modeRETR_LIST
approximation_modeCHAIN_APPROX_SIMPLE

Contours is a thin wrapper around OpenCV's findContours, which means it answers a question you'll actually ask in a masking workflow: "given a black-and-white mask, what are the individual shapes in it?" Feed it a binary image and you get back a list of contours - each one a connected boundary you can count, filter, measure, redraw, or convert back into a mask.

It's part of this pack's CV/Contour section, and it's the entry point to a whole mini-ecosystem: Draw Contour(s) to visualize, Contour To Mask to turn one shape back into a filled mask, plus a Filter Contour node in the pack that ranks contours by a fitness expression you type. None of that works without this node producing the CV_CONTOURS.

How it works

The node converts the image to grayscale and runs cv.findContours with your two OpenCV mode settings:

  • retrieval_mode - how contours nest. RETR_LIST (default) returns all contours with no hierarchy; RETR_EXTERNAL only the outermost; RETR_CCOMP / RETR_TREE build hierarchy. For most masking work, RETR_LIST or RETR_EXTERNAL is what you want.
  • approximation_mode - how much the boundary is simplified. CHAIN_APPROX_SIMPLE (default) compresses straight runs to their endpoints; CHAIN_APPROX_NONE keeps every pixel of the boundary. Simple is fine unless you need exact boundary points.

The important catch is in the author's own note: no threshold is applied. The image is converted to gray but the node expects a binary (black-and-white) image where the objects of interest are nonzero. If you feed a grayscale photo, every gray value becomes a contour candidate and you'll get noise. Threshold first - this pack even has Threshold-family nodes and EqualizeHistogram in the same CV section for exactly that prep step.

Three outputs:

  • CV_CONTOURS - the full list (for Draw Contour(s), filtering).
  • CV_CONTOUR - a list output, so individual contours can be pulled out slot-by-slot (for Contour To Mask, geometry nodes like ContourGetBoundingRect).
  • CV_CONTOURS_HIERARCHY - the nesting hierarchy, when you use a tree retrieval mode.

Tips

  • Thresholding before this node is not optional - do it or get garbage.
  • RETR_EXTERNAL + CHAIN_APPROX_SIMPLE is the "just give me clean shapes" combo.
  • Objects touching the image edge can produce odd contours; pad the image if it matters.

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. This one needs OpenCV, which the pack's requirements install. No models - pure computer vision on the CPU.

CategoryBmad/CV/Contour

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
retrieval_modeCOMBORETR_LIST5 options: RETR_EXTERNAL, RETR_LIST, RETR_CCOMP, RETR_TREE, RETR_FLOODFILL
approximation_modeCOMBOCHAIN_APPROX_SIMPLE4 options: CHAIN_APPROX_NONE, CHAIN_APPROX_SIMPLE, CHAIN_APPROX_TC89_L1, CHAIN_APPROX_TC89_KCOS

Outputs (3)

NameTypeDescription
CV_CONTOURSCV_CONTOURS
CV_CONTOURCV_CONTOUR
CV_CONTOURS_HIERARCHYCV_CONTOURS_HIERARCHY