matlab公共函数之RGB与YUV转换

Posted hudalikm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab公共函数之RGB与YUV转换相关的知识,希望对你有一定的参考价值。

matlab中有自带的rgb转ycbcr函数,但是根据观测,其Y的值为[16 235],不符合我们的要求,所以,提供另一种规范下的转换脚本函数,其Y的值满足[0 255]

RGB转YUV

% function yuv = myrgb2yuv(image)
% input params.
% image: input color image with 3 channels, which value must be [0 255]
% output 
% yuv: 3 channels(YUV444, Y plane, U plane, V plane), value [0 255], double
%
%
% Author: KevenLee 
% Contact: [email protected]
% Version: V1.0

function yuv = myrgb2yuv(image)
image = double(image);
R = image(:,:,1);
G = image(:,:,2);
B = image(:,:,3);

yuv(:,:,1) = 0.299.*R + 0.587.*G + 0.114.*B;
yuv(:,:,2) = - 0.1687.*R - 0.3313.*G + 0.5.*B + 128;
yuv(:,:,3) = 0.5.*R - 0.4187.*G - 0.0813.*B + 128;
end

 YUV转RGB

% function rgb  = myyuv2rgb(image)
% input params.
% image: input YUV image with YUV444 format, which value must be [0 255]
% output 
% rgb: 3 channels color image, value [0 255], double
%
%
% Author: KevenLee 
% Contact: [email protected]
% Version: V1.0

function rgb  = myyuv2rgb(image)
image = double(image);
Y = image(:,:,1);
U = image(:,:,2);
V = image(:,:,3);

R = Y + 1.402.*(V-128);

G = Y - 0.34414.*(U-128) - 0.71414.*(V-128);

B = Y + 1.772.*(U-128);

rgb(:,:,1) = R;
rgb(:,:,2) = G;
rgb(:,:,3) = B;
end

 

以上是关于matlab公共函数之RGB与YUV转换的主要内容,如果未能解决你的问题,请参考以下文章

如何将rgb 模式转换成 yuv 模式

FFmpeg入门详解之121:颜色空间转换RGB和YUV的原理与实战

这个 YUV420P 到 RGB 着色器的转换从何而来?

YUV与RGB 以及之间的转换

YUV与RGB格式转换

YUV422与RGB互相转换