Nodes/ComfyUI CV/cv2.PCACompute2 (1/2)
ComfyUI Node

cv2.PCACompute2 (1/2)

PCA with the eigenvalues attached

By bmad4ever·Created 3 months ago·Updated 14 days ago· 1
cv2.PCACompute2 (1/2)
  • data
  • mean
  • mean
  • eigenvectors
  • eigenvalues
◄maxComponents0►

The 2 on the end is not a version number. cv2.PCACompute2 is cv2.PCACompute with one extra output: it also hands you the eigenvalues, i.e. how much variance each of those eigenvectors actually carries. Same inputs, same call shape, third output.

That third output is the reason this node exists in practice. PCACompute's eigenvectors come sorted by decreasing variance, but they don't tell you the magnitudes - and "the first component carries 99% of the spread" and "the first three components are all roughly equal" are completely different situations that look identical when you only have the vectors. If you're looking at a point cloud to decide how linear, planar, or blobby it is (that ratio is the classic shape descriptor), you need the eigenvalues. Without them you're guessing.

Inputs

data - N x M NPARRAY, N samples per row, float32 or float64 (CV Cast Array if not). cv2's layout expectations don't bend: rows are samples, and a transposed matrix gives you a perfectly valid, perfectly unrelated answer.

mean - required NPARRAY, and the same in/out quirk as the rest of the family: in cv2 the mean is written out through this argument, and the wrapper makes it a socket you have to fill. What you pass is the centroid cv2 works around, so zeros mean "variance about the origin" rather than "compute it for me". Practically: feed the mean you already have, and use this node's mean output for anything downstream.

maxComponents - optional, advanced, preset to 0, which is OpenCV's "keep every component". Set it to 2 for a planar fit, 3 for a volumetric one, and remember that this is where the classic point-cloud shape test happens: λ1 ≈ λ2 ≈ λ3 is noise/scatter, one big eigenvalue is a line, two big ones is a plane.

Outputs

Three NPARRAYs: mean, eigenvectors (K x M), and eigenvalues (K x 1, or K elements). The eigenvalues are in the same order as the eigenvectors, so eigenvalues[0] belongs to eigenvectors[0].

The (2/2) sibling node replaces maxComponents with a retainedVariance fraction, the same way the PCACompute pair splits. Run one, not both.

Where this is genuinely useful in a ComfyUI graph: the pack has a lot of ways to make point clouds (CV Depth to 3D Points, CV Triangulate Points (Two-View), CV Detect Features → CV Match Features → CV Find Homography), and a lot of curated nodes that consume them, but "is this cloud a line, a plane, or mush?" is analysis, not rendering, and it isn't in the curated set. PCACompute2 plus CV Array Statistic on the eigenvalues answers it in three nodes. For 2D blobs the same idea is baked into CV Shape Moments and CV Region Properties - check those first, they've already done the plumbing.

A quiet caveat about the two-output shape: whatever you project with has to use the same mean and the same eigenvectors. PCAProject takes all three as inputs for that reason, and mixing a mean from one dataset with eigenvectors from another is the standard way to get a numerically valid, semantically meaningless result.

Installing

ComfyUI Manager → search ComfyUI CV → Install → restart, or:

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 and a recent ComfyUI: the pack is built on the V3 node API and its raw wrappers are generated at import time from your installed OpenCV's function list. PCACompute2 is core cv2, present in every wheel including the headless one. No model downloads anywhere in this node.

Categoryimage/CV/low-level/cv2 P

Inputs (3)

NameTypeDefaultDescription
dataNPARRAY - - - A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here.
meanNPARRAY - - - A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here.
maxComponentsoptINT0-2147483648–2147483647 - - - Preset to the OpenCV default (0).

Outputs (3)

NameTypeDescription
meanNPARRAY—
eigenvectorsNPARRAY—
eigenvaluesNPARRAY—