加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
dct_2d.py 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
Shuai_Yan 提交于 2020-04-16 17:41 . Added some notes
from scipy.fftpack import dct, idct
def end_T(tensor):
"""
:transpose the last two axes
:param tensor:
:return:
"""
axes = list(range(tensor.ndim))
axes[-1], axes[-2] = axes[-2], axes[-1]
tensor = tensor.transpose(axes)
return tensor
def dct_2d_forward(block):
"""
:forward 2d Discrete Cosine Transformation
:param tensor:
:return:
"""
block = end_T(block)
block = dct(block, norm='ortho')
block = end_T(block)
block = dct(block, norm='ortho')
return block
def dct_2d_reverse(block):
"""
:reverse 2d Discrete Cosine Transformation
:param tensor:
:return:
"""
block = end_T(block)
block = idct(block, norm='ortho')
block = end_T(block)
block = idct(block, norm='ortho')
return block
if __name__ == '__main__':
import cv2
import numpy as np
im = cv2.imread('Cameraman256.png', cv2.IMREAD_GRAYSCALE)
im = im.astype(np.float64)
fre_im = dct_2d_forward(im)
im_ = dct_2d_reverse(fre_im)
im_ = im_.astype(np.uint8)
diff = np.abs(im - im_)
print(np.max(diff))
print(np.sum(diff))
cv2.imshow('im_', im_)
cv2.waitKey()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化