cv2.matMulDeriv
Jacobians of a matrix product, for the three people doing this by hand
- A
- B
- dABdA
- dABdB
Here's the honest version: this is a derivative calculator, and if you already know you need it, you don't need this article. If you googled it because you saw it in the node list, the answer is that it has almost nothing to do with images.
cv2.matMulDeriv(A, B) returns the partial derivatives of the matrix product A·B with respect to each of its two factors. It exists because OpenCV's own bundle adjustment and calibration code is a hand-rolled Levenberg-Marquardt solver, and every such solver needs Jacobians. If you're reconstructing that kind of optimisation as a ComfyUI graph - or teaching yourself how the camera-calibration nodes fit together - these are the building blocks. Otherwise it's a curiosity that sits in the node menu next to the things you actually use.
The sockets
A and B are NPARRAY-only. The tooltips say it plainly - "a data array (points / matrix), NOT an image - only an NPARRAY link is accepted here". So you bring matrices in via Parse Matrix (delimited text) or CV Numbers To Array, not from Load Image.
Two outputs, both NPARRAYs: dABdA and dABdB. Sizes follow the shapes: for A of m×n and B of n×p, the product is m×p, so dABdA comes out as (m·p)×(m·n) and dABdB as (m·p)×(n·p) - each row of the Jacobian is one entry of the product, each column one entry of the input. The entries are mostly zeros with the other matrix's values dropped into the right blocks, which is exactly what a hand-derived Jacobian would look like.
There are no optional parameters. No scale, no flags, nothing to tune. It's a formula.
Where it fits
Two plausible uses. First, verification: take a small A and B, compute the product in the graph, nudge one element of A, and confirm that the change matches the Jacobian entry - a cheap way to check the arithmetic of a pipeline you're building up. Second, supplying a real optimiser: cv2.calcCovarMatrix, cv2.invert, cv2.solve, cv2.projectPoints and friends are all wrapped in this pack, and the derivative nodes are the layer between "here are my matrices" and "here's the update step". The pack ships CV Parse Matrix, CV Cast Array, CV Reshape Array and Inspect CV Data precisely because this end of the library is all shapes and you'll want to look at them.
For image work - masks, edges, warps, detection - this node is noise in the list. Ignore it.
Installing the pack
ComfyUI CV (bmad4ever/comfyui_cv) - ComfyUI Manager, search "comfyui_cv", or:
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"
Restart ComfyUI. Python ≥ 3.12 and a V3-API ComfyUI build; the single dependency is that pinned contrib OpenCV wheel. matMulDeriv is core OpenCV and needs no model.
Where people get burned
Wrong dimensionalities. A's column count has to equal B's row count, or the product doesn't exist and you get an error that doesn't spell this out. Use CV Array Shape first and stop guessing.
Expecting an (m·n)×(n·p) answer. The Jacobians are laid out over the product's entries, and this trips up anyone importing a formula from a textbook that uses a different convention. Print the shape before you trust a number.
Float data with integer habits. These are meant to be float matrices; CV Cast Array if something upstream handed you int32.
The standard pack-wide gotcha. All four opencv-* wheels share a single site-packages/cv2, so installing a non-contrib wheel over the contrib one silently empties the contrib submodules, and the contrib-backed nodes would simply stop registering. This node is core, so it survives - but python tools/repair_opencv_contrib.py --check (then --apply) is the fix when half the pack's menu goes missing.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| A | NPARRAY | First multiplied matrix. A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here. | |
| B | NPARRAY | Second multiplied matrix. A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here. |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| dABdA | NPARRAY | — |
| dABdB | NPARRAY | — |