使用imresize 进行上采样“几乎正确”。
不要使用imresize,最好使用vision.ChromaResampler 进行上采样:
up_resampler = vision.ChromaResampler();
up_resampler.Resampling = '4:2:2 to 4:4:4';
[Cb_resized, Cr_resized] = up_resampler(Cb, Cr);
该模块的设计使得Resampling = '4:2:2 to 4:4:4'“反转”了Resampling = '4:4:4 to 4:2:2'的结果。
'4:4:4 to 4:2:2' ChromaResampler 使用将结果向右移动 0.5 个像素的约定。
(我认为移动 0.5 像素应该符合 MPEG-1 编解码器标准)。
0.5 的位移没有很好的记录 - 我必须建立一个简短的测试来弄清楚它。
据我所知,MPEG-1 编解码器使用移动 0.5 个像素的约定,但 MPEG-2 和更新的编解码器不使用。
我不认为它被JPEG使用,但我不确定......
注意:
由于人类视觉系统对色度分辨率不是很敏感,因此无论是否使用 0.5 位移,您都可能看不到差异。
为了得到与ChromaResampler相同的结果,您可以使用imwarp,在水平轴上位移1个像素。
理解imwarp有点复杂。
我将使用imwarp 来演示 1 个像素的位移,得到与ChromaResampler 相同的结果:
以下代码示例显示了等效性:
close all
I = imread('peppers.png'); % Read sample image
YUV = rgb2ycbcr(I); % Convert RGB to Y:Cb:Cr
U = YUV(:, :, 2); % Get U color channel
V = YUV(:, :, 3); % Get V color channel
down_resampler = vision.ChromaResampler(); % 4:4:4 to 4:2:2
down_resampler.Resampling = '4:4:4 to 4:2:2';
up_resampler = vision.ChromaResampler(); % 4:2:2 to 4:4:4
up_resampler.Resampling = '4:2:2 to 4:4:4';
% Down-sample U and V using ChromaResampler
[downU, downV] = down_resampler(U, V);
%downU2 = imresize(U, [size(U, 1), size(U, 2)/2]); % Not the same as using imresize
%figure;imshow(downU);figure;imshow(downU2);
% Up-sample downU and downV using ChromaResampler
[upU, upV] = up_resampler(downU, downV);
% Result is not the same as using imresize
%resizedU = imresize(downU, [size(downU, 1), size(downU, 2)*2], 'bilinear');
%resizedV = imresize(downV, [size(downV, 1), size(downV, 2)*2], 'bilinear');
% Use transformation matrix that resize horizontally by x2 and include single pixel horizontal displacement.
tform = affine2d([ 2 0 0
0 1 0
-1 0 1]);
% Use imwarp instead of imresize (the warp includes horizontal displacement of 1 pixel)
warpU = imwarp(downU, tform, 'bilinear', 'OutputView', imref2d([size(downU, 1), size(downU, 2)*2]));
warpU(:, end) = warpU(:, end-1); % Fill the last column by duplication
%figure;imagesc(double(upU) - double(resizedU));impixelinfo
%figure;imshow(upU);figure;imshow(resizedU);
%figure;imshow(upU);figure;imshow(warpU);
% Show the differences:
figure;imagesc(double(upU) - double(warpU));title('Diff');impixelinfo
max_abs_diff = max(imabsdiff(warpU(:), upU(:)));
disp(['max_abs_diff = ', num2str(max_abs_diff)]); % Maximum absolute differenced is 1 (due to rounding).
注意:imresize 的用法保存在 cmets 中。
注意:
imresize默认的插值方式是三次插值,ChromaResampler默认的插值方式是线性插值。
三次插值被认为是优越的,但通常使用线性插值(可见差异可以忽略不计)。