Nodes/opencv-comfyui/OpenCV setIdentity_0
ComfyUI Node

OpenCV setIdentity_0

Make a clean identity matrix without leaving ComfyUI

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV setIdentity_0
  • mtx
  • nparray
s

setIdentity_0 takes a matrix, wipes it, and fills it with the identity: ones down the main diagonal, zeros everywhere else. Optionally scaled. It's cv2.setIdentity wrapped as a node, and it's one of those OpenCV math utilities that make sense in a CV pipeline but feel odd sitting in an image generator.

When would you use it? Honestly, rarely, and this is the kind of node you'll go weeks without touching. Its natural home is matrix math in a computer-vision workflow: initializing a homography or affine matrix before composing operations, generating an identity to blend against, or feeding a starting point into solve. It's a "construct the thing that other nodes consume" node, not a pixel effect.

The mechanism is trivial - the function modifies mtx in place, setting mtx[i][j] = s if i == j else 0 - and returns it. Two inputs:

  • mtx - an NPARRAY. It should be a square numeric matrix. This is a key thing to internalize: the NPARRAY type in this pack isn't always an image. setIdentity wants a matrix, and if you feed it an actual photo you'll zero out every pixel except the diagonal, which is a fun way to destroy an image and not a useful one.
  • s - a STRING, and here's the pack's quirk again: composite parameters are Python literals parsed with literal_eval. For a scalar, type a plain number: 1. For a multi-channel matrix you can give a tuple like (1, 1, 1). The default in OpenCV is 1.

One nparray output, which is the identity-filled matrix. Wire it onward to whatever wants a matrix, or to Nparrays2Image if you happen to be visualizing a small matrix as an image.

Installation is the pack-standard: ComfyUI Manager → search "opencv-comfyui", or clone https://github.com/geroldmeisinger/opencv-comfyui into ComfyUI/custom_nodes and restart. Dependencies are opencv-contrib-python, numpy, torch - the last two ship with ComfyUI already. The known install trap: if another custom node pulls in a different OpenCV build you'll see Cannot import name 'guidedFilter' from 'cv2.ximgproc' at startup; uninstall the conflicting package and keep one.

Where people get burned: the s string. 1 parses fine; 1, 1, 1 (no parentheses) does not, and literal_eval throws invalid syntax (<unknown>, line 0). Also remember setIdentity_0 has a twin, setIdentity_1, that does exactly the same thing - the pack's auto-generator numbered OpenCV's overloads and this pair came out functionally identical. Pick either and stay consistent.

If you're a beginner and this node showed up because you loaded someone's math-heavy workflow, don't stress about understanding it deeply - just know it builds identity matrices that other matrix nodes consume. You'll only touch it when you're doing geometry, calibration, or transform math in the graph.

Categoryimage/OpenCV

Inputs (2)

NameTypeDefaultDescription
mtxNPARRAY
sSTRING

Outputs (1)

NameTypeDescription
nparrayNPARRAY