Nodes/opencv-comfyui/OpenCV gemm_1
ComfyUI Node

OpenCV gemm_1

Matrix multiply, duplicate edition

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV gemm_1
  • src1
  • src2
  • src3
  • dst
  • nparray
alpha
beta
flags

gemm_1 is gemm_0 with a different number on it. Same code, same inputs, same output - this pack generates one node per OpenCV overload, and cv2.gemm's overloads came out identical after the generator was done. Pick either; there is no hidden difference to find.

So let's use this page for the actual question: when would a ComfyUI workflow ever want a matrix multiply? The answer is when it stops being "generate a picture" and starts being "compute the geometry."

gemm computes dst = alpha · src1^T · src2 + beta · src3, with the transposes controlled by flags. That's the shape of a huge amount of vision math - composing transforms, fitting coordinates, mapping between spaces. If you're building a measurement workflow, a calibration pipeline, or anything that turns detected points into an aligned image, this is the building block.

The inputs

  • src1, src2, src3 (NPARRAY) - three matrices; src1·src2 is the product, src3 the offset.
  • alpha (FLOAT) - scale on the product.
  • beta (FLOAT) - scale on src3.
  • flags (INT) - transposition switches summed together: 1 transposes src1, 2 transposes src2, 4 transposes src3. 0 = none, 2 = transpose the second matrix.

Optional dst (NPARRAY) is an OpenCV out-parameter you can ignore. Output is nparray - a matrix, not an image, so keep it out of Nparrays2Image or you'll hit the pack's 'NoneType' object has no attribute 'shape' error.

Installing

Ships in geroldmeisinger/opencv-comfyui. ComfyUI Manager: search "opencv-comfyui". Or:

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

Restart ComfyUI. requirements.txt installs opencv-contrib-python, numpy, and torch.

Common issues

Dimension mismatches throw OpenCV's size-mismatch assertion, so check your matrix shapes before wiring. And the same single-batch and BGR rules that govern the whole pack apply to any image-sourced inputs.

If matrix math in ComfyUI sounds like more trouble than it's worth, you're probably right for most workflows - it's a niche tool for people doing measurement. But when you need it, it's genuinely nice to have the real cv2.gemm behind a node instead of exporting data out of ComfyUI to a Python script. And since gemm_0 and gemm_1 are interchangeable, your choice of variant is purely cosmetic.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
src1NPARRAY
src2NPARRAY
alphaFLOAT
src3NPARRAY
betaFLOAT
flagsINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY