我想编写一个函数,它获取两个图像参考和编码并评估每个组件(R、G、B、Y、Cb、Cr)的 (R)MSE 和 PSNR。为此,我提取所有组件,然后转换 RGB -> YCbCr。我想在不使用内置函数的情况下计算 (R)MSE 和 PSNR。
import os, sys, subprocess, csv, datetime
from PIL import Image
############ Functions Definitions ############
# Extracts the values of the R, G, B components of each pixel in the input file and calculates the Y, Cb, Cr components returning a dictionary having a key tuple with the coordinates of
the pixes and values the values of each R, G, B, Y, Cb, Cr components
def rgb_calc(ref_file):
img = Image.open(ref_file)
width, height = img.size
print(width)
print(height)
rgb_dict = {}
for x in range (width):
for y in range(height):
r, g, b = img.load()[x, y]
lum = 0.299 * r + 0.587 * g + 0.114 * b
cb = 128 - 0.168736 * r - 0.331264 * g + 0.5 * b
cr = 128 + 0.5 * r - 0.418688 * g - 0.081312 * b
print("X {} Y {} R {} G {} B {} Y {} Cb {} Cr {}".format(x, y, r, g, b, lum, cb, cr))
rgb_dict[(x, y)] = (r, g, b, lum, cb, cr)
return rgb_dict
############ MAIN FUNCTION ############
r_img = sys.argv[1]
p_img = sys.argv[2]
ref_img = Image.open(r_img)
proc_img = Image.open(p_img)
resolution_ref = ref_img.size
resolution_proc = proc_img.size
if resolution_ref == resolution_proc:
ycbcr_ref = rgb_calc(r_img)
ycbcr_proc = rgb_calc(proc_img)
else:
exit(0)
我想编写一个新函数并最终输出每个组件的平均 PSNR 和整个图像的平均值。
有没有办法加快我的进程?
目前,img.load()
每 8Mpx 图像大约需要 10-11 秒,而字典的创建需要额外 6 秒。因此,仅提取这些值并创建两个字典已经花费了 32 秒。