OpenCV PCAProject_0
Squeezing high-dimensional data (or images) into a few numbers
- data
- mean
- eigenvectors
- result
- nparray
Principal Component Analysis is the workhorse of "shrink the data, keep the variance." PCAProject_0 is the node that does the shrinking: it takes your data and projects it down onto a PCA subspace learned by PCACompute, so a 1024-dimensional feature vector (or a flattened image patch) becomes, say, 20 coordinates that still capture most of what made it distinct. It's the "encode" half of the PCA encoder/decoder pair, with PCABackProject as the decode.
Why would you do this inside ComfyUI? A few honest reasons: you're porting a Python pipeline that uses PCA (eigenfaces-style recognition, feature compression before clustering or matching), or you want to compare samples cheaply by distance in a low-dimensional space. It's also the prerequisite for the classic PCA denoise - project, zero out weak coefficients, back-project. All deterministic, all CPU-side, no diffusion pass needed. That's the appeal.
How it works
cv2.PCAProject(data, mean, eigenvectors) computes, per row:
result = (data - mean) @ eigenvectors.T
The mean centers the data, the eigenvectors rotate it onto the principal axes, and you keep only as many columns as there are eigenvectors. The output has the same number of rows as data and one column per eigenvector. mean and eigenvectors come from PCACompute_0 (which returns exactly those two arrays) or PCACompute2_0.
The inputs that matter
data(NPARRAY) - one sample per row; e.g. rows of flattened image patches or feature vectors.meanandeigenvectors(NPARRAY) - the PCA model. Feed them from aPCACompute_0/PCACompute2_0output so the same model stays consistent across your whole pipeline.result(NPARRAY, optional) - an OpenCV out-parameter; you can ignore it.
Output: nparray - the projected coefficients. Wire it to PCABackProject_0 to reconstruct, or compare rows directly.
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 (README's pip install opencv-python-contrib - same package). No model files, no keys.
Common issues & troubleshooting
- Column count is eigenvector count.
data's columns must equaleigenvectors' rows. Mix up the model and you get an OpenCV(-215:Assertion failed)- the shapes are the whole game here. - The wrapper forces a
meaninput. In raw OpenCV you can pass an empty mean and let PCA compute it; this generated node makes it a requiredNPARRAY. On a first pass, feeding a zero vector works as a stand-in if you're willing to skip mean-centering (results differ slightly), otherwise compute the mean first. It's one of those "expect dragons" edges the author warns about. - Batch rule:
batch_size==1only - grab one frame withImageFromBatchbefore converting.
PCA is one of those things that's 80% "set up the model correctly" and 20% everything else. Get mean + eigenvectors from a single PCACompute run, reuse them everywhere, and this node is genuinely useful.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| data | NPARRAY | — | |
| mean | NPARRAY | — | |
| eigenvectors | NPARRAY | — | |
| resultopt | NPARRAY | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |