Contours
Find the shapes in your mask, the OpenCV way
- image
- CV_CONTOURS
- CV_CONTOUR
- CV_CONTOURS_HIERARCHY
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_EXTERNALonly the outermost;RETR_CCOMP/RETR_TREEbuild hierarchy. For most masking work,RETR_LISTorRETR_EXTERNALis what you want. - approximation_mode - how much the boundary is simplified.
CHAIN_APPROX_SIMPLE(default) compresses straight runs to their endpoints;CHAIN_APPROX_NONEkeeps 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 likeContourGetBoundingRect). - 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_SIMPLEis 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.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| retrieval_mode | COMBO | RETR_LIST | 5 options: RETR_EXTERNAL, RETR_LIST, RETR_CCOMP, RETR_TREE, RETR_FLOODFILL |
| approximation_mode | COMBO | CHAIN_APPROX_SIMPLE | 4 options: CHAIN_APPROX_NONE, CHAIN_APPROX_SIMPLE, CHAIN_APPROX_TC89_L1, CHAIN_APPROX_TC89_KCOS |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| CV_CONTOURS | CV_CONTOURS | — |
| CV_CONTOUR | CV_CONTOUR | — |
| CV_CONTOURS_HIERARCHY | CV_CONTOURS_HIERARCHY | — |