Nodes/opencv-comfyui/OpenCV PCABackProject_0
ComfyUI Node

OpenCV PCABackProject_0

Reconstructing data (and images) from a few components

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV PCABackProject_0
  • data
  • mean
  • eigenvectors
  • result
  • nparray

If you've ever seen the "eigenfaces" demos - a face reconstructed from a handful of principal components, each one adding a little more fidelity - you've seen what PCABackProject_0 does. It's the inverse of PCAProject: it takes data that has been compressed into a low-dimensional PCA subspace and rebuilds it back into the original space. In this pack, that means you can genuinely do lossy PCA compression on image patches or feature vectors inside ComfyUI, or manipulate data while it's in a compact form and then expand it again.

Honest framing: this is real computer vision, not diffusion. You reach for it when you're porting a Python/OpenCV pipeline into a graph, or when you want deterministic, millisecond dimensionality tricks rather than a generative model. Which is exactly what this pack is for.

How it works

cv2.PCABackProject(data, mean, eigenvectors) performs the PCA inverse transformation: the input data (the projected coefficients, one row per sample) is multiplied back through eigenvectors, and mean is added back on. Roughly:

reconstructed = data @ eigenvectors + mean

It's the exact inverse of PCAProject's (data - mean) @ eigenvectors.T, so the two round-trip cleanly. mean and eigenvectors come straight from a PCACompute or PCACompute2 call - that's the natural pipeline:

  1. PCACompute_0 on your raw data learns mean + eigenvectors
  2. PCAProject_0 compresses a sample into a few coefficients
  3. (optional) tweak the coefficients, e.g. zero out the weak components
  4. PCABackProject_0 reconstructs - you've effectively low-pass filtered the data, which is a classic PCA denoise/compression move

The inputs that matter

  • data (NPARRAY) - the compressed coefficients to expand, one sample per row.
  • mean and eigenvectors (NPARRAY) - the PCA model from PCACompute_0/PCACompute2_0. Reuse the exact ones that produced data, or the reconstruction drifts.
  • result (NPARRAY, optional) - an out-parameter from OpenCV's signature; you can ignore it. The node always returns the reconstruction.

Output: nparray - same shape as data but back in the original feature space. Feed it to Nparrays2Image if your rows were flattened image patches.

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 says pip install opencv-python-contrib; same package, alias name). No model files.

Common issues & troubleshooting

  • Shape mismatch is the #1 error. data, mean, and eigenvectors must agree: columns of data must equal rows of eigenvectors, and mean must be 1 row with the original dimension count. If you get a (-215:Assertion failed) it's almost always this - check the OpenCV docs for PCABackProject's expected shapes.
  • Remember the pack's batch rule. Only batch_size==1; pull one frame with ImageFromBatch if Image2Nparray complains.
  • Don't mix _0 and _1 PCA nodes mid-pipeline. They're identical wrappers (MatLike vs UMat overloads), so mixing is harmless - but keep one consistent path so you're not confused about which model you're using.

The name is a mouthful, but the concept is one line: compress with PCAProject, reconstruct with PCABackProject. Everything else is just shapes.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
dataNPARRAY
meanNPARRAY
eigenvectorsNPARRAY
resultoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY