フィルターのクリア

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 件のコメント
Michael Sugiarto
Michael Sugiarto 2022 年 4 月 3 日
I got the code from https://stackoverflow.com/questions/15423132/convert-rgb-image-to-yuv420-image-by-matlab I just tried it out and I just wonder on how to make it work
Michael Sugiarto
Michael Sugiarto 2022 年 4 月 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 Center および File ExchangeConvert Image Type についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by