drawnow() causing very slow display of images

Hi all,
I've read several other questions on this but didn't find a satisfactory answer. My apologies however, if this is already up.
I am building a GUI to display processed video. I do this using
imshow(image,'Parent',axes)
then
drawnow
for each frame in a loop. It is VERY slow. Approx 0.5 seconds per frame (~12 seconds to display 1 second of video in real time). It also accounts for half my total processing time per frame.
Can I make it faster?
Any help is greatly appreciated!!
Cheers, KB

 Accepted Answer

I solved that problem with older versions of MATLAB by putting in a "cla" or "cla('reset')" into the code just before the imshow. Evidently with older versions sometimes the images "stack up" and are all in there and you're just piling one on top of another and it gets slower and slower as it runs out of memory. Try that and let me know if it speeds it up.

7 Comments

By the way, I know for real time application, Sean recommends writing to CData in the axes rather than using imshow().
Hi Image Analyst,
Yes, I am using cla each iteration of the loop. Display is not getting slower, it's just consistently slow!
Not 100% sure what you mean by writing to CData but will have a look into it now.
Thanks for the suggestions.
Well drawnow repaints the entire GUI. If you just use set() to set the cdata property of the axes to be your image it will go faster and without some of the overhead of ishow(). Sean or Alex could explain more.
Ok, updating CData is MUCH faster (~0.003 seconds) compared to 0.12 seconds for imshow().
Sadly this has not helped me. It seems the
drawnow
command is what is taking the time. I built a test GUI with a single axes and button, and it is very fast. However, my GUI w multiple axes, uitables, plots etc is very slow. I assume this is because, as you mentioned, drawnow is updating everything. I tried omitting the drawnow, but then images don't get refreshed. Omitting drawnow and adding a short pause, every 2nd or 3rd image get refreshed (unfortunately unnacceptable for my application).
I'm hesitant to accept this answer because, although it has definitely been helpful, it has not solved my problem. Maybe I'm still missing something?
It may be taking a long time because it's the first time reading your video and it's not cached. Try setting up a ramdisk then: http://www.pcworld.com/article/260918/how_to_supercharge_your_pc_with_a_ram_disk.html Put your video on that.
Updating CData was the answer I needed (I just had to implement it poperly). MY GUI is now running much faster. Example code pasted below, maybe it will help someone else.
Thanks very much Image Analyst :)
Updating CData vs imshow() example code:
%%Setup video
filename = 'your_video_here.mp4';
vidObj = VideoReader(filename);
%%Display first frame and setup handle to image
frame = read(vidObj, 1);
figure;
h = imshow(frame);
%%Loop through subsequent frames and refresh 'CData'
for k = 1:1000
tic
frame = read(vidObj, k);
set(h,'Cdata',frame);
drawnow
toc
end
for k = 1:1000
tic
frame = read(vidObj, k);
imshow(frame)
drawnow
toc
end
It works ! Thanks !

Sign in to comment.

More Answers (1)

I solved a similar issue by adding a very short pause, this will clean up the buffer and speed up the pseudo-video. Add:
pause(0.001)
right after drawnow

1 Comment

Hi David,
Thanks for the suggestion. Unfortunately it has not helped me :( in fact it made execution a little slower.

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Products

Asked:

on 18 Feb 2014

Commented:

on 13 May 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!