I need to display updated images as fast as possible in a Matlab figure. Each image is processed and then displayed. However, the speed of displaying of color images is pretty slow. For example, when I run the following code
videoObj = VideoReader('sample.avi');
nFrames = videoObj.NumberOfFrames;
h = videoObj.Height;
w = videoObj.Width;
mov(1:nFrames) = struct('cdata', zeros(h, w, 3, 'uint8'), 'colormap', []);
for k = 1 : nFrames
mov(k).cdata = read(interObj, k);
end
tic
for i=1:nFrames
frame = mov(i).cdata;
image(frame);
drawnow;
end
secPerFrame = toc/nFrames
it takes secPerFrame = 0.012
seconds to update each frame. Where each frame is a 640x480 pixel RGB image. So, if I want to process a video stream at 30 frames per second, this leaves "only" 0.033 - 0.012 = 0.021
seconds for actual image processing after subtracting the overhead related to image display.
Is there a faster way to update image objects in Matlab?
If all of the images have same size, you can use the image
command once, and then update the CData
property of the axes.
videoObj = VideoReader('sample.avi');
nFrames = videoObj.NumberOfFrames;
mov(1:nFrames) = ...
struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'), 'colormap', []);
for k = 1 : nFrames
mov(k).cdata = read(interObj, k);
end
tic
image(mov(1).cdata);
imageHandle = get(gca,'Children');
for i=1:number
frame = mov(i).cdata;
set(imageHandle ,'CData',frame);
drawnow;
end
secPerFrame = toc/number
This will still be slow, since Matlab figures are not optimized for showing videos. If the application allows to do pre-processing, save it as a movie file to the disk, and call external program to show it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With