Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 用matlab实现RGB图像到yuv420图像的转换_Image_Matlab_Image Processing - Fatal编程技术网

Image 用matlab实现RGB图像到yuv420图像的转换

Image 用matlab实现RGB图像到yuv420图像的转换,image,matlab,image-processing,Image,Matlab,Image Processing,我想将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 = Yd'; U

我想将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 = Yd';
U = UVd';
V = UVd';
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;
        end
        %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;
        end
    end
end

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:

当我得到yuv图像时,我是这样读的(我试着读过其他yuv图像,并且这段代码可以很好地工作):

[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不同:

所以,问题是,将rgb图像转换为yuv图像的代码有什么问题?Thx

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 = Yd';
U = UVd';
V = UVd';
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;
        end
        %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;
        end
    end
end

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 = Yd';
U = UVd';
V = UVd';
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;
        end
        %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;
        end
    end
end

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);
输出:


您是否将输入转换为双精度?如果不这样做,那么matlab将在uint8中执行所有算法。尝试使用im2double,它可以缩放[0 1]之间的所有值。完成后可以使用im2uin8。看看你的代码,我想你的变量r,g,b都是uint8。@jucestain我已经更改了我的代码:将Y,U,V转换为uint8,看起来更好,但似乎仍然不正确。将我的答案复制并粘贴到你的脚本中。我刚刚在imread的第一行代码周围添加了一个“double”。@XiaJun请再试一次。我重新运行代码,这些是当ColorImageRGB未转换为double时的输出。