dct
import { dct } from '@banou/opencv-wasm'Use after await initOpenCV(). See the initialization and named imports guide.
Performs a forward or inverse discrete Cosine transform of 1D or 2D array.
The function cv::dct performs a forward or inverse discrete Cosine transform (DCT) of a 1D or 2D floating-point array:
- Forward Cosine transform of a 1D vector of N elements:
Y = C^{(N)} \cdot X
where
C^{(N)}_{jk}= \sqrt{\alpha_j/N} \cos \left ( \frac{\pi(2k+1)j}{2N} \right )
and
`\alpha_0=1`, `\alpha_j=2` for *j \> 0*.
- Inverse Cosine transform of a 1D vector of N elements:
X = \left (C^{(N)} \right )^{-1} \cdot Y = \left (C^{(N)} \right )^T \cdot Y
(since `C^{(N)}` is an orthogonal matrix, `C^{(N)} \cdot \left(C^{(N)}\right)^T = I` )
- Forward 2D Cosine transform of M x N matrix:
Y = C^{(N)} \cdot X \cdot \left (C^{(N)} \right )^T
- Inverse 2D Cosine transform of M x N matrix:
X = \left (C^{(N)} \right )^T \cdot X \cdot C^{(N)}
The function chooses the mode of operation by looking at the flags and size of the input array:
- If (flags & #DCT_INVERSE) == 0, the function does a forward 1D or 2D transform. Otherwise, it is an inverse 1D or 2D transform.
- If (flags & #DCT_ROWS) != 0, the function performs a 1D transform of each row.
- If the array is a single column or a single row, the function performs a 1D transform.
- If none of the above is true, the function performs a 2D transform.
Note: Currently dct supports even-size arrays (2, 4, 6 ...). For data analysis and approximation, you can pad the array when necessary. Also, the function performance depends very much, and not monotonically, on the array size (see getOptimalDFTSize ). In the current implementation DCT of a vector of size N is calculated via DFT of a vector of size N/2 . Thus, the optimal DCT size N1 >= N can be calculated as:
size_t getOptimalDCTSize(size_t N) { return 2*getOptimalDFTSize((N+1)/2); }
N1 = getOptimalDCTSize(N);
See: dft, getOptimalDFTSize, idct
dct(src: Mat, dst: Mat, flags: number): void;srcinput floating-point array.
dstOutput destination, filled by the native operation. output array of the same size and type as src .
flagstransformation flags as a combination of cv::DftFlags (DCT_*)
These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.