Nodes/opencv-comfyui/OpenCV Canny_0
ComfyUI Node

OpenCV Canny_0

Canny edge detection in ComfyUI — the ControlNet preprocessor you can tune by hand

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV Canny_0
  • image
  • edges
  • nparray
threshold1
threshold2
apertureSize
L2gradient

Canny is the edge detector. It's the classic ControlNet preprocessor - clean, thin, hard edges that are perfect for architectural images, mechanical objects, and anything with strong contours - and it's also just a great general-purpose "give me the structure of this image" filter for masks, sketch passes, and compositing. The OpenCV Canny_0 node wraps cv2.Canny so you can tune its two thresholds by hand in the graph, which is more control than most one-click preprocessor nodes give you.

How it works. cv2.Canny runs the full pipeline: Gaussian smooth, Sobel gradients, non-maximum suppression to thin the edges to one pixel, then a double-threshold pass - pixels above threshold2 are edges, pixels below threshold1 are discarded, and pixels in between survive only if they connect to a strong edge. That last bit is hysteresis, and it's why Canny edges look clean instead of fuzzy. The two thresholds are the entire game: raise them and you get fewer, stronger edges; lower them and you pull in detail. A good starting ratio is roughly threshold1 at half of threshold2 (e.g. 100 and 200).

Inputs. image (NPARRAY), threshold1 and threshold2 (FLOAT), apertureSize (INT, the Sobel aperture - leave at 3), L2gradient (BOOLEAN, whether to use the more accurate L2 gradient norm). The optional edges input is an out-parameter the generator exposed; ignore it. Output is a single nparray - a grayscale edge map, which you can feed straight into a Canny ControlNet as the conditioning image, or convert back with Nparrays2Image for viewing.

The trap that gets everyone. The README's most common Canny error is:

error: (-215:Assertion failed) img.type() == CV_8UC1

That means you fed it a color image. Canny wants a single-channel, 8-bit grayscale input. Convert with the pack's cvtColor node using code = 6 (BGR2GRAY) before you hit Canny - not the IMAGE-based preprocessing you might be used to. Also remember you're on the OpenCV side: Image2Nparray first, and it's BGR 0..255 uint8 in, nparray out.

Install is the standard pack story: ComfyUI Manager → search "OpenCV", or

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-python-contrib

then restart. No models to download. One gotcha: the pack only accepts batch_size == 1 images, so pull a single frame with ImageFromBatch if you're working from a batch.

And yes - Canny_1 next to it is the identical twin. The pack generates one node per overload; _0/_1 are duplicates. Use either.

Categoryimage/OpenCV

Inputs (6)

NameTypeDefaultDescription
imageNPARRAY
threshold1FLOAT
threshold2FLOAT
apertureSizeINT
L2gradientBOOLEAN
edgesoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY