PSNR计算结果不正确 [英] Incorrect results for PSNR calculation
问题描述
I downloaded two images (original uncompressed PNG and Q = 90
compressed JPG, reported PSNR is 45.53 dB
) from Wikipedia's PSNR article. Then, I ran:
import cv2
img1 = cv2.imread('PSNR-example-base.png')
img2 = cv2.imread('PSNR-example-comp-90.jpg')
cv2.PSNR(img1, img2)
但是我获得的输出值是 29.436334461883582
,而不是 45.53
.
But the output value I obtain is 29.436334461883582
, instead of 45.53
.
我在做什么错了?
推荐答案
造成差异的原因很可能是在所引用图像的标题中提示的(我强调):
The cause for the difference is most likely hinted in the caption of the referenced images (emphasis by me):
不同质量级别的cjpeg压缩图像的 luma 示例PSNR值.
不是像您一样为整个RGB图像计算PSNR,而是仅为图像的 luma (亮度").
The PSNR is not calculated for the whole RGB image like you did, but only for the image's luma ("brightness").
此外,我什至无法从您的问题中再现 29.4363 ...
.所以,这是我的比较代码:
Also, I even couldn't reproduce the 29.4363...
from your question. So, here's my code for comparison:
import cv2
import numpy as np
from skimage import io # Only needed for web grabbing images, use cv2.imread for local images
def psnr(image1, image2):
# OpenCV
print('OpenCV PSNR: ', cv2.PSNR(image1, image2))
# Own implementation
mse = np.mean((image1.astype(np.float64) / 255 - image2.astype(np.float64) / 255) ** 2)
print('Own implementation: ', 10 * np.log10(1. / mse))
def luma(image):
return (0.299 * image[:, :, 2] + 0.587 * image[:, :, 1] + 0.114 * image[:, :, 0]).astype(np.uint8)
# return (0.2126 * image[:, :, 2] + 0.7152 * image[:, :, 1] + 0.0722 * image[:, :, 0]).astype(np.uint8)
# return (0.212 * image[:, :, 2] + 0.701 * image[:, :, 1] + 0.087 * image[:, :, 0]).astype(np.uint8)
# Calculate PSNR on referenced images
img1 = cv2.cvtColor(io.imread('https://upload.wikimedia.org/wikipedia/commons/d/d3/PSNR-example-base.png'), cv2.COLOR_RGB2BGR)
img2 = cv2.cvtColor(io.imread('https://upload.wikimedia.org/wikipedia/commons/2/2a/PSNR-example-comp-90.jpg'), cv2.COLOR_RGB2BGR)
psnr(img1, img2)
# Calculate luma PSNR on referenced images
psnr(luma(img1), luma(img2))
输出:
OpenCV PSNR: 39.021537956442224
Own implementation: 39.02153795644222
OpenCV PSNR: 44.79892614734474
Own implementation: 44.79892614734474
对我来说,RGB图像的PSNR远远高于您报告的PSNR.但是,亮度的PSNR确实与给定值匹配,尽管并不完全相同.我测试了几次亮度计算,没有一个给出确切的结果–但由于在参考示例中未提及该亮度,因此亮度的计算方式仍然无济于事.
For me, the PSNR of the RGB image is way higher than the one you report. But, the PSNR for the luma does quite match the given value, although it's not exactly the same. I tested several luma calculations, none gave the exact result – but since it's not mentioned in the referenced example, how the luma was calculated, trying to reproduce the exact values is meaningless anyway.
希望有帮助!
这篇关于PSNR计算结果不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!