Nodes/opencv-comfyui/OpenCV Sobel_0
ComfyUI Node

OpenCV Sobel_0

The classic edge detector, now inside your ComfyUI graph

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV Sobel_0
  • src
  • dst
  • nparray
ddepth
dx
dy
ksize
scale
delta
borderType

Sobel_0 computes the image gradient - how fast brightness changes per pixel - in a chosen direction. It's OpenCV's classic first-derivative edge detector, and it's one of the genuinely useful nodes in this pack, not just a wrapper for completeness.

Where it sits in your workflow: this is deterministic post-processing, the "do one pixel-level thing" tier. A Sobel pass is instant and costs nothing, which makes it the right tool when you want edge structure as data rather than as a pretty picture - line-art extraction, edge-aware masking, directional detail detection. The community's usual instinct is to reach for Canny or a lineart controlnet for edge maps, and those are fine, but Canny is a binary edge map (on/off) while Sobel gives you the raw gradient, sign and all. That sign information - "which way does the edge slope" - is exactly what Sobel uniquely provides and what makes it worth having around.

How it works: Sobel convolves the image with a small separable kernel that approximates a first derivative. The dx and dy inputs choose the direction: dx=1, dy=0 measures horizontal edges (vertical brightness change), dx=0, dy=1 measures vertical edges. To get the full edge strength you combine both directions - sqrt(Gx² + Gy²) - or you just pick the axis you care about. It's the same separable-filter math as sepFilter2D; OpenCV literally implements Sobel as a separable convolution.

Inputs that matter, from the schema:

  • src - NPARRAY. Remember the pack's convention: this is a BGR uint8 array from Image2Nparray, not a Comfy IMAGE. Greyscale works too (and is common for edges); if a function demands it, the README's rule is convert with cvtColor code 6.
  • dx / dy - INT, the derivative order per axis, typically 0 or 1. A very common combination is dx=1, dy=0 or the reverse.
  • ddepth - INT, output depth. The trap here: with -1 (same as src, so uint8), negative gradients clamp to zero and you lose half your signal. Use 5 (CV_32F) to keep signed gradients - it's the difference between seeing only one side of an edge and seeing both.
  • ksize - INT, kernel size; odd numbers like 3, 5, 7. -1 switches to Scharr, a sharper 3×3 variant (only valid when dx+dy == 1).
  • scale / delta / borderType - fine to leave at defaults (1, 0, 4).

Optional dst is the OpenCV out-parameter; leave it unconnected. One nparray output - wire it to Nparrays2Image to preview, or feed it to another matrix node.

Install: ComfyUI Manager → search "opencv-comfyui", or git clone https://github.com/geroldmeisinger/opencv-comfyui into ComfyUI/custom_nodes, restart. Requirements are opencv-contrib-python, numpy, torch. The known startup trap is Cannot import name 'guidedFilter' from 'cv2.ximgproc' - two conflicting OpenCV installs, uninstall one.

The failure mode beginners hit: uint8 clamping. Set ddepth to -1 and the output looks wrong - half your edges missing. It's not the node; it's the depth. Set ddepth=5, and for a viewable image take the absolute value (or feed the two directional passes into a magnitude computation). That's the single adjustment that turns Sobel from "broken" into "the tool you reach for."

Categoryimage/OpenCV

Inputs (9)

NameTypeDefaultDescription
srcNPARRAY
ddepthINT
dxINT
dyINT
ksizeINT
scaleFLOAT
deltaFLOAT
borderTypeINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY