码迷,mamicode.com
首页 > 其他好文 > 详细

matlab公共函数之RGB与YUV转换

时间:2017-08-23 20:47:15      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:log   观测   atl   mat   format   highlight   ams   value   tput   

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: hudalikm@163.com
% 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: hudalikm@163.com
% 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转换

标签:log   观测   atl   mat   format   highlight   ams   value   tput   

原文地址:http://www.cnblogs.com/Keven-Lee/p/7419872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!