0


Python图像处理【10】基于离散余弦变换的图像压缩

基于离散余弦变换的图像压缩

0. 前言

由于图像中相邻像素间的相关性引起的空间冗余、图像序列中不同帧之间存在相关性引起的时间冗余,因此我们需要对图像数据进行压缩。数据压缩的目的就是通过去除这些数据冗余来减少数据表示所占用的存储空间。随着大数据时代的到来,图像数据在质量提高的同时,其大小也变得越来越大,在存储、传输、处理时非常困难,因此图像数据的压缩就显得非常重要。
图像压缩是数据压缩技术在图像上的应用,利用图像压缩可以减少图像数据中的冗余信息,从而能够更加高效存储和传输数据。在本节中,我们首先介绍离散余弦变换 (

  1. Discrete Cosine Transform

,

  1. DCT

) 的基本原理,利用

  1. DCT

执行图像压缩操作,并实现图像

  1. JPEG

压缩。

1. 离散余弦变换基础

离散余弦变换 (

  1. Discrete Cosine Transform

,

  1. DCT

) 以不同频率振幅的余弦函数组合表示图像,而离散傅里叶变换 (

  1. Discrete Fourier Transform

,

  1. DFT

) 则以复杂指数的形式使用正弦和余弦函数。以下方程式显示了如何计算

  1. N
  2. ×
  3. N
  4. N×N
  5. N×N 图像
  6. f
  7. (
  8. x
  9. ,
  10. y
  11. )
  12. f(x,y)
  13. f(x,y)
  1. DCT

的第

  1. (
  2. i
  3. ,
  4. j
  5. )
  6. (i,j)
  7. (i,j) 项(系数):
  8. D
  9. (
  10. i
  11. ,
  12. j
  13. )
  14. =
  15. 1
  16. 2
  17. N
  18. C
  19. (
  20. i
  21. )
  22. C
  23. (
  24. j
  25. )
  26. x
  27. =
  28. 0
  29. N
  30. 1
  31. y
  32. =
  33. 0
  34. N
  35. 1
  36. f
  37. (
  38. x
  39. ,
  40. y
  41. )
  42. c
  43. o
  44. s
  45. [
  46. (
  47. 2
  48. x
  49. +
  50. 1
  51. )
  52. i
  53. π
  54. 2
  55. N
  56. ]
  57. c
  58. o
  59. s
  60. [
  61. (
  62. 2
  63. y
  64. +
  65. 1
  66. )
  67. i
  68. π
  69. 2
  70. N
  71. ]
  72. D(i,j)=\frac 1 {\sqrt{2N}}C(i)C(j)\sum ^{N-1} _{x=0}\sum ^{N-1} _{y=0}f(x,y)cos[\frac {(2x+1)i\pi}{2N}]cos[\frac {(2y+1)i\pi}{2N}]
  73. D(i,j)=2N1C(i)C(j)x=0N1y=0N1f(x,y)cos[2N(2x+1)iπ​]cos[2N(2y+1)iπ​]

其中

  1. C
  2. (
  3. u
  4. )
  5. =
  6. {
  7. 1
  8. 2
  9. u
  10. =
  11. 0
  12. 1
  13. u
  14. >
  15. 0
  16. C(u)=\left\{ \begin{array}{rcl} \frac 1 {\sqrt 2} & & {u=0}\\ 1 & & {u>0} \end{array} \right.
  17. C(u)={211​​u=0u>0​,对于块大小为
  18. 8
  19. ×
  20. 8
  21. 8\times8
  22. 8×8
  1. DCT

  1. N
  2. =
  3. 8
  4. N=8
  5. N=8

2. 基于离散余弦变换的图像压缩

  1. DCT

算法用途广泛,通常用于实现图像

  1. JPEG

压缩,在本节中,我们将学习如何使用

  1. Python

执行图像重建操作。

(1) 首先,导入所需库,关键在于从

  1. scipy.fftpack

模块导入所需的函数

  1. dct()

  1. idct()

  1. import scipy.fftpack as fp
  2. from skimage.io import imread
  3. from skimage.color import rgb2gray, gray2rgb
  4. from skimage.draw import rectangle_perimeter
  5. import numpy as np
  6. import matplotlib.pylab as plt
  7. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused importfrom mpl_toolkits.axes_grid1 import make_axes_locatable
  8. from matplotlib.ticker import LinearLocator, FormatStrFormatter
  9. from scipy.fftpack import dct, idct

(2) 使用

  1. scipy.fftpack

  1. dct()

  1. idct()

函数分别定义函数

  1. dct2()

  1. idct2()

以实现

  1. 2D-DCT

  1. IDCT

。其中,函数

  1. dct()

的调用方式如下,而

  1. idct()

函数的调用方式类似:

  1. scipy.fftpack.dct(x,type=2, n=None, axis=-1, norm=None, overwrite_x=False)

该返回任意类型序列x的离散余弦变换。

根据在前言中介绍的

  1. DCT

方程,并使用通过使用参数

  1. norm ='ortho'

应用归一化:

  1. defdct2(a):return dct(dct(a, axis=0, norm='ortho'), axis=1, norm='ortho')defidct2(a):return idct(idct(a, axis=0, norm='ortho'), axis=1, norm='ortho')

(3) 读取输入

  1. RGB

图像并将其转换为灰度。计算图像的

  1. 2D-DCT

,然后利用

  1. IDCT

使用前面定义的函数重建图像:

  1. im = rgb2gray(imread('1.png'))
  2. imF = dct2(im)
  3. im1 = idct2(imF)

(4) 检查重建的图像,观察其与原始图像之间的差别,查看其是否与原始图像相同,在这里使用函数

  1. np.callclose()

评估图像间的差距:

  1. print(np.allclose(im, im1))

(5) 使用

  1. matplotlib.pyplot

绘制原始图像和重建图像,得到结果如下所示:

  1. plt.figure(figsize=(10,5))
  2. plt.gray()
  3. plt.subplot(121), plt.imshow(im), plt.axis('off'), plt.title('original image', size=10)
  4. plt.subplot(122), plt.imshow(im1), plt.axis('off'), plt.title('reconstructed image (DCT+IDCT)', size=10)
  5. plt.tight_layout()
  6. plt.show()

图像重建
在以上结果图像中可以看到,重建后的图像与原始图像在视觉效果上类似,证明了可以使用

  1. DCT

进行图像压缩,使用

  1. IDCT

对压缩后的图像进行重建。

3. 图像 JPEG 压缩

在上一小节中,我们已经学习了如何实现

  1. 2D-DCT

  1. IDCT

,在本节中,我们将使用这两种技术执行图像压缩操作,完成

  1. JPEG

格式图像压缩。

3.1 JPEG 压缩原理

  1. JPEG

压缩基本操作步骤如下所示:

  • 将图像分割为 8x8 的图像块
  • 在每个图像块上(从左到右,从上到下)应用 DCT
  • 对每个图像块使用量化压缩
  • 将压缩后的图像块存储在较小的空间中,从而达到较高的压缩效果

下图显示了在

  1. 8x8

图像块的

  1. 64

  1. DCT

基函数,对于图像中的每个块,

  1. 64

  1. DCT

系数是基函数的线性组合以生成图像块系数:

DCT 系数

接下来,我们将实现简化版

  1. JPEG

压缩,以确保仅需少数

  1. DCT

系数可以表示图像,而不会导致图像质量有过多的视觉失真。在本节中,我们将直接在输入图像像素上计算

  1. 8x8

图像块的

  1. DCT

。此外,我们将使用全局阈值来清零大部分

  1. DCT

系数,而不是通过用量化矩阵(对于给定的压缩级别)分割

  1. DCT

矩阵来进行量化。最后,我们将跳过霍夫曼 (

  1. Huffman

) 编码,并存储(压缩)/读取(解压缩)图像。

3.2 JPEG 压缩实践

(1) 导入所需库,读取输入图像,并将图像分割为

  1. 8x8

  1. DCT

块:

  1. import scipy.fftpack as fp
  2. from skimage.io import imread
  3. from skimage.color import rgb2gray, gray2rgb
  4. from skimage.draw import rectangle_perimeter
  5. import numpy as np
  6. import matplotlib.pylab as plt
  7. from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused importfrom mpl_toolkits.axes_grid1 import make_axes_locatable
  8. from matplotlib.ticker import LinearLocator, FormatStrFormatter
  9. from scipy.fftpack import dct, idct
  10. defdct2(a):return dct(dct(a, axis=0, norm='ortho'), axis=1, norm='ortho')defidct2(a):return idct(idct(a, axis=0, norm='ortho'), axis=1, norm='ortho')
  11. im = rgb2gray(imread('1.png'))
  12. dct_coeffs = np.zeros(im.shape)for i inrange(0, im.shape[0],8):for j inrange(0, im.shape[1],8):
  13. dct_coeffs[i:(i+8),j:(j+8)]= dct2(im[i:(i+8),j:(j+8)])

(2) 从图像中提取一个图像块,并显示该块的

  1. DCT

系数。

  1. index =112
  2. plt.figure(figsize=(10,6))
  3. plt.gray()
  4. plt.subplot(121), plt.imshow(im[index:index+8,index:index+8]), plt.title("An 8x8 Image block", size=10)
  5. plt.subplot(122), plt.imshow(dct_coeffs[index:index+8,index:index+8], vmax= np.max(dct_coeffs)*0.01, vmin =0, extent=[0, np.pi, np.pi,0])
  6. plt.title("An 8x8 DCT block", size=10)
  7. plt.show()

输出结果如下所示:

图像块

(3) 使用全局阈值对

  1. DCT

系数进行阈值设置,将值小于阈值乘以最大系数值的所有

  1. DCT

系数置为

  1. 0

,打印应用阈值后剩余的非零

  1. DCT

系数的百分比。

  1. thresh =0.03
  2. dct_thresh = dct_coeffs *(abs(dct_coeffs)>(thresh*np.max(dct_coeffs)))
  3. percent_nonzeros = np.sum( dct_thresh !=0.0)/(im.shape[0]*im.shape[1])print("Keeping only {}% of the DCT coefficients".format(percent_nonzeros*100.0))# Keeping only 3.45875% of the DCT coefficients

从以上输出可以看出,应用阈值后,只有约

  1. 3.5

  1. DCT

系数为非零值。

(4) 可视化原始

  1. DCT

系数以及阈值处理后获得的系数:

  1. plt.figure(figsize=(12,7))
  2. plt.gray()
  3. plt.subplot(121), plt.imshow(dct_coeffs,cmap='gray',vmax = np.max(dct_coeffs)*0.01,vmin =0), plt.axis('off')
  4. plt.title("8x8 DCTs of the image", size=10)
  5. plt.subplot(122), plt.imshow(dct_thresh, vmax = np.max(dct_coeffs)*0.01, vmin =0), plt.axis('off')
  6. plt.title("Thresholded 8x8 DCTs of the image", size=10)
  7. plt.tight_layout()
  8. plt.show()

DCT 系数
(5) 最后,仅使用

  1. 3.5

非零

  1. DCT

系数,使用

  1. 8x8

  1. IDCT

重建压缩图像:

  1. im_out = np.zeros(im.shape)for i inrange(0, im.shape[0],8):for j inrange(0, im.shape[1],8):
  2. im_out[i:(i+8),j:(j+8)]= idct2( dct_thresh[i:(i+8),j:(j+8)])

(6) 绘制原始图像和

  1. DCT

压缩图像,并对比原始图像和重建(压缩)图像:

  1. plt.figure(figsize=(15,7))
  2. plt.gray()
  3. plt.subplot(121), plt.imshow(im), plt.axis('off'), plt.title('original image', size=10)
  4. plt.subplot(122), plt.imshow(im_out), plt.axis('off'), plt.title('DCT compressed image', size=10)
  5. plt.tight_layout()
  6. plt.show()

原始图像与压缩图像
从以上图像可以看到,仅通过有效地存储大约

  1. 3.5

  1. DCT

系数,我们便可以高图像质量的重建图像,而不会过多的降低图像质量上,从而实现较高压缩比,并计算

  1. MSE/PSNR

以进行定量测量图像间的差距。

小结

离散余弦变换 (

  1. Discrete Cosine Transform

,

  1. DCT

) 以不同频率振幅的余弦函数组合表示图像,而离散傅里叶变换 (

  1. Discrete Fourier Transform

,

  1. DFT

) 则以复杂指数的形式使用正弦和余弦函数。在实际应用中,

  1. DCT

算法用途广泛,通常用于实现图像

  1. JPEG

压缩,在本节中,我们学习了如何利用

  1. DCT

执行图像

  1. JPEG

压缩,用于更加高效存储和传输图像数据。

系列链接

Python图像处理【1】图像与视频处理基础
Python图像处理【2】探索Python图像处理库
Python图像处理【3】Python图像处理库应用
Python图像处理【4】图像线性变换
Python图像处理【5】图像扭曲/逆扭曲
Python图像处理【6】通过哈希查找重复和类似的图像
Python图像处理【7】采样、卷积与离散傅里叶变换
Python图像处理【8】使用低通滤波器模糊图像
Python图像处理【9】使用高通滤波器执行边缘检测


本文转载自: https://blog.csdn.net/qq_30167691/article/details/128434681
版权归原作者 AI technophile 所有, 如有侵权,请联系我们删除。

“Python图像处理【10】基于离散余弦变换的图像压缩”的评论:

还没有评论