Nodes/opencv-comfyui/OpenCV dct_0
ComfyUI Node

OpenCV dct_0

The Discrete Cosine Transform, or How JPEG-Thinking Leaks Into ComfyUI

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV dct_0
  • src
  • dst
  • nparray
flags

OpenCV dct_0 computes the Discrete Cosine Transform of an nparray - the same frequency transform JPEG uses, applied to your image as a whole. If you've never needed a DCT, it's not obvious why you'd want one in a ComfyUI graph, so let's get concrete: it's how you do frequency-domain work, which is the classic route to denoising (chop the high-frequency coefficients, inverse-transform, enjoy a cleaner image), image compression-ish experiments, perceptual hashing, and spectral analysis of a frame.

It's part of geroldmeisinger/opencv-comfyui, the auto-generated pack wrapping ~635 cv2 functions. Straight cv2.dct(src, dst, flags) wrapper; dct_0 and dct_1 are the usual MatLike/UMat overload twins and behave identically.

How it works

The DCT decomposes a signal into a sum of cosine waves of increasing frequency. The output layout matters: the DC term (overall brightness) lands in the top-left corner, and moving right/down goes to higher frequencies. For a typical image, almost all the energy sits in the top-left - which is exactly why JPEG keeps low frequencies and discards the rest.

  • src (NPARRAY) - your input. Note that cv2.dct operates per-channel on float arrays; the README's usual flow is Image2Nparray → convert to float32 → DCT.
  • flags (INT) - 0 for the forward transform. Use 1 (cv2.DCT_INVERSE) to go back, 3 (DCT_INVERSE | DCT_SCALE) for a properly scaled inverse, and 4 (DCT_ROWS) to transform each row independently.
  • dst (NPARRAY, optional) - out-parameter; skip it.
  • nparray (NPARRAY, output) - the coefficient array.

The trap

The output is not an image. Coefficients are floats, often spanning huge ranges, and the top-left corner dominates. The pack README's Nparrays2Image error - 'NoneType' object has no attribute 'shape' - is the classic endgame here: people feed DCT output straight into the image converter and get garbage. If you want to see the transform, normalize the magnitude (e.g., log-scale) before conversion. If you're doing denoising, you threshold the coefficients, then run dct again with flags=3 to rebuild the image.

Install

ComfyUI Manager (search opencv-comfyui) or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python

Restart ComfyUI. No models - this is pure math on numpy arrays. Use opencv-contrib-python (the README's opencv-python-contrib is the wrong name), and watch for the guidedFilter import error if you have conflicting OpenCV installs; the README links the known fix.

One more mechanical gotcha: cv2.dct needs a float32/float64 array, not the uint8 you get out of Image2Nparray, and it wants the batch trimmed to batch_size==1 first. If you hit a type assertion, convert to float before feeding it in.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
srcNPARRAY
flagsINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY