Search code examples
opencvimage-processingdct

how use Discrete cosine transform(DCT) in opencv


dct don't the conversion properly in opencv.

imf = np.float32(block)  
dct = cv2.dct(imf)

           [[154,123,123,123,123,123,123,136],
           [192,180,136,154,154,154,136,110],
           [254,198,154,154,180,154,123,123],
           [239,180,136,180,180,166,123,123],
           [180,154,136,167,166,149,136,136],
           [128,136,123,136,154,180,198,154],
           [123,105,110,149,136,136,180,166],
           [110,136,123,123,123,136,154,136]] 

this block of an image,when converting with code shown above

[162.3 ,40.6, 20.0...
[30.5 ,108.4...

this should be the result,

[1186.3 , 40.6, 20.0...
[30.5, 108.4 ....

but I found this Result. for sample block, https://www.math.cuhk.edu.hk/~lmlui/dct.pdf


Solution

  • The DCT is working fine. The difference between what you got and what you expect is because that particular example given actually does the DFT on M instead of on the original image, I. In this case, as the paper shows, M = I - 128. The only difference in your example is that you don't subtract off that piece, so the values are all larger. In a cosine or Fourier transform, the first coefficient (the "DC offset" as it is sometimes called) has a higher value because your image values are just greater. But that's why all the other coefficients are the same. If you take an image and you simply add some or subtract some from the entire image equally, the coefficients of the transform will be the same, except the very first one.

    From the standard definition of the DCT:

    DCT definition

    You can see here that for the first coefficient with k = 0, that inside the cosine function, you just get 0, and cos(0) = 1. Thus, X_0 as it's shown in this picture is just the sum of all the x_n values. Generally this value may be scaled by something relating to N so that it's something like an average. When doing so, it relates back to the X_0 term being a "DC offset" which you'll see described as the "mean value of the signal," or in other words, how far the signal is from 0. This is super useful to have as one of the cosine/Fourier transform coefficients as it then can completely describe a signal; all the other coefficients describe the frequency content and so they say nothing about how far the values are from 0, but the first coefficient, the DC offset, does tell you the shift!