OpenCV PCACompute2_2
Eigenvalues plus a variance target — the 'just keep the good stuff' PCA
- data
- mean
- eigenvectors
- eigenvalues
- nparray_0
- nparray_1
- nparray_2
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 meannparray_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.9–0.99is 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
retainedVariancemust stay in (0, 1].0.95is the classic default; out-of-range values misbehave.- Shape discipline:
dataandmeancolumn counts must match;(-215:Assertion failed)is the usual shape complaint. - Batch rule:
batch_size==1only - pull a frame withImageFromBatchfirst 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.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| data | NPARRAY | — | |
| mean | NPARRAY | — | |
| retainedVariance | FLOAT | — | |
| eigenvectorsopt | NPARRAY | — | |
| eigenvaluesopt | NPARRAY | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| nparray_0 | NPARRAY | — |
| nparray_1 | NPARRAY | — |
| nparray_2 | NPARRAY | — |