cv2.PCACompute2 (2/2)
Eigenvalues, and let the variance pick the component count
- data
- mean
- mean
- eigenvectors
- eigenvalues
The last of the four PCA wrappers in this pack: PCACompute2 (which gives you eigenvalues) driven by retainedVariance (which lets the data pick how many components to keep) instead of a fixed count. Three outputs, three inputs, no optional widgets - everything is required, including the fraction.
If you've read the other three, you can stop here: use this one when you want to know the magnitudes and you don't want to guess a component count.
What each input does
data - the matrix, N x M, rows are samples, float32/float64. cv2 asserts on the dtype, so CV Cast Array is usually the node just upstream. Row/column layout is not negotiable.
mean - the centroid, as an NPARRAY, and required. This is an in/out argument in cv2: the mean is computed and written out through it, and the wrapper makes that a socket rather than a hidden detail, so you can't leave it empty the way plain Python lets you. Passing zeros asks about variance around the origin instead of around the centroid; passing the mean from the node that generated your features is the correct move, and using this node's own mean output downstream is the rule that keeps you out of trouble.
retainedVariance - a fraction, 0 to 1, and it arrives at 0, which is not a sensible value to ship. 0.9 or 0.95 are the usual asks. Over 1 and cv2 complains; 95 (meaning percent) rather than 0.95 will not do what you meant.
What you get out
mean, eigenvectors (K x M - K decided by the variance threshold), and eigenvalues (K values, one per component, same order as the rows). The eigenvalues are your diagnostic: if the first one is 95% of the total, you're looking at data that's essentially one-dimensional and the whole reduction was free. If the first six are all similar, you have six roughly equal directions and "PCA didn't help" is the finding.
That last case is common with image-derived features - CV HOG Features and CV Deep Features both produce vectors where variance is spread across a lot of directions, which is why a fixed component count plus a variance threshold plus a look at the eigenvalues is the responsible order to explore a new feature matrix in.
Downstream: PCAProject (feed it this node's mean and eigenvectors, then the data) for the reduced representation, PCABackProject for the reverse, and CV Array Shape when you want to know exactly how many components the threshold chose. Inspect CV Data if you want to see the numbers rather than trust them.
Install
ComfyUI Manager → search ComfyUI CV → Install → restart. Manual:
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"
Python ≥ 3.12, a recent ComfyUI (the pack's nodes are V3 API definitions), and nothing else - the only declared dependency is the OpenCV contrib headless wheel, and this node needs no model files. Found under image/CV/low-level/cv2 P as cv2.PCACompute2 (2/2); the search box matches display names, so typing "PCA" gets you all four variants at once.
The thing to remember about the family
Four nodes, two cv2 functions, two stopping rules. PCACompute = mean + eigenvectors. PCACompute2 = plus eigenvalues. (1/2) = count components; (2/2) = variance fraction. And in all four, mean is an input you must supply and an output you should reuse. Wire the wrong mean and PCA still runs happily - it just answers a different question, which is the failure mode that costs you an evening.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| data | NPARRAY | - - - A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here. | |
| mean | NPARRAY | - - - A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here. | |
| retainedVariance | FLOAT | 0.0000-1e+38–1e+38 | - - - |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| mean | NPARRAY | — |
| eigenvectors | NPARRAY | — |
| eigenvalues | NPARRAY | — |