在此记录一下自己课程实验的结果。
实验要求:
将读入内存的图像以不同的压缩率保存为多个JPEG文件,每一个文件对应一个压缩率。
将读入内存的图像以不同的压缩率保存为多个JPEG文件,每一个文件对应一个压缩率。
下面代码段为读取JPG图像,以不同的图片质量保存为JPEG格式图像。其原本图片的默认值为95。
import cv2
PATH = 'C:/Users/lishu/Desktop/code/PatternRecognition/PatternRecognition/img/'
if __name__ == '__main__':
img = cv2.imread(PATH+'test01.jpg') #读取图像
print(img)
# 设值改变图像可缩放
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
#将JPG文件保存为不同图像质量的图像
cv2.imwrite(PATH+'QUALITY100.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),100])
cv2.imwrite(PATH+'QUALITY90.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),90])
cv2.imwrite(PATH+'QUALITY80.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),80])
cv2.imwrite(PATH+'QUALITY70.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),70])
cv2.imwrite(PATH+'QUALITY60.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),60])
cv2.imwrite(PATH+'QUALITY50.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),50])
cv2.imwrite(PATH+'QUALITY40.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),40])
cv2.imwrite(PATH+'QUALITY30.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),30])
cv2.imwrite(PATH+'QUALITY20.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),20])
cv2.imwrite(PATH+'QUALITY10.JPEG', img, [int(cv2.IMWRITE_JPEG_QUALITY),10])
# cv2.imshow('image', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
下面代码段为读取JPG图像,以不同的图片压缩比保存为PNG格式图像。其原本图片的默认值为10。
import cv2
PATH = 'C:/Users/lishu/Desktop/code/PatternRecognition/PatternRecognition/img/'
if __name__ == '__main__':
img = cv2.imread(PATH+'test01.jpg') #读取图像
print(img)
# 设值改变图像可缩放
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
#将JPG文件保存为不同图像质量的图像
cv2.imwrite(PATH+'compression9.PNG', img, [int(cv2.IMWRITE_PNG_COMPRESSION),9])
cv2.imwrite(PATH+'compression8.PNG', img, [int(cv2.IMWRITE_PNG_COMPRESSION),8])
cv2.imwrite(PATH+'compression7.PNG', img, [int(cv2.IMWRITE_PNG_COMPRESSION),7])
cv2.imwrite(PATH+'compression6.PNG', img, [int(cv2.IMWRITE_PNG_COMPRESSION),6])
cv2.imwrite(PATH+'compression5.PNG', img, [int(cv2.IMWRITE_PNG_COMPRESSION),5])
cv2.imwrite(PATH+'compression4.PNG', img, [int(cv2.IMWRITE_PNG_COMPRESSION),4])
cv2.imwrite(PATH+'compression3.PNG', img, [int(cv2.IMWRITE_PNG_COMPRESSION),3])
cv2.imwrite(PATH+'compression2.PNG', img, [int(cv2.IMWRITE_PNG_COMPRESSION),2])
cv2.imwrite(PATH+'compression1.PNG', img, [int(cv2.IMWRITE_PNG_COMPRESSION),1])
# cv2.imshow('image', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
图片创建完成后,进行编码误差分析,并作出distortion-rate分析图像。
对于每个图像,其rate的如下:
rate = 文件大小(Byte) / 像素总数
distortion计算方法如下:
对于生成图: 计算其每个像素点RGB和原图像素点RGB的绝对值之和,记为total 得到其像素点*3 计RGB总数 记为size distortion = total / size
代码如下:
JPEG:
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
DPATH = 'C:/Users/lishu/Desktop/code/PatternRecognition/experience1/img/'
if __name__ == '__main__':
path95 = DPATH+'test01.jpg'
pathList = []
for i in range(10):
pathList.append(DPATH+'QUALITY'+ str(i*10+10) +'.JPEG')
length = len(pathList)
img95 = cv2.imread(path95) #读取图像
imgList = []
for i in range(length):
imgList.append(cv2.imread(DPATH+'QUALITY'+ str(i*10+10) +'.JPEG'))
rateList = []
for i in range(length):
rateList.append(os.path.getsize(pathList[i]) * 8 / (imgList[i].size / 3))
distortionList = []
a = img95.astype(np.float32)
for k in range(length):
b = imgList[k].astype(np.float32)
c = np.maximum(a - b, b - a)
total = np.sum(c)
distortionList.append(total / img95.size)
print(rateList)
print(distortionList)
plt.plot(rateList, distortionList)
plt.xlabel('rate') # 横坐标轴的标题
plt.ylabel('distortion') # 纵坐标轴的标题
plt.show()
PNG:
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
DPATH = 'C:/Users/lishu/Desktop/code/PatternRecognition/experience1/img/'
if __name__ == '__main__':
path95 = DPATH+'test01.jpg'
pathList = []
for i in range(9):
pathList.append(DPATH+'compression'+ str(i+1) +'.PNG')
length = len(pathList)
img95 = cv2.imread(path95) #读取图像
imgList = []
for i in range(length):
imgList.append(cv2.imread(DPATH+'compression'+ str(i+1) +'.PNG'))
rateList = []
for i in range(length):
rateList.append(os.path.getsize(pathList[i]) * 8 / (imgList[i].size / 3))
distortionList = []
a = img95.astype(np.float32)
for k in range(length):
b = imgList[k].astype(np.float32)
total = np.sum(a-b)
distortionList.append(total / img95.size)
print(rateList)
print(distortionList)
plt.plot(rateList, distortionList)
plt.xlabel('rate') # 横坐标轴的标题
plt.ylabel('distortion') # 纵坐标轴的标题
plt.show()