Search code examples
pythonopencvscipydct

Different outputs of DCT from OpenCV and Scipy.fftpack.dctn?


I am calculating DCT of grayscale image of size 32x32

  • Using OpenCV
def _dct(image):
    result = cv2.dct(np.float32(image)/255.0)
    return (result * 255.0)
  • Using Scipy.fftpack.dctn
dctn(image)

Output of both functions first using cv2.dct() then using scipy.fftpack.dctn() Output


Solution

  • The reason is of this behaviour is listed here https://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.dct.html

    Answer : dctn(image, norm = "ortho")

    Reason : In case of norm = "ortho" resulting DCT is multiplied with scaling factor f which results in same output as given by _dct using OpenCV.

    f = sqrt(1/4N) when k = 0 
        sqrt(1/2N) otherwise