Nodes/opencv-comfyui/OpenCV PCACompute2_2
ComfyUI Node

OpenCV PCACompute2_2

Eigenvalues plus a variance target — the 'just keep the good stuff' PCA

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV PCACompute2_2
  • data
  • mean
  • eigenvectors
  • eigenvalues
  • nparray_0
  • nparray_1
  • nparray_2
retainedVariance

Of all the PCA nodes in this pack, PCACompute2_2 is the one I'd actually reach for. It combines the two genuinely useful ideas: it returns the eigenvalues (so you can see how much each component matters), and it sizes the subspace by a retainedVariance target - "keep enough components that I get 95% of the variance back" - instead of making you guess an integer component count. You set the quality bar; OpenCV figures out how many axes that takes.

It's the retainedVariance overload of cv2.PCACompute2. The _0/_1 twins are the maxComponents overload; _2 is the MatLike variant and _3 its identical UMat twin.

How it works

cv2.PCACompute2(data, mean, retainedVariance, eigenvectors, eigenvalues) computes the mean, eigenvectors, and eigenvalues of data (rows = samples), keeping as many components as needed to account for retainedVariance of the total variance. Three outputs:

  • nparray_0 - the mean
  • nparray_1 - the eigenvectors (principal axes)
  • nparray_2 - the eigenvalues (variance per component)

mean + eigenvectors feed PCAProject_0/PCABackProject_0; the eigenvalues tell you where the variance curve flattens - the visual "cut here" signal. And like the rest of the PCACompute2 family, it's the more robust pick when your samples are fewer than your dimensions.

The inputs that matter

  • data (NPARRAY) - training samples, one per row.
  • mean (NPARRAY) - required by the wrapper; zero vector = skip centering.
  • retainedVariance (FLOAT) - fraction of variance to preserve, in (0, 1]. 0.90.99 is the sane band.
  • eigenvectors, eigenvalues (NPARRAY, optional) - out-parameters; leave them.

How to install it

Pack-level install, once:

  • ComfyUI Manager → search opencv-comfyui → Install, restart ComfyUI.
  • Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

Requires opencv-contrib-python. No model downloads.

Common issues & troubleshooting

  • retainedVariance must stay in (0, 1]. 0.95 is the classic default; out-of-range values misbehave.
  • Shape discipline: data and mean column counts must match; (-215:Assertion failed) is the usual shape complaint.
  • Batch rule: batch_size==1 only - pull a frame with ImageFromBatch first if needed.

This is the node that does the whole job: learns the model, reports how much each axis matters, and lets you set the quality target instead of the axis count. The rest of the family is variations on that theme.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
dataNPARRAY
meanNPARRAY
retainedVarianceFLOAT
eigenvectorsoptNPARRAY
eigenvaluesoptNPARRAY

Outputs (3)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY
nparray_2NPARRAY