Nodes/opencv-comfyui/OpenCV gemm_0
ComfyUI Node

OpenCV gemm_0

Generalized matrix multiply for people doing math in ComfyUI

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

gemm_0 is the node you reach for when your workflow has quietly turned into linear algebra. GEMM stands for generalized matrix multiplication, and the operation is one line: dst = alpha · src1^T · src2 + beta · src3, where the transposes are optional and controllable. It's OpenCV's cv2.gemm, wrapped for ComfyUI.

Let's be honest about the audience here. If you're generating images and just want filters, this node is not for you - it doesn't touch pixels in any intuitive way, and its output is a matrix, not a picture. You reach for it when you're doing real geometry: composing homographies, transforming point sets, applying an affine or perspective transform by hand, doing coordinate math on detected features. In a ComfyUI context, that's the niche of people who build measurement or calibration workflows rather than "make it prettier" ones.

The matrix triple product is the workhorse of computer vision math, and gemm is the batched, flag-controlled version of it. If you've ever wanted src1 @ src2 with a scaled offset added and transposes where you need them, this is the one-call version.

Inputs that matter

  • src1, src2, src3 (NPARRAY) - the three matrices. src1 and src2 are multiplied; src3 is the additive offset.
  • alpha (FLOAT) - scale applied to the src1·src2 product.
  • beta (FLOAT) - scale applied to src3.
  • flags (INT) - the transposition switches, added together: 1 (GEMM_1_T) transposes src1, 2 (GEMM_2_T) transposes src2, 4 (GEMM_3_T) transposes src3. So 0 = no transposes, 2 = transpose the second matrix, 3 = transpose both src1 and src2, and so on.

Optional dst (NPARRAY) is the usual OpenCV out-parameter; the README says to ignore those. Output is nparray, the result matrix.

Installing

Part of geroldmeisinger/opencv-comfyui. ComfyUI Manager: search "opencv-comfyui". Or:

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

Restart ComfyUI. The pack's requirements.txt installs opencv-contrib-python, numpy, and torch.

Common issues

The big one: the output is not an image. Wire it into Nparrays2Image and you'll get the pack's documented 'NoneType' object has no attribute 'shape' error - that message is literally the README warning you that some nparray outputs aren't pictures. If your matrices are the wrong shape for multiplication, OpenCV throws a size-mismatch assertion; get the dimensions right before you wire things up.

The usual rules apply: Image2Nparray is single-batch only, and remember OpenCV's BGR world if any of the inputs do come from images. This is a math node - it rewards a quick sketch of your matrices on paper before you build the graph. Do that, and gemm_0 is a clean, deterministic way to keep the geometry honest inside ComfyUI instead of bouncing out to Python every time.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
src1NPARRAY
src2NPARRAY
alphaFLOAT
src3NPARRAY
betaFLOAT
flagsINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY