Nodes/opencv-comfyui/OpenCV PCACompute_0
ComfyUI Node

OpenCV PCACompute_0

The node that learns the PCA model everything else depends on

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV PCACompute_0
  • data
  • mean
  • eigenvectors
  • nparray_0
  • nparray_1
maxComponents

PCACompute_0 is where the PCA pipeline in this pack starts. PCAProject and PCABackProject are the workhorses, but they're nothing without a model - and PCACompute is what learns that model: it takes a dataset, finds its mean and its principal components (the directions of greatest variance), and hands you both. Once you have them, you can project any sample into that subspace, reconstruct it, or compare things by a handful of numbers.

Be honest about when you'd use this: it's for people porting real computer-vision logic into ComfyUI - eigenfaces-style analysis, feature compression, PCA denoising - not for the typical text-to-image flow. If that's you, great; if it isn't, this node is likely overkill. But if you're in the first camp, this is the foundation node of the whole PCA family.

How it works

cv2.PCACompute(data, mean, eigenvectors, maxComponents) performs principal component analysis on data (one sample per row) and returns (mean, eigenvectors). maxComponents caps how many principal components you keep - fewer components means a smaller, lossier subspace; more means closer to the full data. mean and eigenvectors are exactly what PCAProject and PCABackProject consume, so the natural flow is:

PCACompute_0 → mean, eigenvectors → PCAProject_0 (encode) → PCABackProject_0 (decode)

The inputs that matter

  • data (NPARRAY) - your training samples, one per row. Flattened image patches work fine.
  • mean (NPARRAY) - here's the dragon: raw OpenCV lets you pass an empty mean and computes it for you, but this generated wrapper makes it a required input. A zero vector is an acceptable stand-in if you don't need centering; otherwise compute the real mean first.
  • maxComponents (INT) - how many principal components to keep. Start modest (tens, not thousands).
  • eigenvectors (NPARRAY, optional) - an OpenCV out-parameter; you can leave it disconnected.

Outputs: nparray_0 = the computed mean, nparray_1 = the eigenvectors.

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 wheel). No models, no keys.

Common issues & troubleshooting

  • PCACompute wants more samples than dimensions for clean results. If your samples are short and wide, PCACompute2 handles the row/column case more gracefully and also returns eigenvalues - reach for _0 of that one instead.
  • Shape errors again. data and mean must share the same column count; (-215:Assertion failed) is the symptom.
  • Batch rule: the pack only supports batch_size==1. Pull a single frame with ImageFromBatch if Image2Nparray complains.

If you've only got one node to understand in this family, make it this one - everything else in the PCA set is a consumer of what it produces.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
dataNPARRAY
meanNPARRAY
maxComponentsINT
eigenvectorsoptNPARRAY

Outputs (2)

NameTypeDescription
nparray_0NPARRAY
nparray_1NPARRAY