Nodes/ComfyUI CV/cv2.ximgproc.GradientDericheX
ComfyUI Node

cv2.ximgproc.GradientDericheX

An edge detector that doesn't get slower when you smooth more

By bmad4ever·Created 3 months ago·Updated 14 days ago· 1
cv2.ximgproc.GradientDericheX
  • op
  • nparray
◄alpha0.0000►
◄omega0.0000►

Deriche's recursive operator is the answer to a question you've hit: "I want Sobel's gradient, but without the noise." The usual fix - Gaussian blur, then Sobel - costs more the more you smooth, because the Gaussian kernel grows with sigma. Deriche's filter implements the smoothing recursively, so the cost per pixel barely moves as you widen it. That's the entire pitch, and for anyone building edge or gradient maps at video resolution it's a real one.

The X here is direction: this node differentiates horizontally (it measures how fast things change left-to-right). Its twin in the same module, cv2.ximgproc.GradientDericheY, does the vertical axis. Use them together, as a pair.

How it works

op is your source image - 8- or 16-bit, one or three channels; despite the odd parameter name it's the image, not an operation. The filter runs an IIR pass in both directions along X, approximating a derivative of a Gaussian. Output is a single-channel 32-bit float gradient map, and it's signed: negative values are dark-to-bright transitions, positive the reverse. That's why it comes back as a plain NPARRAY rather than an IMAGE - the pack deliberately doesn't hand you a float score map dressed up as a picture.

To look at it, CV Array → Image (float input gets min-max normalized) or CV Color Map for a heatmap. To combine X and Y into a gradient magnitude, run both nodes into the generated cv2.magnitude wrapper, which is what it takes two of these for.

Inputs that matter

  • alpha - the sharpness knob. The author's tooltip: larger = narrower smoothing and sharper, noisier edges; smaller = broader smoothing. This is your blur-vs-detail trade, in one number.
  • omega - the spatial frequency the operator is tuned to, and the tooltip's guidance is "small, e.g. 0.1". Change alpha first; omega is the tuning you touch when you already know the band you care about.

One warning that isn't in the schema: both widgets default to 0. The generated node passes those zeros straight through, and 0 sits outside the range either parameter was designed for - a fresh node is not going to give you a sensible derivative. Type real numbers in before you conclude the node is broken.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv

Restart ComfyUI, or install ComfyUI CV from ComfyUI Manager. Python ≥3.12, a recent V3-API ComfyUI, opencv-contrib-python-headless~=5.0.0.93. No model downloads. Because ximgproc lives in the contrib build, a plain opencv-python wheel installed over the contrib one makes these nodes vanish; tools/repair_opencv_contrib.py --check diagnoses it.

Common issues

Unset alpha/omega. See above. This is the #1 reason a first run looks like a flat grey field.

You expected an image and got an array. The output is a signed float gradient - Inspect CV Data shows you min/max and the dtype, which is how you decide whether to normalize, take abs, or combine with Y.

A 3-channel input gives you one channel out. The gradient is computed per-pixel and returned single-channel here; if you want per-channel gradients you're looking at colour-space questions the wrapper won't answer for you. Grayscale the input if you want a predictable result.

ControlNet conditioning. Yes, this is a legitimate way to build a gradient/edge preprocessor - it sits in the same family as the classical Sobel/Canny preprocessors that concepts.md records as the original "structure without detail" conditioning path. But it emits raw floats, so you need a normalizing step before anything expects a mask or an image. Canny is still the boring, reliable choice for a ControlNet input; Deriche earns its place when you're tuning the smoothing radius and want it to not cost anything.

Categoryimage/CV/low-level/ximgproc

Inputs (3)

NameTypeDefaultDescription
opNPARRAY,IMAGE,MASK - - - 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.
alphaFLOAT0.0000-1e+38–1e+38 - - -
omegaFLOAT0.0000-1e+38–1e+38 - - -

Outputs (1)

NameTypeDescription
nparrayNPARRAY—