Nodes/opencv-comfyui/OpenCV spatialGradient_1
ComfyUI Node

OpenCV spatialGradient_1

SpatialGradient_1 — the Scharr derivative node's twin

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV spatialGradient_1
  • src
  • dx
  • dy
  • nparray_0
  • nparray_1
ksize
borderType

Let's get it out of the way immediately: spatialGradient_1 is spatialGradient_0 under a second auto-generated name. cv2.spatialGradient's signature is declared twice in OpenCV's type stubs, the pack's generator numbers both, and you get two nodes that call the identical function with the identical arguments. Same inputs, same outputs, same Scharr operator underneath. The "1" is a serial number.

If you got here without the sibling page, that's where the real guide lives - what dx/dy mean, why the input must be grayscale, how to pick borderType. This page is the duplicate's reference, but it's worth one honest warning up top because it's the pack's most common footgun.

The input that bites everyone

  • src (NPARRAY) - the image, and it must be single-channel. OpenCV asserts on this, so you'll get error: (-215:Assertion failed) img.type() == CV_8UC1 in function if you feed it the BGR output of Image2Nparray directly. The fix is right in the pack README: run cvtColor with code 6 (BGR2GRAY) first.
  • ksize (INT) - set to 3; spatialGradient uses Scharr, and 3 is the value that makes sense.
  • borderType (INT) - 4 (reflect-101) to start, 1 (replicate) if the frame edges misbehave.
  • dx / dy (optional NPARRAY) - out-parameters; unwired is fine.

Outputs: nparray_0 (x-derivative) and nparray_1 (y-derivative), same shape as src.

The workflow in brief

Image2NparraycvtColor (code 6) → spatialGradient_1 → combine or preview. The outputs are signed float arrays, so rendering one straight through Nparrays2Image will look like static until you normalize; that's expected. For gradient magnitude, combine with sqrt(dx² + dy²) in a downstream node. Good for edge maps, masking, and detail detection.

Install

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

or ComfyUI Manager → "opencv-comfyui". Restart. Deps: opencv-contrib-python, numpy, torch.

Troubleshooting

  • error: (-215:Assertion failed) img.type() == CV_8UC1 - src isn't grayscale; cvtColor code 6 fixes it.
  • Border artifacts - switch borderType to 1.
  • Cannot import name 'guidedFilter' at startup - conflicting OpenCV packages; README links the fix.

One node, two names, same Scharr kernel. If you've read spatialGradient_0, you're fully equipped.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
srcNPARRAY
ksizeINT
borderTypeINT
dxoptNPARRAY
dyoptNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY