views:

69

answers:

5

I use zeros to initialize my matrix like this:

height  = 352
width   = 288
nFrames = 120
imgYuv=zeros([height,width,3,nFrames]);

However, when I set the value of nFrames larger than 120, MATLAB gives me an error message saying out of memory.

The original function is

[imgYuv, S, A]= changeYuv(fileName, width, height, idxFrame, nFrames)

my command is

[imgYuv,S,A]=changeYuv('tilt.yuv',352,288,1:120,120);

Can anyone please tell me what's going on here?

PS: one of the purposes of the function is to load a yuv video which consists more than 2000 frames. Is there any possibility to implement that?

+2  A: 

Yes, you (or rather, your Matlab session) are running out of memory.

Get out your calculator and find the product height x width x 3 x nFrames x 8 which will tell you how much memory you have tried to get in your call to zeros. That will be a number either close to or in excess of the RAM available to Matlab on your computer.

High Performance Mark
okay i see. but is there any other way to load the file? the file consists of 2000 plus frames...any possibility?
appi
+1  A: 

The function B = zeros([d1 d2 d3...]) creates an multi-dimensional array with dimensions d1*d2*d3*... Depending on width and height, given the 3rd dimension of 3 and the 4th dimension of 120 (which effectively results in width*height*360), may result in a very huge array. There are certain memory limits on every machine, maybe you reached these... ;)

Flinsch
okay i see. but is there any other way to load the file? the file consists of 2000 plus frames...any possibility?
appi
Is it necessary to load the complete file in a bunch? A solution could be to "stream" the file and just load smaller parts of the file at one time and discard the data when the next data block has been loaded and processed (e.g., displayed or what ever).
Flinsch
not really necessary. Yes, you are right. I should try loading a small portion each time. Thanks Flinsch!
appi
+2  A: 

Your command is:

[imgYuv,S,A]=changeYuv('tilt.yuv',352,288,1:120,120);

That is:

352*288*120*120 = 1459814400

That is 1.4 * 10^9. If one object has 4 bytes, then you need 6GB. That is a lot of memory...

eumiro
okay i see. but is there any other way to load the file? the file consists of 2000 plus frames...any possibility?
appi
Can you iterate over the frames and work on one at a time?
eumiro
Hmm, make sense. Let me try. Thanks eumiro!
appi
+2  A: 

Referencing the code I've seen in your withdrawn post, your calculating the difference between adjacent frame histograms. One option to avoid massive memory allocation might be to just hold two frames in memory, instead of reading all the frames at once.

zellus
Yes zellus. That's exactly what I'm trying to do. Thanks for your suggestion. I will try it.
appi
+4  A: 

There are three ways to avoid the error

  1. Process a limited number of frames at any given time.
  2. Work with integer arrays. Most movies are in 8-bit format, while Matlab normally works with doubles. uint8 takes 1 byte per element, while double takes 8 bytes. Thus, if you create your array as B = zeros(height,width,3,nFrames,'uint8)`, it only uses 1/8th of the memory. This might work for 120 frames, though for 2000 frames, you'll run again into trouble. Note that not all Matlab functions work for integer arrays; you may have to reimplement those that require double.
  3. Buy more RAM.
Jonas
Now I see. Thanks for pointing that out.
appi