OpenCV dct_0
The Discrete Cosine Transform, or How JPEG-Thinking Leaks Into ComfyUI
- src
- dst
- nparray
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 thatcv2.dctoperates per-channel on float arrays; the README's usual flow isImage2Nparray→ convert tofloat32→ DCT.flags(INT) -0for the forward transform. Use1(cv2.DCT_INVERSE) to go back,3(DCT_INVERSE | DCT_SCALE) for a properly scaled inverse, and4(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.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| src | NPARRAY | — | |
| flags | INT | — | |
| dstopt | NPARRAY | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |