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
% formats are:
% 'YUV444_8' = 4:4:4 sampling, 8-bit precision
% 'YUV420_8' = 4:2:0 sampling, 8-bit precision
% convmtrx - Conversion matrix [optional, default = 'BT709_l']. The
% following conversions ase defined (see in Notes for more
% details):
% 'BT601_f' = ITU-R BT.601, RGB full [0...255] (BT601_f.mat)
% 'BT601_219' = ITU-R BT.601, RGB limited [0...219] (BT601_219.mat)
% 'BT601_l' = ITU-R BT.601, RGB limited [16...235] (BT601_l.mat)
% 'BT709_f' = ITU-R BT.709, RGB limited [0...255] (BT709_f.mat)
% 'BT709_l' = ITU-R BT.709, RGB limited [16...235] (BT709_l.mat)
%
%Output:
% Y,U,V - Y,U and V components of the frame
%
%Uses:
% imresize.m - Matlab Image Processing Toolbox (when formats other than
% 4:4:4 used)
%
%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"):
% Y = [16...235]
% Cb,Cr = [16...240]
%
% 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:
% 0.257 0.504 0.098
% -0.148 -0.291 0.439
% 0.439 -0.368 -0.071
% yuvoffset = [16; 128; 128]
% (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:
% 0.299 0.587 0.114
% -0.173 -0.339 0.511
% 0.511 -0.428 -0.083
% yuvoffset = [16; 128; 128]
% (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,
% while the coefficients here are from [4] and [6]. If original signal is in
% range [16...235] then offset of 16 for Y signal is not necessary, e.g.
% definition from [6])
% 3) RGB limited to [16...235], rgb2yuvT matrix:
% 0.299 0.587 0.114
% -0.169 -0.331 0.500
% 0.500 -0.419 -0.081
% yuvoffset = [0; 128; 128]
% (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
% full [0...255] range, where the output is in range Y=[0...255];
% Cb=[0.5...255.5];Cr=[0.5...255.5])
% (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:
% 0.1826 0.6142 0.0620
% -0.1006 -0.3386 0.4392
% 0.4392 -0.3989 -0.0403
% yuvoffset = [16; 128; 128]
% (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
% scaled version of the next one, where RGB is limited.)
%
% 2) RGB limited to [16...235], rgb2yuvT matrix:
% 0.2126 0.7152 0.0722
% -0.1146 -0.3854 0.5000
% 0.5000 -0.4542 -0.0468
% yuvoffset = [0; 128; 128]
% (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
% obvious reason)
%
% References:
% [1] Rec. ITU-R BT.601-6
% [2] Rec. ITU-R BT.709-5
% [3]
http://en.wikipedia.org/wiki/YCbCr
% [4]
http://www.poynton.com/ColorFAQ.html
% [5]
http://www.mathworks.com/access/helpdesk/help/toolbox/vipblks/ref/colorspaceconversion.html
% [6]KeithJack,VideoDemystified,Chapter3,
http://www.compression.ru/download/articles/color_space/ch03.pdf
% [7]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceddraw/html/_dxce_converting_between_yuv_and_rgb.asp
%
%Example:
% yuv = rgb2yuv(R,G,B,'YUV420_8','BT709_f');
R = frame(:,:,1);
G = frame(:,:,2);
B = frame(:,:,3);
if strcmp(convmtrx,'BT601_f')
load('BT601_f.mat','-mat');
elseif strcmp(convmtrx,'BT601_l')
load('BT601_l.mat','-mat');
elseif strcmp(convmtrx,'BT601_219')
load('BT601_219.mat','-mat');
elseif strcmp(convmtrx,'BT709_f')
load('BT709_f.mat','-mat');
elseif strcmp(convmtrx,'BT709_l')
load('BT709_l.mat','-mat');
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'))
U = imresize(U,0.5,'bicubic');
V = imresize(V,0.5,'bicubic');
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 + 409 * E + 128) >> 8)
% G = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8)
% B = clip(( 298 * C + 516 * D + 128) >> 8)
%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));
本文转载自:
http://www.mathworks.cn/matlabcentral/fileexchange/27051-mpeg2-video-encoder/content/MPEG2_Video_Encoder_MATLAB-RaviLakkundi/rgb2yuv.m
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, defa
matlab
中
有自带的
rgb
转
ycbcr
函数
,但是根据观测,其Y的值为[16 235],不符合我们的要求,所以,提供另一种规范下的
转换
脚本
函数
,其Y的值满足[0 255]
RGB
转
YUV
% function
yuv
= my
rgb
2
yuv
(image)
% input params.
% image: input color image with 3 channels, w...
在图像处理过程
中
,经常会遇到
YUV
与
RGB
之间的
转换
,这个
转换
有不同标准,有多种
转换
公式
,有时,由于没有成对的匹配上,导致了效果异常。现在总结各种标准的
转换
公式
。
BT601
标准里有三种,
BT709
有两种。
BT601
full range
rgb
=[0, 255], y=[16, 235], uv=[16, 240]
R = 1.164 * (Y - 16) + 1.596 * (V - 128)
G = 1.164 * (Y - 16) - 0.392 * (U - 128) - 0.812 *...
% BT.601标准
Y = 0.299*omg(:,:,1) + 0.587*omg(:,:,2) + 0.114*omg(:,:,3);
U = -0.147*omg(:,:,1)- 0.289*omg(:,:,2) + 0.436*omg(:,:,3);
V = 0.615*omg(:,:,1) - 0.5...
在图像处理
中
,经常会遇到
RGB
和
YUV
图像的问题,虽然有一些工具可以直接
转化
,但是对于数据的处理过程并不太清晰,这里给出
MATLAB
的实现,直观感受数据
转化
过程及结果。
1.从
RGB
转
为
YUV
422
clc;close all;clear
infilename = '***.bmp';
outfilename = '***.
yuv
';
RGB
img =imread(infilename);
%figure;imshow(
RGB
img);
YUV
img =
rgb
2ycbcr(
RGB
img);
颜色空间的
转换
,除了闹着玩的找找定位,画画框,更具代表性的就是
yuv
到
rgb
的
转化
主要用到了sdk
中
ive内的算子,由于默认的是不支持ive的静态库的,需要在Makefile
中
将其添加进来
还是新建专门用于
转换
的线程
在原先处理线程的基础上进行修改
HI_MPI_IVE_CSC
是核心的
转换
函数
,位于SVP目录下HiIVE API 参考手册
中
,重点是对该
函数
参数的配置
注意事项要看仔细,这里对分辨率有着明确的要求,没细看的我第一次天真的又跳进自己挖好的坑里,不过在上一期思维方式
因为在作品图像相关的东西,过程
中
,不可避免会涉及到
RGB
与
YUV
的相互
转化
,同时在模型训练的过程
中
,有时候,只是训练的Y channel,所以还涉及到提取出channel 最后再合并成
RGB
图像的一个问题。
但是呢,在模型训练的时候,用的都是tensor,所以还涉及到从image
到tensor 的过程。
这里只介绍两种方法,一种是 基于BT.601
另一种是使用PIL transforms的那个Image.open().convert(‘YCbCr’)
先看第一种方法:
BT601
def
rgb
2y
YUV
是一种颜色编码方法,Y 代表明亮度,U 和 V 则是色度。
YUV
是一类颜色编码方法的统称,如
YUV
、Y`UV、YCbCr、YPbPr 等专有名词都可以称为
YUV
。
Y'UV 的发明是由于黑白电视到彩色电视的过渡时期。黑白视频只有 Y 数据,也就是灰阶值,到了彩色电视时期,增加了 UV 表示图像的色度(U 代表蓝色通道与亮度的差值,V 代表红色通道与亮度的差值),如果忽略...
"Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
Naruto123456__:
加载内核模块-Unknown symbol错误分析
西瓜要加盐: