YUV to RGB formula:
/* R = Y + 1.14V,G = Y - 0.39U - 0.58V,B = Y + 2.03U YUV对应YCbCr
r = y + 1.14*cr;
g = y - 0.39*cb - 0.58*cr;
b = y + 2.03*cb; */
/* YCbCr与RGB的关系 */
r = 1.164*(y-16) + 1.596*(cr-128);
g = 1.164*(y-16) - 0.813*(cr-128) - 0.392*(cb-128);
b = 1.164*(y-16) + 2.017*(cb-128);
/* r=1.0*y+1.402*(cr-128) ;
g=1.0*y-0.34413*(cb-128)-0.71414*(cr-128);
b=1.0*y+1.772*(cb-128);*/
http://www.cnblogs.com/h2-database/archive/2011/09/29/2572580.html
http://m.blog.csdn.net/blog/zhengtu009/17221247
Sometimes All White or Black
imshow(uint8(A));
If the format is double, the value will be 0 or 1.
Or U can: imshow(I,[])
http://blog.csdn.net/sdlyjzh/article/details/8246752
http://zhidao.baidu.com/link?url=2CHdAHNvp_93IjpnZI_nA4-W5aPdp3ojPXSo5rCv5glYdXGLLPjBJLs7rSlqy5SqMuQ8LVRNPevtknUkueyYKK
http://www.360doc.com/content/13/1105/10/12085465_326756155.shtml
Whole Function
% Function: Draw arrow array for motion vector field.
% Input:
% fn: YUV data filename
% Output:
% pic:RGB picture.
%author:Chen Yu
%school:Carnegie Mellon University
function CutFrame
fid=fopen('————input interface YUV data————.txt','r');
if(fid == -1)
disp(['Open File ',fp,' Fail']);
else
ArrayData = fscanf(fid, '%d');
if(ArrayData(3) == 1)
ChromWidth = ArrayData(1)/2;
ChromHeight = ArrayData(2)/2;
elseif(ArrayData(3) == 2)
ChromWidth = ArrayData(1)/2;
ChromHeight = ArrayData(2);
elseif(ArrayData(3) == 3)
ChromWidth = ArrayData(1);
ChromHeight = ArrayData(2);
end
YPart = cat(1);
UVPart = cat(2);
for i = 1:ArrayData(2)
for j = 1:ArrayData(1)
index = (i - 1)*ArrayData(1) + j + 3;
YPart(i,j,1) = ArrayData(index);
end
end
imshow(uint8(YPart));
for i = 1:2
for j = 1:ChromHeight
for k = 1:ChromWidth
index = ArrayData(2)*ArrayData(1) + 3 + (j - 1)*ChromWidth + k + ChromHeight*ChromWidth*(i - 1);
UVPart(j, k, i) = ArrayData(index);
end
end
end
% imshow(uint8(UVPart(:, :, 1)));
RGB = cat(3);
for i = 1:ArrayData(2)
for j = 1:ArrayData(1)
tempi = ceil(i/2);
tempj = ceil(j/2);
RGB(i, j, 1) = 1.164*(YPart(i,j, 1)-16) + 1.596*(UVPart(tempi, tempj, 2)-128);
RGB(i, j, 2) = 1.164*(YPart(i,j, 1)-16) - 0.813*(UVPart(tempi, tempj, 1)-128) - 0.392*(UVPart(tempi, tempj, 2)-128);
RGB(i, j, 3) = 1.164*(YPart(i,j, 1)-16) + 2.017*(UVPart(tempi, tempj, 1)-128);
%RGB(i, j, 1) = YPart(i,j) + 1.4075*(UVPart(tempi, tempj, 1)-128);
%RGB(i, j, 2) = YPart(i,j) - 0.3455*(UVPart(tempi, tempj, 2)-128) - 0.7169*(UVPart(tempi, tempj, 1)-128);
%RGB(i, j, 3) = YPart(i,j) + 1.779*(UVPart(tempi, tempj, 2)-128);
if(RGB(i, j, 1) > 255)
RGB(i, j, 1) = 255;
elseif(RGB(i, j, 1) < 0)
RGB(i, j, 1) = 0;
end
if(RGB(i, j, 2) > 255)
RGB(i, j, 2) = 255;
elseif(RGB(i, j, 2) < 0)
RGB(i, j, 2) = 0;
end
if(RGB(i, j, 3) > 255)
RGB(i, j, 3) = 255;
elseif(RGB(i, j, 3) < 0)
RGB(i, j, 3) = 0;
end
end
end
imwrite(uint8(RGB(1:ArrayData(2),1:ArrayData(1),:)),'E:/ErrorConcealment/Bat/myPic.bmp');%写图片
%imshow(uint8(RGB(1:ArrayData(2),1:ArrayData(1),:)));
%imview(RGB(1:ArrayData(2),1:ArrayData(1),:));
end
if(fid ~= -1)
fclose(fid);
end
end
Version Two:
% Function: Draw arrow array for motion vector field.
% Input:
% fn: YUV data filename
% Output:
% pic:RGB picture.
%author:Chen Yu
%school:Carnegie Mellon University
function CutFrame
fid=fopen('D:/CY/JM18.6_RC/jm18.6/build/vs version1.0/YuvDataOfAFrame.txt','r');
if(fid == -1)
disp(['Open File ',fp,' Fail']);
else
ArrayData = fscanf(fid, '%d');
if(ArrayData(3) == 1)
ChromWidth = ArrayData(1)/2;
ChromHeight = ArrayData(2)/2;
elseif(ArrayData(3) == 2)
ChromWidth = ArrayData(1)/2;
ChromHeight = ArrayData(2);
elseif(ArrayData(3) == 3)
ChromWidth = ArrayData(1);
ChromHeight = ArrayData(2);
end
YPart = cat(1);
UVPart = cat(2);
for i = 1:ArrayData(2)
for j = 1:ArrayData(1)
index = (i - 1)*ArrayData(1) + j + 3;
YPart(i,j,1) = ArrayData(index);
end
end
figure;
imshow(uint8(YPart));
for i = 1:2
for j = 1:ChromHeight
for k = 1:ChromWidth
index = ArrayData(2)*ArrayData(1) + 3 + (j - 1)*ChromWidth + k + ChromHeight*ChromWidth*(i - 1);
UVPart(j, k, i) = ArrayData(index);
end
end
end
figure;
imshow(uint8(UVPart(:, :, 1)));
figure;
imshow(uint8(UVPart(:, :, 2)));
RGB = cat(3);
for i = 1:ArrayData(2)
for j = 1:ArrayData(1)
tempi = ceil(i/2);
tempj = ceil(j/2);
RGB(i, j, 1) = 1.164*(YPart(i,j, 1)-16) + 1.596*(UVPart(tempi, tempj, 2)-128);
RGB(i, j, 2) = 1.164*(YPart(i,j, 1)-16) - 0.813*(UVPart(tempi, tempj, 1)-128) - 0.392*(UVPart(tempi, tempj, 2)-128);
RGB(i, j, 3) = 1.164*(YPart(i,j, 1)-16) + 2.017*(UVPart(tempi, tempj, 1)-128);
%RGB(i, j, 1) = YPart(i,j) + 1.4075*(UVPart(tempi, tempj, 1)-128);
%RGB(i, j, 2) = YPart(i,j) - 0.3455*(UVPart(tempi, tempj, 2)-128) - 0.7169*(UVPart(tempi, tempj, 1)-128);
%RGB(i, j, 3) = YPart(i,j) + 1.779*(UVPart(tempi, tempj, 2)-128);
if(RGB(i, j, 1) > 255)
RGB(i, j, 1) = 255;
elseif(RGB(i, j, 1) < 0)
RGB(i, j, 1) = 0;
end
if(RGB(i, j, 2) > 255)
RGB(i, j, 2) = 255;
elseif(RGB(i, j, 2) < 0)
RGB(i, j, 2) = 0;
end
if(RGB(i, j, 3) > 255)
RGB(i, j, 3) = 255;
elseif(RGB(i, j, 3) < 0)
RGB(i, j, 3) = 0;
end
end
end
imwrite(uint8(RGB(1:ArrayData(2),1:ArrayData(1),:)),'E:/ErrorConcealment/Bat/directiveAverage.bmp');%写图片
figure;
imshow(uint8(RGB(1:ArrayData(2),1:ArrayData(1),:)));
end
if(fid ~= -1)
fclose(fid);
end
end
YUV component as below:
RGB picture:
http://www.cnblogs.com/h2-database/archive/2011/09/29/2572580.html
http://blog.csdn.net/velanjun/article/details/20643545
http://zhidao.baidu.com/link?url=SErTJmLny82Fj94o3vG44-VHrLYC1qV_K_3v_2fqwMgs6TD7GcC4knTdM3EyTAvIF12323gh1TkkFIITe4HQX201o6TF_Iw79WoYIbNHCmG
http://m.blog.csdn.net/blog/zhengtu009/17221247
http://blog.csdn.net/jerrytong/article/details/5658818
http://www.cnblogs.com/hoys/archive/2012/08/01/2619144.html
http://www.xuebuyuan.com/867587.html
http://www.cnblogs.com/liwenbin/archive/2012/10/18/2728759.html