cv2.PCACompute (2/2)
Keep 99% of the variance instead of counting components
- data
- mean
- mean
- eigenvectors
Same function as cv2.PCACompute (1/2), same two outputs, one different input. Instead of "give me exactly K components", this variant asks OpenCV to keep as many components as it takes to explain a fraction of the total variance. 0.95 means "keep adding eigenvectors until I've got 95%". The count comes out as however many rows the eigenvectors happen to have.
Use one of the two nodes. They are the two overloads of the same cv2 call, not steps in a sequence.
Why you'd want this version
Because "2 components" is a guess and "95% of the variance" is a statement about your data. In practice: you've got a feature matrix out of CV HOG Features or CV Deep Features and you want to shrink it before a classifier, and you don't know a priori whether it collapses to four dimensions or forty. retainedVariance lets the data answer.
It's also the honest way to check whether dimension reduction is even worth it. Set it to 0.99, look at how many rows come back - CV Array Shape on the eigenvectors gives you height, and the shape output is the one to wire, because the socket is a plain NPARRAY and eyeballing a 40 x 3780 array is not a plan.
The trade-off is legibility: with a count you know exactly how wide your projected data will be, which matters if something downstream is expecting a fixed size. With a variance threshold the width is data-dependent, and it changes when the input changes. For a fixed pipeline, count. For exploring a new feature set, variance.
The inputs, and the one that's a trap
data - N x M, rows are samples, float32/float64. cv2 requires it; CV Cast Array gets you there.
mean - required, and it's the same trap as the counting variant. In cv2 this is an input/output argument: the computed mean is written out through it, and what you pass is what cv2 works around. The wrapper turns it into a required NPARRAY socket, so there's no "leave it empty" option like there is in Python, and a zero vector means "(variance about the origin)", not "(please compute the centroid)". Feed it a mean you already have, or just accept that you need to give it something and use the node's own mean output for everything downstream.
retainedVariance is required here, not optional, and it arrives at 0. A fraction between 0 and 1: 0.9, 0.95, 0.99. Zero is a degenerate ask - keep the components needed to explain none of the variance - so don't ship the default. It's a FLOAT with the pack's usual wide min/max bounds, which means nobody stops you typing 95 instead of 0.95; just get an error from cv2 instead of a surprise.
Outputs: mean and eigenvectors, both NPARRAY, eigenvectors ordered by decreasing eigenvalue. Wire both into PCAProject (or PCABackProject) so upstream and downstream agree on the centroid - mismatched mean is the classic way this pair silently produces nonsense.
What you do with the result
Project with PCAProject to get the reduced representation, and keep the mean and eigenvectors with the thing you trained on them. PCABackProject inverts it, which is how you get a reconstruction error - the number that tells you whether 0.95 was enough, or whether you're throwing away the signal you care about. Both directions live in this pack as raw wrappers, all NPARRAY in and out, so the round trip is one thread of nodes with no image conversion anywhere.
If you're feeding pixel coordinates rather than engineered features: CV Contour To Points turns a contour into N x 1 x 2, and CV Reshape Array flattens it to N x 2 for PCA. Those two nodes are the bridge between the pack's geometry world and its matrix world, and they're worth knowing before you reach for numpy.
Installing
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, recent ComfyUI (V3 node API - the pack's nodes are V3 definitions and won't load on an old frontend). PCACompute is core cv2, so this node survives even a non-contrib OpenCV - the rest of the pack won't. No models, no downloads; found under image/CV/low-level/cv2 P as cv2.PCACompute (2/2).
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 (2)
| Name | Type | Description |
|---|---|---|
| mean | NPARRAY | — |
| eigenvectors | NPARRAY | — |