Nodes/opencv-comfyui/OpenCV magnitude_0
ComfyUI Node

OpenCV magnitude_0

Combine Sobel X and Y gradients into a real edge map

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV magnitude_0
  • x
  • y
  • magnitude
  • nparray

If you've ever run a Sobel filter and thought "the horizontal edges look great but the vertical ones vanished" - this is the fix. OpenCV magnitude_0 wraps cv2.magnitude, which computes sqrt(x² + y²) per pixel across two same-shaped arrays. Feed it the X-gradient and the Y-gradient of an image and it merges them into the gradient magnitude: a single map where edge strength in every direction is preserved. It's the mathematically correct way to get an edge map out of derivative filters, and it's why the result looks so much better than a single-direction Sobel pass.

It's also how you'd complete the classic edge-detection chain by hand: run Sobel twice on the same image - once with dx=1, dy=0 for the X gradient, once with dx=0, dy=1 for the Y (either Sobel_0 or Sobel_1; the pair is the usual identical overload) - then feed both into magnitude_0, normalize, and you've got line art or a ControlNet edge condition. No model, instant, deterministic.

How it works

Each pixel in x and y holds the gradient in one axis. The magnitude - the length of that gradient vector - tells you how fast the image intensity is changing at that pixel regardless of direction. sqrt(x² + y²) is that length. It's the same operation behind cartToPolar (which also gives you the angle), just without the angle output.

Inputs:

  • x - first NPARRAY (your X gradient).
  • y - second NPARRAY (your Y gradient), same shape as x.
  • magnitude - optional out-parameter. Skip it; the result comes back on the nparray output.

Both arrays must match in size and ideally in type. Pack rule applies: these are nparrays, so Image2Nparray/Nparrays2Image are your bridge.

Install

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

Restart, or install via ComfyUI Manager (search "opencv"). Dependencies: opencv-contrib-python, numpy, torch. No models.

Common issues

  • Result is too dark / float garbage - gradient magnitudes are floats far outside display range. Normalize (stretch-to-range, or normalize_0 from the pack) before previewing.
  • Mismatched shapes - x and y must be the same size. If one Sobel output got resized downstream, that's your error, and it usually shows as a shape assertion failure.
  • Batch error - batch_size==1 only; slice with ImageFromBatch before converting.

The _1 variant is identical (the pack numbers OpenCV's MatLike/UMat overloads rather than merging them). Honest take: if you're only after edges for ControlNet, Canny does all of this and thresholding for you in one node. But when you want gradient strength as a continuous map - for a depth-ish effect, a custom detector, or just to understand your image - this pair of Sobel + magnitude is the right tool.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
xNPARRAY
yNPARRAY
magnitudeoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY