【谷速软件】matlab源码-图像RGB转YUV
function yuvimg = rgb2yuv(rgbimg)
% 转换矩阵
rgb2yuvT = [0.2126 0.7152 0.0722;...
-0.1146 -0.3854 0.5000;...
0.5000 -0.4542 -0.0468];
% 偏移矩阵
yuvoffset = [0; 128; 128];
% 计算
T = rgb2yuvT;
R = double(I(:,:,1));
G = double(I(:,:,2));
B = double(I(:,:,3));
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);
% 转成uint8
Y = uint8(round(Y));
U = uint8(round(U));
V = uint8(round(V));
% 输出
yuvimg = cat(3,Y,U,V);
end
相关主题