Image convert to RGB to YUV420

24 次查看(过去 30 天)
Michael Sugiarto
Michael Sugiarto 2022-4-3
编辑: DGM 2022-4-3

I have this code to change image into RGB to YUV. but, the yuv_import is not recognised any ideas on what to replace it with?
ColorImageRGB = imread('view1.png');

[rows, cols , ~] = 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);

  3 个评论

请先登录,再进行评论。

回答(2 个)

Image Analyst
Image Analyst 2022-4-3
OK, so do you have a function called yuv_import() in your current folder, or at least on the search path? What does this show
>> which -all yuv_import
  5 个评论
Michael Sugiarto
Michael Sugiarto 2022-4-3
Yes but, since in stackoverflow does not specify the function of yuv_import i dont know what that is. When i search the internet it gave me imports yuv sequence. how would I create a function for that? Do you know?
Image Analyst
Image Analyst 2022-4-3
I hvae no idea what that function is, nor did I read the Stackoverflow post in detail. If the function is not given there you'll have to come up with some workaround. What are you really trying to do? And why do you think you need that specific function? You already have the YUV image before the code gets to that point. Why do you want the YUV color space instead of any of the others?

请先登录,再进行评论。


DGM
DGM 2022-4-3
编辑:DGM 2022-4-3
I don't know where you're getting yuv2rgb(), but it's almost certainly using YCbCr instead of YUV -- in which case, you can probably just use ycbcr2rgb(), assuming it's BT601 with standard margins. Both of the FEX tools linked above use YCbCr, not YUV as the names imply.
Trying to do this with actual YUV doesn't work like you think it does. You can't just offset by 128 without truncating chroma on conversion to uint8. That's why YCbCr exists and has the chroma range that it does. To put it simply, 2*0.615 >1. You can't offset by half (or any other offset value) and still keep the V swing in the available data range.
This is YUV:
This is YCbCr (uint8, with margins):
As I mentioned, YCbCr usually isn't full-swing, so you'd need a further scaling operation prior to offsetting -- or you can just use ycbcr2rgb().
See this comment for more disambiguation (ignore the discussion of rounding error)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by