Search code examples
pythonopencvimage-processingscipydct

How do I apply a DCT to an image in Python?


I want to apply a Discrete Cosine Transform (as well as the inverse) to an image in Python and I'm wondering what is the best way to do it and how. I've looked at PIL and OpenCV but I still don't understand how to use it.


Solution

  • From OpenCV:

    DCT(src, dst, flags) → None
    
        Performs a forward or inverse Discrete Cosine transform of a 1D or 2D 
        floating-point array.
    
        Parameters: 
    
            src (CvArr) – Source array, real 1D or 2D array
            dst (CvArr) – Destination array of the same size and same type as the source
            flags (int) –
    
            Transformation flags, a combination of the following values
                CV_DXT_FORWARD do a forward 1D or 2D transform.
                CV_DXT_INVERSE do an inverse 1D or 2D transform.
                CV_DXT_ROWS do a forward or inverse transform of every individual row of 
    the input matrix. This flag allows user to transform multiple vectors simultaneously 
    and can be used to decrease the overhead (which is sometimes several times larger 
    than the processing itself), to do 3D and higher-dimensional transforms and so forth.
    

    Here is an example of it being used.

    The DCT is also available in scipy.fftpack.