Nodes/opencv-comfyui/OpenCV getDerivKernels_0
ComfyUI Node

OpenCV getDerivKernels_0

Build Your Own Edge-Detection Filters

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV getDerivKernels_0
  • kx
  • ky
  • nparray_0
  • nparray_1
dx
dy
ksize
normalize
ktype

OpenCV getDerivKernels_0 wraps cv2.getDerivKernels(), the function that manufactures the small convolution kernels behind Sobel- and Scharr-style edge detection. Instead of applying an edge filter to an image, it hands you the kernels themselves - two 1D numpy arrays that, applied one after another, approximate a derivative in x and y. It's the "show me your work" node of the edge-detection family, and it's genuinely useful once you know why you'd want the parts instead of the finished product.

How it works

Sobel edge detection is separable: the 2D filter splits into a smoothing kernel along one axis and a derivative kernel along the other. getDerivKernels computes those two 1D kernels for whatever derivative order you ask for. The two outputs - nparray_0 (the x kernel, kx) and nparray_1 (the y kernel, ky) - are what you feed into sepFilter2D (also in this pack) to apply the filter, or into custom convolution code. Because they're 1D arrays, they're also handy for building your own hand-rolled filter pipeline rather than calling the ready-made Sobel node.

The inputs that matter

  • dx and dy (INT) - derivative order in x and y, e.g. dx=1, dy=0 for a first-order horizontal edge detector. One of them is usually 0.
  • ksize (INT) - aperture size; must be odd, from 1 to 7. 1 gives you the Scharr-style 3-tap kernel (better rotational symmetry for small scales), and 3/5/7 give increasingly smoothed Sobel kernels.
  • normalize (BOOLEAN) - whether to normalize the kernels so their coefficients sum to 1 (well, so the derivative has unit gain). Leave it False unless you have a reason; Sobel's classic integer coefficients are what people usually expect.
  • ktype (INT) - output type of the kernel arrays. -1 matches the source type; 5 is CV_32F, 6 is CV_64F. Float types are the safe default for later filtering.
  • kx and ky (NPARRAY, optional) - these are OpenCV's out-parameters (the dst convention). Skip them; the node returns the kernels as its outputs anyway.

Why build rather than blur

If you just want edges, the pack's Sobel, Scharr, and Canny nodes do the whole job in one step. You reach for getDerivKernels when you want to see and control the filter: customizing ksize beyond what the convenience nodes expose, separating the x and y passes to analyze them independently, or baking your own derivative kernel into a filter2D-based pipeline for texture work. It's a builder's tool, not an end-user's.

Install and the usual caveats

Install via ComfyUI Manager (search opencv-comfyui, displayed as "OpenCV"), or:

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

Restart. No models to download.

Pack-wide gotchas apply: the kernels come out as NPARRAY (OpenCV numpy, not Comfy IMAGE), keep batch size at 1, and remember composite inputs are literal strings. dx/dy both 0 is a legal but useless call (you get smoothing, no derivative), and an even ksize will fail OpenCV's assertion. The _1 variant of this node is an identical overload duplicate - no functional difference, pick either.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
dxINT
dyINT
ksizeINT
normalizeBOOLEAN
ktypeINT
kxoptNPARRAY
kyoptNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY