MATLAB Answers

Why does video slow down towards the end?

5 views (last 30 days)
Sam
Sam on 6 Aug 2020
Edited: Aman Vyas on 12 Aug 2020
Hello,
I've written a code to display circles on my video every 100th frame (9000 frames in total, 25fps). At the first frame, I draw 2 circles on the image and store the vertices of these circles. Later on, I display these circles again at each 100th frame. I've read somewhere that plotting could slow down the execution of this code, so could this be the reason why my video is playing way slower at the end compared to the beginning?
vid = VideoReader(video_name);
curr_axes = axes;
hfig = figure(1);
set(hfig,'WindowButtonDownFcn',@count_the_clicks) %Use callback to click
framecount = 0;
%Play video
while vid.hasFrame()
temp = vid.readFrame();
framecount = framecount + 1;
image(temp,'Parent',curr_axes);
%Draw 2 circles on the first videoframe, so that you can plot these circles
%again at each 100th frame (see below).
if framecount == 1
hfig6 = copyobj(hfig,0);
set(hfig6,'visible','off')
figure(hfig);
disp('Carefully select 2 objects you want to label by drawing ellipses')
p2 = imellipse;
vert_p2 = getVertices(p2);
wait(p2);
p3 = imellipse;
vert_p3 = getVertices(p3);
wait(p3);
vert_p_all = horzcat(vert_p2',vert_p3')';
end
curr_axes.Visible = 'off';
pause(0.55/vid.FrameRate)
%At each 100th frame, plot the drawn circles
if mod(framecount,100)==0
hold on
scatter(vert_p_all(:,1),vert_p_all(:,2),16,'oy','filled')
p1 = imrect; %Double-click to proceed to the next 100 frames of the video
wait(p1);
end
end

Answers (1)

Aman Vyas
Aman Vyas on 12 Aug 2020
Edited: Aman Vyas on 12 Aug 2020
Hi,
You can try either of the following or both of the following as both would result in preventing the lag or delay in the video as it progresses.
1) You are trying to store images in the axes, since function image() is used.Try inserting a "cla('reset')" just before and see if that speeds it up. Otherwise you may be loading all of the images in there instead of replacing them.
2) Also, you have used "hold on" and haven't used "hold off" because of which all of the images are loaded to the same axes everytime and slows down as video progresses. Try using "hold off" function in the loop.
For more info on hold off function you can visit the links:
Hope it helps !

Community Treasure Hunt

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

Start Hunting!

Translated by