OpenCV PCACompute2_0
Like PCACompute, but it also tells you how much each axis matters
- data
- mean
- eigenvectors
- eigenvalues
- nparray_0
- nparray_1
- nparray_2
PCACompute2_0 is the version of PCA model-learning you want when you care about how much each component matters. It does everything PCACompute_0 does - takes a dataset, computes the mean and the principal components - but it also hands you the eigenvalues: one number per component telling you how much variance that axis explains. That's the signal that lets you decide where to cut, rather than guessing a component count blind.
It's the same "learn the model" role in the PCA family, and it plays nicer in one specific situation: OpenCV's docs recommend the PCACompute2 family for the case where your number of samples is smaller than your number of dimensions - a common shape when your "samples" are a handful of image patches and your "dimensions" are tens of thousands of pixels. If that's your setup, skip PCACompute entirely and start here.
How it works
cv2.PCACompute2(data, mean, eigenvectors, eigenvalues, maxComponents) returns three arrays instead of two:
nparray_0- the mean of the datanparray_1- the eigenvectors (the principal axes)nparray_2- the eigenvalues (variance explained per component, sorted descending)
maxComponents caps how many components you keep. The mean + eigenvectors feed PCAProject_0/PCABackProject_0 exactly as they would from PCACompute; the eigenvalues are the new information, useful for eyeballing the variance curve or automating the cutoff.
The inputs that matter
data(NPARRAY) - training samples, one per row.mean(NPARRAY) - required by the wrapper (OpenCV can compute it from an empty array; this node can't express empty). Zero vector = skip centering.maxComponents(INT) - how many components to keep.eigenvectors,eigenvalues(NPARRAY, optional) - OpenCV out-parameters; leave them disconnected.
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, no downloads.
Common issues & troubleshooting
- Fewer samples than dimensions? That's precisely the case OpenCV points at
PCACompute2for - you're in the right place.PCACompute_0may misbehave or return unstable results there. - Shape errors:
dataandmeancolumn counts must match;(-215:Assertion failed)is the tell. - Batch rule:
batch_size==1only - useImageFromBatchifImage2Nparraycomplains.
Want the eigenvalues too, or working with a skinny dataset? This is the PCA node to reach for. The _1 twin is the identical UMat variant; _2/_3 are the same function driven by retainedVariance instead of maxComponents.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| data | NPARRAY | — | |
| mean | NPARRAY | — | |
| maxComponents | INT | — | |
| eigenvectorsopt | NPARRAY | — | |
| eigenvaluesopt | NPARRAY | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| nparray_0 | NPARRAY | — |
| nparray_1 | NPARRAY | — |
| nparray_2 | NPARRAY | — |