MATLAB read an RGB image into YUV format

Source: Internet
Author: User

1. Read in the photo

Flag definition for control output

Clc;close all;clear YES = 1; NO = 0;%yes Indicates the output of the file, please configure yuv444_out_txt = 1;   YUV444_OUT_YUV = 0;yuv422_out_txt = 0; YUV422_OUT_YUV = 0;yuv420_out_txt = 0; YUV420_OUT_YUV = 1;
filename = ' koala.jpg '; filestr = filename (1:findstr (filename, '. jpg ')-1); filepath = ['. \ ' filestr ' out\ ']mkdir (filepath ); filestr = [filepath filestr]; Rgbimg =imread (filename); figure;imshow (rgbimg);

1) The photos you read are configured by filename, where the photos must be placed in the directory. such as: filename = ' koala.jpg '

2) filename to find the starting position of the string. findstr (' koala.jpg ', '. jpg ') = 6, so you can take the filename minus the suffix name

3) Get a directory of. \ filename out\

4) Create the directory

5) filestr = Directory + file name, for subsequent data output, easy to output to. \ filename out\ directory. Here Filestr = '. \koalaout\koala '

2. Call the MATLAB function to convert RGB to YUV
yuvimg = RGB2YCBCR (rgbimg);     %%% RGB--Yuvfigure;imshow ((yuvimg));
3. Remove YUV Separately

Y,U,V Data Retention matrix storage, easy to follow the 4:2:2,4:2:0 sampling, more intuitive

[ImgHeight imgwidth Imgdim] = size (yuvimg);         %%len = Imgheight*imgwidth*imgdim;yuvimout = Zeros (1,len); Y = Yuvimg (:,:, 1);     % Y-Matrix U = yuvimg (:,:, 2);     % U Matrix V = yuvimg (:,:, 3);     % V Matrix Yvec = Reshape (yuvimg (:,:, 1) ', 1,[]); % matrix finishing rows Vector Uvec = Reshape (yuvimg (:,:, 2) ', 1,[]); Vvec = Reshape (yuvimg (:,:, 3) ', 1,[]); Yuvimout (1:3:len) = Yvec;yuvimout (2:3:len) = Uvec;yuvimout (3:3:len) = Vvec;

Note: When reshape a matrix, it is necessary to transpose the result to ensure that the matrix is organized by rows

4. Output YUV444 data to a file
%%% the output image of YUV data to. txt%if Yuv444_out_txt = = YES    filename = [filestr ' _444.txt '];    fid= fopen (filename, ' w ');        fprintf (FID, '%02x\n ', yuvimout);    Fclose (FID);    Disp (' Yuv444_out_txt YES '); else    disp (' Yuv444_out_txt NO '); end

In this example: filename = [filestr ' _444.txt '] = '. \koalaout\koala_444.txt '

%%% output image of YUV data to. yuv% four pixels: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 v3]%% stored stream: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3 ]%% mapped pixels: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 v3]if YUV444_OUT_YUV = = YES    filename = [filestr ' _444.yuv '];    fid= fopen (filename, ' WB ');        Fwrite (fid,yuvimout, ' uint8 ');    Fclose (FID);    Disp (' Yuv444_out_yuv YES '); else    disp (' Yuv444_out_yuv NO '); end
5. Turn Yuv4:4:4 into YUYV 4:2:2 packed
%%% yuv4:4:4-->> yuyv 4:2:2% four pixels: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 v3]%% stored bitstream: [Y0 U0] [Y1 V1] [Y2 U2] [Y3 v3]%% mapped pixels: [Y0 U0 V1] [Y1 U0 V1] [Y2 U2 V3] [Y3 U2 v3]%%%len = Imgheight*imgwidth+imgheight*imgwidth/2+imgheight*imgwidt H/2;yuv422out = zeros (1,len); yuv422sampy = Y;yuv422sampu = U (:, 1:2:size (u,2)); YUV422SAMPV = V (:, 2:2:size (V,2)); Yuv422out (1:2:len) = reshape (Yuv422sampy ', 1,[]);  %%% Note to transpose yuv422out (2:4:len) = reshape (Yuv422sampu ', 1,[]); Yuv422out (4:4:len) = reshape (YUV422SAMPV ', 1,[]);
Description

1) The 4:2:2 mode is reserved for all y,u,v that are sampled at a point of 1 columns per interval, as shown in the code.

2) Yuyv 4:2:2 packed that is, the pixel value is the mode of YU/YV alternating storage. Stored streams: [Y0 U0] [Y1 V1] [Y2 U2] [Y3 V3]

Output data to File
%%% output image yuv422 data to. txt%if Yuv422_out_txt = = YES    filename = [filestr ' _422.txt '];    fid= fopen (filename, ' w ');        fprintf (FID, '%02x\n ', yuv422out);    Fclose (FID);    Disp (' Yuv422_out_txt YES '); else    disp (' Yuv422_out_txt NO '); end% output yuyv422 to. YUV Fileif YUV422_OUT_YUV = yes< C6/>filename = [filestr ' _422yuyv.yuv '];    fid= fopen (filename, ' WB ');        Fwrite (fid,yuv422out, ' uint8 ');    Fclose (FID);    Disp (' Yuv422_out_yuv YES '); else    disp (' Yuv422_out_yuv NO '); end
6. Turn Yuv4:4:4 into YV12 4:2:0 planar
%%% yuv4:4:4-->> yuyv 4:2:0% output yuyv422 to. YUV file% first row four pixels: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 v3]% second row Four pixels: [Y4 U4 V4] [Y5 U5 V5] [Y6 U6 V6] [Y7 U7 v7]% 4:2:0 sampling% first line sampled pixels: [Y0 U0   ] [Y1      ] [Y2 U2]   [Y3      ]% second row sampling Pixels: [Y4    V4] [Y5      ] [Y6    V6] [Y7]% 420yv12      Planar storage stream: [Y0 Y1 Y2 Y3 Y4 Y5 Y6 Y7] [V4 V6] [U0 u2]% mapped pixels: 
   [y0 U0 V4] [Y1 U0 V4] [Y2 U2 V6] [Y3 U2 v6]% [Y4 U0 V4] [Y5 U0 V4] [Y6 U2 V6] [Y7 U2 v6]%%%len             = imgheight*imgwidth +imgheight*imgwidth/4+imgheight*imgwidth/4;yuv420out = [];yuv420sampy = Y;yuv420sampu = U (1:2:size (U,1), 1:2:size (U,2 ): YUV420SAMPV = V (2:2:size (v,1), 1:2:size (v,2)),%%%yuv420out = [y V u]  % yuv420 yv12 formatyuv420out = [Yuv420out res Hape (Yuv420sampy ', 1,[]);    %Y Note to transpose yuv420out = [yuv420out reshape (YUV420SAMPV ', 1,[])];    %vyuv420out = [yuv420out reshape (Yuv420sampu ', 1,[])];    %u
Description

1) Yuv420sampu = U (1:2:size (u,1), 1:2:size (u,2)); From the first line of the picture, each interval of 1 lines and 1 columns per interval sample one U

2) YUV420SAMPV = V (2:2:size (v,1), 1:2:size (v,2)); From the second line of the picture, each interval of 1 lines and 1 columns per interval is sampled by a V

Data data to File
%%% output image yuv422 data to. txt%if Yuv420_out_txt = = YES    filename = [filestr ' _420.txt '];    fid= fopen (filename, ' w ');        fprintf (FID, '%02x\n ', yuv420out);    Fclose (FID);    Disp (' Yuv420_out_txt YES '); else    disp (' Yuv420_out_txt NO '); end% output yuyv420 to. YUV Fileif YUV420_OUT_YUV = yes< C6/>filename = [filestr ' _420yv12.yuv '];    fid= fopen (filename, ' WB ');        Fwrite (fid,yuv420out, ' uint8 ');    Fclose (FID);    Disp (' Yuv420_out_yuv YES '); else    disp (' Yuv420_out_yuv NO '); end

Watch the command output and wait for it to close

DISP ('---program run susseed---');d the ISP ('---Press any key to close all figure---'), System (' pause '); close all;

Open the output YUV file to view the picture

Reference
Http://www.cnblogs.com/xkfz007/archive/2012/07/31/2616806.html
Http://www.xuebuyuan.com/1541892.html
http://www.fourcc.org/yuv.php

Code Link:

Http://pan.baidu.com/s/1pLMRcmB

MATLAB read an RGB image into YUV format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.