求教matlab rgb图像图像RGB转YUV

在MATLAB中为什么要把RGB图像需要转成YUV图像后再进行操作_百度知道
在MATLAB中为什么要把RGB图像需要转成YUV图像后再进行操作
这个问题的潜含义是“必须把RGB转YUV”。但实际不是必须的。不同的色彩空间有不同的用途,根据实际需要选择合适的就可以。至于题主的具体应用里为什么要用YUV,这别人就不知道了。
其他类似问题
为您推荐:
yuv的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁 上传我的文档
 下载
 收藏
多年高中历史授课经验,具有相应专业背景,善于与学生沟通,善于调动课堂气氛。
 下载此文档
正在努力加载中...
基于Matlab的视频图像捕获与格式转换 
下载积分:636
内容提示:基于Matlab的视频图像捕获与格式转换 
文档格式:PDF|
浏览次数:0|
上传日期: 22:33:10|
文档星级:
该用户还上传了这些文档
基于Matlab的视频图像捕获与格式转换 
官方公共微信Convert RGB image to yuv420 image by matlab [RGB图像转换为YUV420格式图像的MATLAB] - 问题-字节技术
Convert RGB image to yuv420 image by matlab
RGB图像转换为YUV420格式图像的MATLAB
问题 (Question)
I want to convert RGB image to yuv420 image and this is my Matlab code:
ColorImageRGB = imread('view1.png');
ColorImageYUV = rgb2ycbcr(ColorImageRGB);
[rows cols d] = size(ColorImageRGB);
dims = [cols rows];
dimsUV = uint8(dims / 2);
Yd = zeros(dimsUV);
UVd = zeros(dimsUV);
Y = uint8(Y);
U = uint8(U);
V = uint8(V);
%sample:420
for i = 1 : rows
for j = 1 : cols
%sample Y in every row
r = ColorImageRGB(i,j,1);
g = ColorImageRGB(i,j,2);
b = ColorImageRGB(i,j,3);
Y(i,j) = 0.299 * r + 0.587 * g + 0.114 * b + 16;
%old line : sample 1/2 U
if mod(i,2) == 1
index_i = uint8(i / 2);
index_j = uint8(j / 2);
U(index_i,index_j) = -0.147 * r - 0.289 * g + 0.436 * b + 128;
%even line : sample 1/2 V
if mod(i,2) == 0
index_i = uint8(i / 2);
index_j = uint8(j / 2);
V(index_i,index_j) = 0.615 * r - 0.515 * g - 0.1 * b + 128;
filename = 'view1.yuv';
fid=fopen(filename,'w');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
fclose(fid);
view1.png:
When I get the yuv image ,I read it like this(I have tried to read other yuv image,and it this code can work well):
[Y U V] = yuv_import('view1.yuv',[cols rows],2);
rgb=yuv2rgb(Y{1},U{1},V{1});
imwrite(rgb,'test.bmp','bmp');
But test.bmp is no the same as view1.png:
So ,the question is,what is wrong with the code that convert rgb image to yuv image?Thx。
我想把RGB图像到YUV420格式图像,这是我的MATLAB代码:ColorImageRGB = imread('view1.png');
ColorImageYUV = rgb2ycbcr(ColorImageRGB);
[rows cols d] = size(ColorImageRGB);
dims = [cols rows];
dimsUV = uint8(dims / 2);
Yd = zeros(dimsUV);
UVd = zeros(dimsUV);
Y = uint8(Y);
U = uint8(U);
V = uint8(V);
%sample:420
for i = 1 : rows
for j = 1 : cols
%sample Y in every row
r = ColorImageRGB(i,j,1);
g = ColorImageRGB(i,j,2);
b = ColorImageRGB(i,j,3);
Y(i,j) = 0.299 * r + 0.587 * g + 0.114 * b + 16;
%old line : sample 1/2 U
if mod(i,2) == 1
index_i = uint8(i / 2);
index_j = uint8(j / 2);
U(index_i,index_j) = -0.147 * r - 0.289 * g + 0.436 * b + 128;
%even line : sample 1/2 V
if mod(i,2) == 0
index_i = uint8(i / 2);
index_j = uint8(j / 2);
V(index_i,index_j) = 0.615 * r - 0.515 * g - 0.1 * b + 128;
filename = 'view1.yuv';
fid=fopen(filename,'w');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
fclose(fid);
view1 PNG? 1/4 ?。当我得到的YUV图像,我看这样我试图读其他? 1/4 ^YUV图像,它这个代码可以很好地工作? 1/4 ‰:[Y U V] = yuv_import('view1.yuv',[cols rows],2);
rgb=yuv2rgb(Y{1},U{1},V{1});
imwrite(rgb,'test.bmp','bmp');
但没有相同test.bmp view1 PNG? 1/4 ?。所以,问题是,什么是代码转换成RGB图像YUV图像错了吗?谢谢?EUR,
最佳答案 (Best Answer)
ColorImageRGB = imread('view1.png');
ColorImageYUV = rgb2ycbcr(ColorImageRGB);
[rows cols d] = size(ColorImageRGB);
dims = [cols rows];
dimsUV = uint8(dims / 2);
Yd = zeros(dimsUV);
UVd = zeros(dimsUV);
Y = uint8(Y);
U = uint8(U);
V = uint8(V);
%sample:420
for i = 1 : rows
for j = 1 : cols
%sample Y in every row
r = ColorImageRGB(i,j,1);
g = ColorImageRGB(i,j,2);
b = ColorImageRGB(i,j,3);
Y(i,j) = 0.299 * r + 0.587 * g + 0.114 * b + 16;
%old line : sample 1/2 U
if mod(i,2) == 1
index_i = uint8(i / 2);
index_j = uint8(j / 2);
U(index_i,index_j) = -0.147 * r - 0.289 * g + 0.436 * b + 128;
%even line : sample 1/2 V
if mod(i,2) == 0
index_i = uint8(i / 2);
index_j = uint8(j / 2);
V(index_i,index_j) = 0.615 * r - 0.515 * g - 0.1 * b + 128;
filename = 'view1.yuv';
fid=fopen(filename,'w');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
fclose(fid);
figure(1);
[Y U V] = yuv_import('view1.yuv',[cols rows],2);
rgb=yuv2rgb(Y{1},U{1},V{1});
imwrite(rgb,'test.bmp','bmp');
a = imread('test.bmp');
subplot(1,2,1);
imshow(a);
ColorImageRGB = double(imread('view1.png'));
ColorImageYUV = rgb2ycbcr(ColorImageRGB);
[rows cols d] = size(ColorImageRGB);
dims = [cols rows];
dimsUV = uint8(dims / 2);
Yd = zeros(dimsUV);
UVd = zeros(dimsUV);
Y = uint8(Y);
U = uint8(U);
V = uint8(V);
%sample:420
for i = 1 : rows
for j = 1 : cols
%sample Y in every row
r = ColorImageRGB(i,j,1);
g = ColorImageRGB(i,j,2);
b = ColorImageRGB(i,j,3);
Y(i,j) = 0.299 * r + 0.587 * g + 0.114 * b + 16;
%old line : sample 1/2 U
if mod(i,2) == 1
index_i = uint8(i / 2);
index_j = uint8(j / 2);
U(index_i,index_j) = -0.147 * r - 0.289 * g + 0.436 * b + 128;
%even line : sample 1/2 V
if mod(i,2) == 0
index_i = uint8(i / 2);
index_j = uint8(j / 2);
V(index_i,index_j) = 0.615 * r - 0.515 * g - 0.1 * b + 128;
filename = 'view1.yuv';
fid=fopen(filename,'w');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
fclose(fid);
[Y U V] = yuv_import('view1.yuv',[cols rows],2);
rgb=yuv2rgb(Y{1},U{1},V{1});
imwrite(rgb,'test.bmp','bmp');
a = imread('test.bmp');
subplot(1,2,2);
imshow(a);
ColorImageRGB = imread('view1.png');
ColorImageYUV = rgb2ycbcr(ColorImageRGB);
[rows cols d] = size(ColorImageRGB);
dims = [cols rows];
dimsUV = uint8(dims / 2);
Yd = zeros(dimsUV);
UVd = zeros(dimsUV);
Y = uint8(Y);
U = uint8(U);
V = uint8(V);
%sample:420
for i = 1 : rows
for j = 1 : cols
%sample Y in every row
r = ColorImageRGB(i,j,1);
g = ColorImageRGB(i,j,2);
b = ColorImageRGB(i,j,3);
Y(i,j) = 0.299 * r + 0.587 * g + 0.114 * b + 16;
%old line : sample 1/2 U
if mod(i,2) == 1
index_i = uint8(i / 2);
index_j = uint8(j / 2);
U(index_i,index_j) = -0.147 * r - 0.289 * g + 0.436 * b + 128;
%even line : sample 1/2 V
if mod(i,2) == 0
index_i = uint8(i / 2);
index_j = uint8(j / 2);
V(index_i,index_j) = 0.615 * r - 0.515 * g - 0.1 * b + 128;
filename = 'view1.yuv';
fid=fopen(filename,'w');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
fclose(fid);
figure(1);
[Y U V] = yuv_import('view1.yuv',[cols rows],2);
rgb=yuv2rgb(Y{1},U{1},V{1});
imwrite(rgb,'test.bmp','bmp');
a = imread('test.bmp');
subplot(1,2,1);
imshow(a);
ColorImageRGB = double(imread('view1.png'));
ColorImageYUV = rgb2ycbcr(ColorImageRGB);
[rows cols d] = size(ColorImageRGB);
dims = [cols rows];
dimsUV = uint8(dims / 2);
Yd = zeros(dimsUV);
UVd = zeros(dimsUV);
Y = uint8(Y);
U = uint8(U);
V = uint8(V);
%sample:420
for i = 1 : rows
for j = 1 : cols
%sample Y in every row
r = ColorImageRGB(i,j,1);
g = ColorImageRGB(i,j,2);
b = ColorImageRGB(i,j,3);
Y(i,j) = 0.299 * r + 0.587 * g + 0.114 * b + 16;
%old line : sample 1/2 U
if mod(i,2) == 1
index_i = uint8(i / 2);
index_j = uint8(j / 2);
U(index_i,index_j) = -0.147 * r - 0.289 * g + 0.436 * b + 128;
%even line : sample 1/2 V
if mod(i,2) == 0
index_i = uint8(i / 2);
index_j = uint8(j / 2);
V(index_i,index_j) = 0.615 * r - 0.515 * g - 0.1 * b + 128;
filename = 'view1.yuv';
fid=fopen(filename,'w');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
count = fwrite(fid,Y','ubit8');
count = fwrite(fid,U','ubit8');
count = fwrite(fid,V','ubit8');
fclose(fid);
[Y U V] = yuv_import('view1.yuv',[cols rows],2);
rgb=yuv2rgb(Y{1},U{1},V{1});
imwrite(rgb,'test.bmp','bmp');
a = imread('test.bmp');
subplot(1,2,2);
imshow(a);
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:}

我要回帖

更多关于 matlab rgb2yuv函数 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信