Python 实现图像快速傅里叶变换和离散余弦变换 (2)

二维 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 运行结果

lena_dct

baboon_dct

circle_dct

avicii_dct

完整源码请见 GitHub 仓库

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspzwd.html