No BSD License  

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Convert YUV CIF 4:2:0 video file to image files

4.6 | 13 ratings Rate this file 78 Downloads (last 30 days) File Size: 2.61 KB File ID: #6318 Version: 1.0

Convert YUV CIF 4:2:0 video file to image files


Da Yu (view profile)


17 Nov 2004 (Updated )

Load the YUV file, and convert it to 2D image file (matrix form).

| Watch this File

File Information

convert YUV CIF 4:2:0 video file to image files

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (29)
29 Apr 2016 LOGO

LOGO (view profile)

25 Mar 2015 Nikhil singh

It reads individual frame very well. To read the whole video I run a loop. But how can I check that all the frames have been read. Does it have any parameter to count total number of frames so that I can break the loop after that ?

Actually it keeps on reading the frames in a circular fashion i.e after last frame comes the first frame and so on.

One solution is to check if any frame matrix matches exactly to the first frame then the frames are same hence the video is finished. But that can happen before the end also.

I am new to Matlab so maybe I am missing something basic. TIA

Comment only
15 Dec 2014 david

david (view profile)

Hi, it works well but for 'saveFileYuv' i wonder if the chroma downsampling is correct. The function takes only one value in four. Should we rather take the average of the 4 values ?

Comment only
26 Sep 2014 yogesh

yogesh (view profile)

17 Sep 2014 Hossam Amer

What would you suggest me to do if I would like to extract some frames from two YUV files to produce extracted videos and then combine the extracted videos?

The YUV file is the output of HEVC coding.

Comment only
21 Nov 2013 ahmed

ahmed (view profile)

21 Nov 2013 ahmed

ahmed (view profile)


29 Sep 2013 Chaine San Buenaventura

What is idxFrame in loadFileYUV? I'd appreciate any respone :)

Comment only
29 Sep 2013 Chaine San Buenaventura

What is the output the loadFileYUV?

Comment only
06 Apr 2013 jake

jake (view profile)

any onecan help how to use"loadFileYuv"this function?

i type

>> loadFileYuv(test1.yuv,176,144,91)
Undefined variable "test1" or function "test1.yuv".


Comment only
23 Mar 2013 Today

Today (view profile)

It works well. Thanks

31 Jan 2013 Muhamamd

Any body can help how to use this code?

Comment only
26 Oct 2012 Chethan Pandarinath

This code is great. It can be sped up dramatically though by rewriting the clipValue function in a vectorized fashion. That is, replace the code in clipValue.m with these two lines:

val(val < valMin) = valMin;

val(val > valMax) = valMax;

for me this sped up conversion from >8s to <0.3s for a 2560 pixels x 1280 pixels image.

26 Oct 2012 Chethan Pandarinath  
17 Jun 2012 Vijay Ukani

Can I convert a YUV file to .ras image sequence? Someone plz help

Comment only
13 Jun 2012 65uyh

65uyh (view profile)

I Am try to execute function [mov,imgRgb] = loadFileYuv(fileName, width, height, idxFrame) but matlab send to me this message :-
??? function [mov,imgRgb] = loadFileYuv(fileName, width, height, idxFrame)
Error: Function definitions are not permitted in this context.

Comment only
05 May 2012 Shashi barot

As of now, I am using CIF video,but how does it work for interlaced video?

Comment only
11 Apr 2012 Shashi barot

I am little confused how he's separating Y,U and V components here,because I am not sure how data is saved in a YUV file. when he first uses uses buf = fread(fileId, width * height, 'uchar'); and after reshaping this data he uses fread(fileId,width/2*height/2,'uchar')

that is 1/4 of the data used in previous 'fread' function and because there is no fseek between these 2 fread function and the next fread function the read data should be the same(atleast first 1/4 part of the data read using fread(fileId,height*width,'uchar') and fread(fileID,height/2*width/2,'uchar')).I am also not getting why/how he's using 'kron' to reshape the U and V components.

Comment only
10 Apr 2012 MechtEngineer

Shashi, I believe you are referring to the line
sizeFrame = 1.5 * width * height;
occuring in the file loadFileYuv.m.

The author is preparing enough memory to be able to load one whole YUV (or YCbCr) 4:2:0 frame. This type of video encoding is called "Chroma Subsampling". 4:2:0 means that all luminance values of the scene are recorded, but Cb and Cr are each subsampled at a factor of 2 both horizontally and vertically. This means the total amount of data required to store each frame is:
Total = luminance + Cb + Cr
Total = full resolution + 1/4 res + 1/4 res
Total = 1.5 * full resolution

For more information, see Wikipedia's article on Chroma Subsampling:

Chroma subsampling is also discussed at 24:21 minutes through A Digital Media Primer for Geeks at:

10 Apr 2012 Shashi barot

why is the size of one frame equals 1.5*height*width ?

Comment only
05 Feb 2012 MechtEngineer

elena, I just use the File>import function in MATLAB. This can also generate the code for you to use to import a particular file in the future.

Comment only
20 Jan 2012 elena

elena (view profile)

I have matlab 7.0.1 and unfortunately i can't find functions for yuv, e.g i want to load yuv file into matlab and i cannot find the appropriate function, help? thank you

Comment only
19 Oct 2011 MechtEngineer

"idxFrame" can be entered as a vector containing the frame numbers you desire to be extracted from the yuv movie.

e.g. idxFrame = 200:250; would extract frames 200 to 250 from the yuv file.

19 Oct 2011 MechtEngineer

"idxFrame" is the frame number within the YUV 4:2:0 video you want extracted to an RGB image. Thus, each time you run the function loadFileYuv, it will open the YUV file, extract the specified frame from the video file in the fileName string, and close the YUV file.

For sample YUV 4:2:0 files, use the Video Trace Library at

A versatile and extremely reliable YUV player is located here:
It is now freeware, as evidenced by here:

19 Aug 2011 MechtEngineer

Currently the program lacks an example of how to use the code.

e.g. in the file, loadFileYuv.m, the function call is:
function [mov,imgRgb] = loadFileYuv(fileName, width, height, idxFrame)
What is idxFrame? I thought it might be frames per second, but the code seems to indicate it is some sort of structure. Does this mean the YUV video file needs to be loaded into MATLAB as a variable before this subroutine is used?

It seems like this code may work well, but it just needs an example so most people can use it!! Any help would be appreciated.

Comment only
01 Jun 2010 Naeem

Naeem (view profile)

i need to convert yuv sequences downloaded from
into 4:2:0 yuv video file.
if you have any idea. please help me out.

03 Mar 2009 Fraser Stirling

I need to convert a YUV file into a YUV16 file and also a RAW file. Then I need to convert back from these two variants to YUV.

Could this be altered to accomodate this?

Comment only
25 Feb 2008 Jeo Jaje

It seems a good code. But I need reverse of this. I want to write a 4:2:0 YUV file from a sequence of 2D image files (matrix form). Any idea? please help.

30 Nov 2004 test test  

Contact us