Nodes/opencv-comfyui/OpenCV PCABackProject_1
ComfyUI Node

OpenCV PCABackProject_1

The other half of the PCA round-trip (and a compression trick)

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

PCABackProject_1 and PCABackProject_0 are the same node. Literally the same signature - data, mean, eigenvectors in, nparray out - and the same call to cv2.PCABackProject. The _0/_1 split is just the pack's generator emitting one wrapper for OpenCV's MatLike type declaration and another for its UMat (OpenCL) declaration; Python calls both identically. Pick either. This article will assume you picked this one.

So what is back-projection, in practical terms? It's the reconstruction step that completes a PCA round-trip, and it doubles as a cheap denoising/compression trick. Project a high-dimensional thing into a handful of principal components, then back-project - what you get back is the best low-rank approximation of the original. Drop the weakest components and the reconstruction is smoother than the input. For image patches that means a soft, structure-preserving cleanup that runs in milliseconds and needs no model.

How it works

cv2.PCABackProject is the inverse of cv2.PCAProject: reconstructed = data @ eigenvectors + mean. The mean and eigenvectors come from PCACompute_0 / PCACompute2_0 - run one of those once on a dataset, keep the model, and reuse it across many samples.

The pipeline that makes sense in a graph:

PCACompute_0 (learn model)
   ├─ mean, eigenvectors → PCAProject_0 (compress)
   │                        └─ data → PCABackProject_1 (reconstruct)

Inputs: data (the coefficients to expand), mean, eigenvectors (the PCA model - reuse the ones that produced data), and optional result (an OpenCV out-parameter you can safely leave alone). Output: nparray, back in original space.

How to install it

Same as every node in the pack - 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 is the same wheel). No downloads.

Common issues & troubleshooting

  • Assertion failures are shape problems. data's column count must match eigenvectors' row count, and mean must be 1 row of the original dimension. OpenCV's error: (-215:Assertion failed) is the usual tell.
  • Batch rule: pack supports batch_size==1 only. Use ImageFromBatch (length=1) first if needed.
  • Keep the model consistent. If PCAProject_0 produced the coefficients, reconstruct with the same mean/eigenvectors, not a fresh PCACompute run - mismatch means drift.

For a "boring" utility node, the round-trip trick it enables is quietly powerful. Compress, clean, reconstruct - all deterministic, all in the graph.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
dataNPARRAY
meanNPARRAY
eigenvectorsNPARRAY
resultoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY