MATLAB:RGB转BT601、BT709协议中各种YUV格式的转换函数
(2012-09-21 17:35:17)
标签:
图像matlab色彩空间it |
分类: MATLAB |
function
[Y,U,V]=rgb2yuv(frame,yuvformat,convmtrx)
%Converts RGB to YUV
%[Y,U,V]=rgb2yuv(R,G,B,yuvformat)
%Version: 3.00, Date: 2007/11/21, author: Nikola Sprljan
%
%Input:
% R,G,B - R,G and B components of the frame
% yuvformat - YUV format [optional, default = 'YUV444_8'].
Supported YUV
%
%
%
% convmtrx - Conversion matrix [optional, default = 'BT709_l'].
The
%
%
%
%
%
%
%
%
%Output:
% Y,U,V - Y,U and V components of the frame
%
%Uses:
% imresize.m - Matlab Image Processing Toolbox (when formats other
than
%
%
%Note:
% Note that a more correct term for what is here called YUV would
be YCbCr,
% since it is used for YUV representation in digital domain. Also,
the R, G
% and B components are actually non-linear because of gamma
correction, and
% are more correctly denoted as R', G' and B'. YCbCr is expected to
be in
% the range (below that is "footroom" range and above is
"headroom"):
%
%
%
% Some more details on the defined conversions follow.
% ITU-R BT.601 - for SD (720x576) and lower resolutions. Three
versions are
% available:
% 1) RGB in full range [0...255], rgb2yuvT matrix:
%
%
%
%
% (Resulting output range is
Y=[16...235];Cb=[16...240];Cr=[16...240]
% (Coefficients taken from [3],[4],[5],[6]. Integer implementation
in [7])
%
% 2) RGB limited to [0...219], rgb2yuvT matrix:
%
%
%
%
% (Resulting output range is
Y=[16...235];Cb=[16...240];Cr=[16...240])
% (Note that in [1] the coeffcients are rounded to the nearest
integer,
%
%
%
% 3) RGB limited to [16...235], rgb2yuvT matrix:
%
%
%
%
% (Resulting output range is
Y=[16...235];Cb=[18.5...237.5];Cr=[18.5...237.5])
% (This conversion is also used in JPEG, which allows the input to
be in the
%
%
% (Coefficients taken from [1],[3])
%
% ITU-R BT.709 - for HD resolutions (i.e. higher than SD). Two
versions are
% available:
% 1) RGB in full range [0...255], rgb2yuvT matrix:
%
%
%
%
% (Resulting output range is
Y=[16...235];Cb=[16...240];Cr=[16...240])
% (Coefficients taken from [5], less precise version in [6].
Appears to be a
%
%
% 2) RGB limited to [16...235], rgb2yuvT matrix:
%
%
%
%
% (Resulting output range is
Y=[16...235];Cb=[18.5...237.5];Cr=[18.5...237.5])
% (Coefficients taken from [2]. The ones in [6] are slightly
different, for no
%
%
% References:
%
%
%
%
%
%
%
%
%Example:
% yuv = rgb2yuv(R,G,B,'YUV420_8','BT709_f');
R =
frame(:,:,1);
G = frame(:,:,2);
B = frame(:,:,3);
if strcmp(convmtrx,'BT601_f')
elseif strcmp(convmtrx,'BT601_l')
elseif strcmp(convmtrx,'BT601_219')
elseif strcmp(convmtrx,'BT709_f')
elseif strcmp(convmtrx,'BT709_l')
end;
T = rgb2yuvT;
R = double(R);
G = double(G);
B = double(B);
Y = T(1,1) * R + T(1,2) * G + T(1,3) * B + yuvoffset(1);
U = T(2,1) * R + T(2,2) * G + T(2,3) * B + yuvoffset(2);
V = T(3,1) * R + T(3,2) * G + T(3,3) * B + yuvoffset(3);
if (strcmp(yuvformat,'YUV420_8'))
elseif (strcmp(yuvformat,'YUV444_8'))
%do nothing, already in the correct
subsampling format
end;
Y = uint8(round(Y));
U = uint8(round(U));
V = uint8(round(V));
%Alternative conversion, as in [7],
defined with:
% C = Y - 16
% D = U - 127
% E = V - 128
% R = clip(( 298 *
C
% G = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8)
% B = clip(( 298 * C + 516 *
D
%yuv(:,:,1) = yuv(:,:,1) - 16;
%yuv(:,:,2) = yuv(:,:,2) - 128;
%yuv(:,:,3) = yuv(:,:,3) - 128;
%rgb(:,:,1) = uint8(floor((298*yuv(:,:,1) + 409*yuv(:,:,3) +
128)/256));
%rgb(:,:,2) = uint8(floor((298*yuv(:,:,1) - 100*yuv(:,:,3) -
208*yuv(:,:,2))/256));
%rgb(:,:,3) = uint8(floor((298*yuv(:,:,1) + 516*yuv(:,:,2) +
128)/256));

加载中…