Nodes/comfyui_bmad_nodes/Get Contour from list
ComfyUI Node Runs on cloud

Get Contour from list

Pick one OpenCV contour out of the pile — Get Contour from list

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
Get Contour from list
  • contours
  • CV_CONTOUR
index0

Get Contour from list is the index node of Bmad's OpenCV contour toolchain: a CV_CONTOURS list in, one CV_CONTOUR out, chosen by an integer. If you've ever run a contour-finding node and gotten back a hundred blobs when you wanted just the one that mattered, this is the node that pulls it out.

It slots into the pack's CV pipeline, which mirrors OpenCV's classic workflow: threshold an image, find contours, then do geometry on the results. Contours runs OpenCV's findContours and returns the full list; Get Contour from list picks entry index; and downstream you can feed that single contour into ContourToMask (turn it into a mask), ContourGetBoundingRect (get its box), or Filter Contour (rank candidates by fitness). The practical move is to chain ContoursFilter Contour to get the list down to the shapes you care about, then index into it here.

How it works

The implementation is refreshingly honest:

def get_contour(self, contours, index):
    if index >= len(contours):
        return (None,)
    return (contours[index],)

So unlike the pack's FromListGet1* nodes, this one does not wrap around on a bad index - out of range hands you None instead. That's a deliberate design choice, and the pack leans on it: the contour nodes are built to tolerate None. ContourGetBoundingRect, for instance, prints "Contour = None !" and returns zeros rather than crashing. Which is your clue to keep an index that can legitimately miss - it's a way of saying "no contour here" without blowing up the graph.

Inputs and outputs

  • contours - a CV_CONTOURS list, from the Contours node (same pack) or anything that outputs the contour type.
  • index - an INT, default 0, minimum 0 - no negatives, no wrap.
  • Output: one CV_CONTOUR, or None if the index is past the end.

Install

Ships in bmad4ever/comfyui_bmad_nodes. In ComfyUI Manager, search "comfyui_bmad_nodes" ("Bmad Nodes"), install, restart. Manual:

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

Restart ComfyUI after. This node needs OpenCV, which is in the requirements (opencv-python); no model files.

Common issues

The main gotcha is the asymmetry with the FromListGet1* nodes: index past the end here gives you None, not a wrap-around. That's usually what you want, but if you're driving the index from a random node, expect None results to propagate downstream - make sure whatever consumes the contour tolerates it (Bmad's own nodes do). Second, index has a floor of 0: negative indexes are clamped at the input level, so you can't count from the end. If you want the last contour, compute the list length first.

CategoryBmad/CV/Contour

Inputs (2)

NameTypeDefaultDescription
contoursCV_CONTOURS
indexINT0

Outputs (1)

NameTypeDescription
CV_CONTOURCV_CONTOUR