Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Getting frames from .avi gets very slow...

Subject: Getting frames from .avi gets very slow...

From: Tom F

Date: 27 Jul, 2009 19:39:01

Message: 1 of 11

Hi all,

I've written a few functions using mmreader() to load in an avi file, and then using read(myvideo,x) to read in a frame as an image.

I've noticed that if one does this cumulatively in a loop, even if I'm not storing each frame, the function runs very slowly.

For example, if I run this code:

tic
for i=1:100
     myframe=read(myvideo,i);
     if mod(i,10)==0
          toc
          tic
     end
end

I get the following timing outputs:

Elapsed time is 1.309798 seconds.
Elapsed time is 1.883603 seconds.
Elapsed time is 2.448006 seconds.
Elapsed time is 3.025906 seconds.
Elapsed time is 3.570710 seconds.
Elapsed time is 4.134835 seconds.
Elapsed time is 4.704180 seconds.
Elapsed time is 5.256315 seconds.
Elapsed time is 5.817437 seconds.
Elapsed time is 6.347105 seconds.

While I understand that the time it takes to load a frame will vary based on a lot of things, I don't get why the delay is accumulating (why do the last ten frames take so much longer to load than the first ten?!). I'm not great at writing efficient code, but this a real bottleneck for me, can someone point me toward an answer?

Many thanks, T
(Matlab 7.6.0 on a MacBook Pro)

Subject: Getting frames from .avi gets very slow...

From: Tom F

Date: 28 Jul, 2009 16:54:02

Message: 2 of 11

Please?!

Subject: Getting frames from .avi gets very slow...

From: us

Date: 28 Jul, 2009 17:23:05

Message: 3 of 11

"Tom F" <tfoulsh@talk21.com> wrote in message <h4nafa$lgp$1@fred.mathworks.com>...
> Please?!

how can anybody at CSSM possibly help you without knowing what READ in

     myframe=read(myvideo,i);

does...
eg, if READ(...,I) reads the whole file up to frame I, your timing certainly is not much of a surprise...

us

Subject: Getting frames from .avi gets very slow...

From: Tom F

Date: 28 Jul, 2009 18:01:03

Message: 4 of 11

Thanks for the comment, maybe I wasn't clear, READ is a MATLAB function which you can look up. As I understand it, MATLAB's read(obj, index) function reads in 1 or multiple frames from a video. In my loop it should only read the i-th frame (i.e. 1 frame at a time). If I'm only reading one frame, should it take me longer to read the 100th than the 1st? If this is in fact what READ is doing then any suggestions on how to make it more efficient?

T

"us " <us@neurol.unizh.ch> wrote in message <h4nc5o$h25$1@fred.mathworks.com>...
> "Tom F" <tfoulsh@talk21.com> wrote in message <h4nafa$lgp$1@fred.mathworks.com>...
> > Please?!
>
> how can anybody at CSSM possibly help you without knowing what READ in
>
> myframe=read(myvideo,i);
>
> does...
> eg, if READ(...,I) reads the whole file up to frame I, your timing certainly is not much of a surprise...
>
> us

Subject: Getting frames from .avi gets very slow...

From: us

Date: 28 Jul, 2009 18:12:01

Message: 5 of 11

"Tom F" <tfoulsh@talk21.com> wrote in message <h4necv$dk1$1@fred.mathworks.com>...
> Thanks for the comment, maybe I wasn't clear, READ is a MATLAB function which you can look up...

there's many a READ - be MORE specific...
- which READ do you refer to (tbx?)...
- which ML/tbx version do you use...

in general, CSSMers do not have a crystal ball...
us

Subject: Getting frames from .avi gets very slow...

From: Tom F

Date: 28 Jul, 2009 19:03:01

Message: 6 of 11

OK, sorry for being vague and thanks for your help.

I'm using Matlab 7.6.0 (R2008a) on a Mac. I do have the Image Processing Toolbox but both READ and MMREADER are core matlab functions. My matlab help points to only one function called READ. The documentation for these functions is here

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/mmreader.html
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/read.html

There are several examples in this documentation, and using READ is the suggested way to access frames of a multimedia file. But I can't find any references to my problem. To re-iterate: I need to loop through all the frames of a movie, but this takes an inordinate amount of time, even for short clips. This seems to be because successive calls to READ get slower and slower when accessing later frames. All suggestions much appreciated.

T

Subject: Getting frames from .avi gets very slow...

From: Nathan

Date: 28 Jul, 2009 19:11:31

Message: 7 of 11

On Jul 28, 12:03 pm, "Tom F" <tfou...@talk21.com> wrote:
> OK, sorry for being vague and thanks for your help.
>
> I'm using Matlab 7.6.0 (R2008a) on a Mac.  I do have the Image Processing Toolbox but both READ and MMREADER are core matlab functions.  My matlab help points to only one function called READ.  The documentation for these functions is here
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/mmreader.htmlhttp://www.mathworks.com/access/helpdesk/help/techdoc/ref/read.html
>
> There are several examples in this documentation, and using READ is the suggested way to access frames of a multimedia file.  But I can't find any references to my problem.  To re-iterate: I need to loop through all the frames of a movie, but this takes an inordinate amount of time, even for short clips.  This seems to be because successive calls to READ get slower and slower when accessing later frames.  All suggestions much appreciated.
>
> T

Does reading in their example movie do the same thing?

readerobj = mmreader('xylophone.mpg', 'tag', 'myreader1');
tic
for i=1:100
     myframe=read(readerobj,i);
     if mod(i,10)==0
          toc
          tic
     end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Elapsed time is 0.080490 seconds.
Elapsed time is 0.064718 seconds.
Elapsed time is 0.057035 seconds.
Elapsed time is 0.057919 seconds.
Elapsed time is 0.055034 seconds.
Elapsed time is 0.061578 seconds.
Elapsed time is 0.061231 seconds.
Elapsed time is 0.067209 seconds.
Elapsed time is 0.065392 seconds.
Elapsed time is 0.059605 seconds.

(Even for larger ranges of frames I get an average time)

tic
for i=1:1000
     myframe=read(readerobj,i);
     if mod(i,100)==0
          toc
          tic
     end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Elapsed time is 0.665787 seconds.
Elapsed time is 0.606082 seconds.
Elapsed time is 0.601284 seconds.
Elapsed time is 0.583804 seconds.
Elapsed time is 0.599731 seconds.
Elapsed time is 0.580057 seconds.
Elapsed time is 0.613274 seconds.
Elapsed time is 0.613252 seconds.
Elapsed time is 0.600569 seconds.
Elapsed time is 0.582987 seconds.

That's what I get. I'm using windows XP matlab version r2009a.
Based off of this example, what do your times come out? Is it a
problem with your video for some reason?
-Nathan

Subject: Getting frames from .avi gets very slow...

From: Tom F

Date: 28 Jul, 2009 20:37:02

Message: 8 of 11

Hmm, yes it seems to work better with their video, this is what I get for every 10 frames up until 100. My system seems to be much slower than yours, but the times don't seem to vary all that much.

Elapsed time is 0.445977 seconds.
Elapsed time is 0.304754 seconds.
Elapsed time is 0.309246 seconds.
Elapsed time is 0.299069 seconds.
Elapsed time is 0.301987 seconds.
Elapsed time is 0.296940 seconds.
Elapsed time is 0.292025 seconds.
Elapsed time is 0.288584 seconds.
Elapsed time is 0.292308 seconds.
Elapsed time is 0.306057 seconds.

I couldn't go much further as the xylophone clip is only 141 frames on my system. It's also a much smaller clip (240 x 320 x 3) compared to my clips (480 x 640 x 3). I was starting with 30 second clips (~900 frames) but hoping my function would also work on much longer clips with thousands of frames! Could it just be the larger video? That still doesn't seem to explain why the delay gets longer though. Is there anything fundamental I'm missing about what READ is doing? I've also come across this comment from a while back which suggests that AVIREAD has to read in all frames prior to the one you want, which would explain why it gets longer if mmreader is doing a similar thing:

http://www.archivum.info/comp.soft-sys.matlab/2008-04/msg02388.html

Thanks.

Subject: Getting frames from .avi gets very slow...

From: Nathan

Date: 28 Jul, 2009 20:48:46

Message: 9 of 11

On Jul 28, 1:37 pm, "Tom F" <tfou...@talk21.com> wrote:
> Hmm, yes it seems to work better with their video, this is what I get for every 10 frames up until 100.  My system seems to be  much slower than yours, but the times don't seem to vary all that much.  
>
> Elapsed time is 0.445977 seconds.
> Elapsed time is 0.304754 seconds.
> Elapsed time is 0.309246 seconds.
> Elapsed time is 0.299069 seconds.
> Elapsed time is 0.301987 seconds.
> Elapsed time is 0.296940 seconds.
> Elapsed time is 0.292025 seconds.
> Elapsed time is 0.288584 seconds.
> Elapsed time is 0.292308 seconds.
> Elapsed time is 0.306057 seconds.
>
> I couldn't go much further as the xylophone clip is only 141 frames on my system.  It's also a much smaller clip (240 x 320 x 3) compared to my clips (480 x 640 x 3).  I was starting with 30 second clips (~900 frames) but hoping my function would also work on much longer clips with thousands of frames!  Could it just be the larger video? That still doesn't seem to explain why the delay gets longer though.  Is there anything fundamental I'm missing about what READ is doing?  I've also come across this comment from a while back which suggests that AVIREAD has to read in all frames prior to the one you want, which would explain why it gets longer if mmreader is doing a similar thing:
>
> http://www.archivum.info/comp.soft-sys.matlab/2008-04/msg02388.html
>
> Thanks.

Note: I tried the example on r2007b and it was returning values about
twice as slow as yours. I guess optimization has occurred for this
function across versions.

But still, I don't know why your video would do that when reading it
in.
Also: My version of the video lets me read 1500+ frames from it...
Perhaps they gave different versions to different releases?

I'm stumped now as well.

Sorry I couldn't be much of help.
-Nathan

Subject: Getting frames from .avi gets very slow...

From: T

Date: 3 Aug, 2009 11:28:02

Message: 10 of 11

what you are probably overloking is the encoding of the avi file. an AVI is just a containere; i.e. it can contain a videa packed in a number of ways. If it is packes as mjpeg, it is just a bunch of jpeg files, and thus easy to decode. If it is packed in some .h264 codec, only differential information between conescutive frames is tsored. To know what is in frame 10, you need to know what is in frame 1 through 9. Maybe the read algorithm is just really slow, so what you could to is use an optimized algorithm to convert the codec of your video file from a small file/slow readable one to a large file/fast readable codec (like mjpeg). use virtualdub or tmpe for this.
Or you can use an optimized video2frames extraction program, or maybe just read in all the frames in matlab at once, and then run your code on them


"Tom F" <tfoulsh@talk21.com> wrote in message <h4nnhe$77v$1@fred.mathworks.com>...
> Hmm, yes it seems to work better with their video, this is what I get for every 10 frames up until 100. My system seems to be much slower than yours, but the times don't seem to vary all that much.
>
> Elapsed time is 0.445977 seconds.
> Elapsed time is 0.304754 seconds.
> Elapsed time is 0.309246 seconds.
> Elapsed time is 0.299069 seconds.
> Elapsed time is 0.301987 seconds.
> Elapsed time is 0.296940 seconds.
> Elapsed time is 0.292025 seconds.
> Elapsed time is 0.288584 seconds.
> Elapsed time is 0.292308 seconds.
> Elapsed time is 0.306057 seconds.
>
> I couldn't go much further as the xylophone clip is only 141 frames on my system. It's also a much smaller clip (240 x 320 x 3) compared to my clips (480 x 640 x 3). I was starting with 30 second clips (~900 frames) but hoping my function would also work on much longer clips with thousands of frames! Could it just be the larger video? That still doesn't seem to explain why the delay gets longer though. Is there anything fundamental I'm missing about what READ is doing? I've also come across this comment from a while back which suggests that AVIREAD has to read in all frames prior to the one you want, which would explain why it gets longer if mmreader is doing a similar thing:
>
> http://www.archivum.info/comp.soft-sys.matlab/2008-04/msg02388.html
>
> Thanks.

Subject: Getting frames from .avi gets very slow...

From: Tom F

Date: 4 Aug, 2009 17:10:21

Message: 11 of 11

Thanks T, that makes a lot of sense. I knew a little about codecs but was still having trouble working out why it would need to get frames 1-9 before getting 10, but your explanation sounds right. I will try converting my movies to a better format first.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us