二维 DCT
def dct2(img): h, w = img.shape if ((h-1) & h) or ((w-1) & w): print('Image size not a power of 2') return img img = normalize(img) res = np.zeros([h, w], 'complex128') for i in range(h): res[i, :] = fft(np.concatenate([img[i, :], np.zeros(w)]))[:w] res[i, :] = np.real(res[i, :]) * np.sqrt(2 / w) res[i, 0] /= np.sqrt(2) for j in range(w): res[:, j] = fft(np.concatenate([res[:, j], np.zeros(h)]))[:h] res[:, j] = np.real(res[:, j]) * np.sqrt(2 / h) res[0, j] /= np.sqrt(2) return res 运行结果完整源码请见 GitHub 仓库