实验验证离散余弦变换和简单压缩应用,显示对应的原图,dct图和恢复的图像
离散余弦变换压缩
离散余弦变换
import cv2
import numpy
as np
import matplotlib
.pyplot
as plt
img
=cv2
.imread
("lena.jpg",0)
img1
=img
.astype
(np
.float)
img_dct
=cv2
.dct
(img1
)
img_dct_log
=np
.log
(abs(img_dct
))
img_idct
=cv2
.idct
(img_dct
)
plt
.subplot
(141)
plt
.imshow
(img
,cmap
='gray')
plt
.title
('original')
plt
.axis
('off')
plt
.subplot
(142)
plt
.imshow
(img_dct_log
,cmap
='gray')
plt
.title
('dct')
plt
.axis
('off')
plt
.subplot
(143)
plt
.imshow
(img_idct
,cmap
='gray')
plt
.title
('idct')
plt
.axis
('off')
plt
.show
()
压缩
import cv2
import numpy
as np
import matplotlib
.pyplot
as plt
img
=cv2
.imread
("lena.jpg",0)
img1
=img
.astype
(np
.float32
)
img_dct
=cv2
.dct
(img1
)
(rows
,cols
)=img_dct
.shape
for i
in range(0,rows
):
for j
in range(0,cols
):
if i
>60 or j
>60:
img_dct
[i
,j
]=0
img_dct_log
=np
.log
(abs(img_dct
))
img_dct
=cv2
.idct
(img_dct
)
plt
.subplot
(141)
plt
.imshow
(img
,cmap
='gray')
plt
.title
('original')
plt
.axis
('off')
plt
.subplot
(142)
plt
.imshow
(img_dct_log
,cmap
='gray')
plt
.title
('dct')
plt
.axis
('off')
plt
.subplot
(143)
plt
.imshow
(img_dct
,cmap
='gray')
plt
.title
('idct')
plt
.axis
('off')
plt
.show
()
转载请注明原文地址: https://lol.8miu.com/read-21593.html