OpenCV getStructuringElement_0
The kernel factory you didn't know you needed
- nparray
If you've ever wanted to erode, dilate, or morphologically clean up a mask in ComfyUI, this is the node that makes it possible. getStructuringElement_0 doesn't touch a single pixel itself - it builds the little binary grid (a "structuring element," aka kernel) that every erosion, dilation, and morphological operation needs. Think of it as the stencil you stamp over your mask to define the neighborhood the operation looks at. In the wider workflow: you generate an inpaint mask, it's got speckles and hairy edges, and you run it through erode/dilate/morphologyEx to tidy it before the sampler sees it. Those nodes all want a kernel, and this is where you make one.
How it works
It's a thin wrapper around OpenCV's cv2.getStructuringElement, one of 600+ top-level cv2 functions this pack auto-generated from the type definitions. Three inputs do everything:
- shape (
INT): the geometry.0= rectangle,1= cross,2= ellipse. You'll use0for 90% of cases. - ksize (
STRING): the size as a Python literal, like(3, 3)or(7, 7). Odd numbers are the norm so the anchor sits in the center. - anchor (
STRING): the reference point inside the kernel, also a literal.(-1, -1)means "center," which is what you want almost always.
The output is a single nparray - a small matrix of 0s and 1s shaped by your ksize. A 3×3 rectangle is all ones; a cross is ones on the center row and column. That's the kernel, ready to wire into erode_0, dilate_0, or morphologyEx from the same pack. Larger kernels mean stronger effects; if your mask edges come out eroded too far, shrink the ksize instead of fighting the operation.
The literal-string gotcha
Because the pack represents composite types as text parsed with Python's ast.literal_eval, you must type ksize as a real tuple. (3, 3) works. 3 or "3,3" throws the pack's signature error - invalid syntax (<unknown>, line 0). This trips everyone once; after that it's second nature.
Install
This whole pack installs the same way. ComfyUI Manager: search "opencv-comfyui" (or just "OpenCV") and install. Or by hand:
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
then restart ComfyUI. The pack's own requirements are opencv-contrib-python, numpy, and torch - you almost certainly have the last two already, and ComfyUI Manager installs OpenCV for you. No models, no API keys, nothing to download.
Common issues
invalid syntax (<unknown>, line 0)- yourksizeoranchorisn't a valid Python tuple literal. Fix:(5, 5)and(-1, -1).Cannot import name 'guidedFilter' from 'cv2.ximgproc'at startup - you have two conflicting OpenCV packages installed (another custom node pulled one in). The README points to the standard fix: keep a singleopencv-contrib-pythoninstall and remove the duplicates.- The
nparrayoutput is not a Comfy image. If you want to see the kernel, you can't just drop it into a preview - route it throughNparrays2Imageonly if you actually need to look at it.
Honest take: most of this pack is rough auto-generated plumbing, but this one is quietly one of the more useful nodes in it. Mask cleanup before an inpaint pass is a real workflow step, and this is the only dependency-free way to do it in the graph.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| shape | INT | — | |
| ksize | STRING | — | |
| anchor | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |