Nodes/opencv-comfyui/OpenCV matMulDeriv_1
ComfyUI Node

OpenCV matMulDeriv_1

The twin of the matrix-derivative node (they're the same)

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV matMulDeriv_1
  • A
  • B
  • dABdA
  • dABdB
  • nparray_0
  • nparray_1

Here's the whole secret of this node before we start: matMulDeriv_1 and matMulDeriv_0 are the same node. Identical inputs, identical outputs, identical math. The _0/_1 numbering is just the opencv-comfyui pack's way of handling overloads - the OpenCV type stubs declare cv2.matMulDeriv twice (once taking MatLike arrays, once taking UMat arrays), and the pack's code generator dutifully emitted one node per overload. Since there's no Image2UMat node, both end up running on regular CPU nparrays. You can wire them interchangeably and nobody will ever be able to tell.

So if you're here, what you actually want is the OpenCV matMulDeriv function: it computes the analytic partial derivatives of a matrix product C = A·B with respect to each factor, and hands you both Jacobians in one call. That's ∂(AB)/∂A (which comes down to Bᵀ) and ∂(AB)/∂B (which comes down to Aᵀ). It's the kind of thing classical CV pipelines use when they're doing nonlinear least-squares - camera calibration, pose refinement, anything where you need the derivative of a re-projection error with respect to the unknowns.

Using it

Both inputs are NPARRAY - the pack's type for raw numpy arrays. You need A and B, the two matrices. Then ignore dABdA and dABdB: they're optional inputs, but in OpenCV they're out-parameters (buffers the function writes into), not things you're supposed to supply. Leave them disconnected, per the README's "Avoid the optional out-parameters" rule.

Outputs are nparray_0 and nparray_1 - the two derivative matrices. A word of caution: these are matrices, not images. Feeding them into Nparrays2Image is how you get the pack's famous 'NoneType' object has no attribute 'shape' error, because it tries to treat a derivative matrix as a picture.

Install and gotchas

Get the pack with ComfyUI Manager (search "opencv-comfyui") or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

then restart ComfyUI. Dependencies are opencv-contrib-python, numpy, torch. If another custom node already pulled in OpenCV, you're usually set - the common trip-up is conflicting OpenCV versions, which shows up as Cannot import name 'guidedFilter' from 'cv2.ximgproc' (the README links a ComfyUI_LayerStyle issue about it).

Honest take: this is one of the pack's most academic nodes. OpenCV's whole point here is small, deterministic, milliseconds-fast transforms - a blur, an edge map, a geometry measurement. A matrix Jacobian in a diffusion workflow is a rare thing. But if you're building one of those rare things, at least you now know the _1 suffix is a fiction and the node just works.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
ANPARRAY
BNPARRAY
dABdAoptNPARRAY
dABdBoptNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY