• 主页
  • 相册
  • 随笔
  • 目录
  • 存档
Total 244
Search AboutMe

  • 主页
  • 相册
  • 随笔
  • 目录
  • 存档

实验:图像处理:pnsr~q曲线

2019-10-13

1. python图像处理:psnr~q曲线绘制

本文实现jpeg压缩量化因子,与JPEG压缩前后图像峰值信噪比的函数关系

  • psnr:Peak Signal to Noise Ratio,峰值信噪比
  • q:quality ,量化因子

说明:此处q值不确定为JPEG压缩中的量化因子(quality factor)

2. 原理

PSNR

PSNR is most commonly used to measure the quality of reconstruction of lossy compression codecs (e.g., for image compression). The signal in this case is the original data, and the noise is the error introduced by compression. When comparing compression codecs, PSNR is an approximation to human perception of reconstruction quality.

3. 代码

  1. 包

    1
    2
    3
    4
    import math
    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
  2. psnr,轮子。可能出现的问题:Nonetype——图片没有正确读取

    1
    2
    3
    4
    5
    6
    def psnr(img1, img2):
    mse = np.mean((img1/255. - img2/255.) ** 2)
    if mse < 1.0e-10:
    return 100
    PIXEL_MAX = 1
    return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
  3. 对图像进行JPEG压缩,控制压缩的质量(cv2.IMWRITE_JPEG_QUALITY),先编码后解码得到压缩后图像

    1
    2
    3
    4
    5
    6
    def compresss(im1, i):
    encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), i]
    _, im2 = cv2.imencode('.jpg', im1, encode_param)
    im2 = cv2.imdecode(im2, 1)
    #cv2.imwrite(str(i).join(".jpg"),im2)
    return im2
  4. 曲线

    1
    2
    3
    4
    5
    6
    7
    8
    def curve(P, Q):
    #plt.figure(figsize=(10.0,6.0))
    plt.title("PSNR~Q")
    plt.ylabel("PSNR")
    plt.xlabel("Q")
    plt.plot(Q, P)
    plt.savefig("result.png")
    plt.show()

4. 结果

## 其他

比较一下不同量化因子下图片长相

  • q=1(注意q的范围是0-100,默认95),出现明显色块和色彩失真

  • q=91,清晰度上的损失

  • 以及附上原图

(话说这张胶片还挺好看的~)

5. 参考资料

Peak signal-to-noise ratio - Wikipedia

  • Lab
  • Computer Graphics
  • Lab
正则式到dfa最小化实现
VirtualBox网络攻防环境搭建
© 2024 何决云 载入天数...