ComfyUI Node
CV Components Touching Border
Keeps every connected component of a binary mask that reaches a chosen image edge - or, with mode = 'remove', everything that does NOT. Touching the frame edge is what separates unbounded background (sky, a table surface, water) from bounded objects, and the inverse is the usual way to drop detections the frame clipped. Neither 'Keep Largest Component' nor 'Select Component At Point' can express it: the largest blob is often something else entirely, and one probe point catches one component, while a background region is routinely cut into many by whatever crosses it. Failure-tolerant: an empty mask (or one where nothing touches the chosen edge) yields an empty mask and found=false, not an error.
CV Components Touching Border
- image
- mask
- area
- count
- found
◄borderany edge►
◄modekeep touching►
◄connectivity8►
◄margin0►
Categoryimage/CV/contours
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| image | COMFY_MATCHTYPE_V3 | Binary image (or MASK); non-zero = foreground. The mask comes back in this input's format. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size. | |
| border | COMBO | any edge | Which image edge a component must reach to count. |
| mode | COMBO | keep touching | 'keep touching' isolates the background that runs off the frame; 'remove touching' drops blobs the frame cut off and keeps the fully-visible ones. |
| connectivityopt | COMBO | 8 | 8 also connects diagonal pixels; 4 only direct neighbours. |
| marginopt | INT | 00–64 | How many pixels in from the edge still count as 'the border'. It has to cover whatever artefact the frame edge already carries, or a region that visually reaches the edge is missed entirely: cv2.remap with BORDER_CONSTANT leaves a dark seam one row deep, and any blur/derivative applied after it smears that seam a few rows further, so a mask derived from gradients may only start answering at row 4. Morphology is NOT the problem - OpenCV pads erosion with +inf, so an opening does not shrink a region at the image border. |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| mask | COMFY_MATCHTYPE_V3 | 0/255 mask of the kept components, in the image input's format. |
| area | INT | Total pixel area kept. |
| count | INT | How many components were kept. |
| found | BOOLEAN | False when nothing was kept (empty mask out). |