问题描述
我已经实现了一个python代码,用于计算YCrCb通道中Y通道的PSNR值.我得到的PSNR值大约为35.7dB(对于一对图像)
I have implemented a python code for calculating PSNR values of Y channel in YCrCb channel.I get the PSNR values to be around 35.7dB(for a pair of images)
import cv2, main
import sys
i1 = cv2.imread(sys.argv[1])
i2 = cv2.imread(sys.argv[2])
i1= cv2.cvtColor(i1, cv2.COLOR_BGR2YCrCb)
i2= cv2.cvtColor(i2, cv2.COLOR_BGR2YCrCb)
print(main.psnr(i1[:,:,0], i2[:,:,0]))
在主psnr中定义为:
In main psnr is defined as:
def psnr(target, ref):
import cv2
target_data = numpy.array(target, dtype=numpy.float64)
ref_data = numpy.array(ref,dtype=numpy.float64)
diff = ref_data - target_data
print(diff.shape)
diff = diff.flatten('C')
rmse = math.sqrt(numpy.mean(diff ** 2.))
return 20 * math.log10(255 / rmse)
我在matlab中获得了在线实现(来自我所指的论文)我得到的PSNR值大约为37.06dB(对于同一副图像)
I got an online implementation(from the paper I am referring to) in matlabI get PSNR values to be around 37.06dB (for the same pair of images)
function psnr=compute_psnr(im1,im2)
if size(im1, 3) == 3,
im1 = rgb2ycbcr(im1);
im1 = im1(:, :, 1);
end
if size(im2, 3) == 3,
im2 = rgb2ycbcr(im2);
im2 = im2(:, :, 1);
end
imdff = double(im1) - double(im2);
imdff = imdff(:);
rmse = sqrt(mean(imdff.^2));
psnr = 20*log10(255/rmse)
此错误可能是由于numpy引入的错误还是numpy似乎实现了准确性?
Can this error be due to errors introduced by numpy or accuracy numpy seems to achieve?
推荐答案
您的两个转换函数似乎产生了截然不同的结果:
Your two conversion functions seem to produce wildly different results:
解释了差异.
八度确实提到了几种YCbCr标准:
Octave does mention there are several YCbCr standards:
The formula used for the conversion is dependent on two constants,
KB and KR which can be specified individually, or according to
existing standards:
"601" (default)
According to the ITU-R BT.601 (formerly CCIR 601) standard.
Its values of KB and KR are 0.114 and 0.299 respectively.
"709" (default)
According to the ITU-R BT.709 standard. Its values of KB and
KR are 0.0722 and 0.2116 respectively.
也许python版本使用的是其他标准? (或者可能是BGR与RGB问题?).无论如何,这就是差异所在,这似乎不是精妙的精度(当使用相同的输入分别测试这些函数时,它们会产生相同的结果).
Maybe the python version is using a different standard? (or maybe it's a BGR vs RGB issue?). In any case, that's where the discrepancy lies, it doesn't seem to be a matter of numpy precision (when those functions are tested separately with identical inputs, they produce the same results).
根据这些:
- 为什么在Matlab中由rgb2ycbcr转换的Y在[16,235]范围内?
- http://docs.opencv.org/3.1 .0/de/d25/imgproc_color_conversions.html#color_convert_rgb_ycrcb
- https://en.wikipedia.org/wiki/YCbCr#ITU -R_BT.601_conversion
- Why is the Y, converted by rgb2ycbcr in the Matlab, in the range [16, 235]?
- http://docs.opencv.org/3.1.0/de/d25/imgproc_color_conversions.html#color_convert_rgb_ycrcb
- https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion
python(或更确切地说,opencv库)似乎正在输出模拟"(未缩放)版本,而matlab/octave正在输出数字"(缩放)版本.
python (or rather, the opencv library) seems to be outputting the 'analog' (unscaled) version, whereas matlab / octave is outputting the 'digital' (scaled) version.
已确认:
# Python
RGB = numpy.concatenate(
( numpy.array([[[0], [255], [255], [0], [0], [0], [255]]], dtype=numpy.uint8),
numpy.array([[[0], [0], [255], [255], [255], [0], [255]]], dtype=numpy.uint8),
numpy.array([[[0], [0], [0], [0], [255], [255], [255]]], dtype=numpy.uint8)),
axis=2)
RGB2Y = cv2.cvtColor(RGB, cv2.COLOR_BGR2YCrCb)
print(RGB2Y)
[[[ 0 128 128]
[ 29 107 255]
[179 0 171]
[150 21 43]
[226 149 1]
[ 76 255 85]
[255 128 128]]]
% Octave
pkg load image;
RGB = uint8 (cat (3, [0, 255, 255, 0, 0, 0, 255], ...
[0, 0, 255, 255, 255, 0, 255], ...
[0, 0, 0, 0, 255, 255, 255]));
RGB2Y = rgb2ycbcr(RGB)
RGB2Y =
ans(:,:,1) =
16 81 210 145 170 41 235
ans(:,:,2) =
128 90 16 54 166 240 128
ans(:,:,3) =
128 240 146 34 16 110 128
因此,如果要实现一致性,我将使用上面的维基百科页面中提到的从模拟到数字的转换公式来缩放python结果,即:
Therefore, if it's a matter of achieving consistency, I would scale the python results using the conversion formula from analog to digital, mentioned in the wikipedia page above i.e.:
如果不是哪个版本最适合计算PSNR"的问题,我不知道,但是根据我在上面链接中看到的内容,我的钱将放在matlab/octave上实施.
If it's a question of "which version is the most appropriate one for the calculation of PSNR", I don't know, but from what I'm reading in the links above, my money would be on the matlab / octave implementation.
这篇关于PSNR值在Matlab实现和python中有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!