Nodes/opencv-comfyui/OpenCV PCACompute_2
ComfyUI Node

OpenCV PCACompute_2

'keep enough components to explain X% of the variance'

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

PCACompute_2 is the "I don't want to think about how many components" variant of PCA model-learning. Instead of telling OpenCV how many principal components to keep with an integer, you tell it how much of the data's variance you want to preserve with a fraction - 0.95 means "keep enough components that the reconstruction explains 95% of the variance." OpenCV then picks the component count for you. That's a genuinely nicer way to think about PCA: "keep the meaningful structure, drop the noise, and let the math decide where the cutoff is."

It's the retainedVariance overload of cv2.PCACompute - the _0/_1 twins in this family are the maxComponents overload, _2/_3 are this one. Both return the same thing (mean + eigenvectors), they just decide the subspace size differently. _2 is the MatLike variant; _3 is the identical UMat twin.

How it works

cv2.PCACompute(data, mean, retainedVariance, eigenvectors) computes the mean and eigenvectors of data (rows = samples), keeping as many components as needed to account for retainedVariance of the total variance. Returns (mean, eigenvectors) - the exact model that PCAProject_0 and PCABackProject_0 consume.

The inputs that matter

  • data (NPARRAY) - training samples, one per row.
  • mean (NPARRAY) - required (raw OpenCV computes it from an empty array; this wrapper can't express "empty"). Zero vector skips centering.
  • retainedVariance (FLOAT) - the fraction of variance to keep, between 0 and 1. 0.90.99 is the typical band; lower compresses harder, higher is more faithful.
  • eigenvectors (NPARRAY, optional) - out-parameter; ignore it.

Outputs: nparray_0 (mean), nparray_1 (eigenvectors).

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 models to download.

Common issues & troubleshooting

  • retainedVariance must be in (0, 1]. Out of range and OpenCV behaves oddly or throws. A value like 0.95 is the sensible default.
  • Shape discipline: data and mean need matching column counts; (-215:Assertion failed) is the usual shape complaint.
  • Batch rule: batch_size==1 only - use ImageFromBatch (length=1) if Image2Nparray objects.

If you're not sure whether to reach for the maxComponents or retainedVariance overload, retainedVariance is usually the more intuitive dial. You're saying what you want back, not micromanaging how many axes it takes to get there.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
dataNPARRAY
meanNPARRAY
retainedVarianceFLOAT
eigenvectorsoptNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY