speeding up mpg video frame reading

18 views (last 30 days)
Levente Gellért
Levente Gellért on 19 Nov 2025 at 13:04
Commented: Levente Gellért ongeveer 6 uur ago
Dear MAtlab Community, I am using the below code to read a .mpg video file fram by frame and track movement by manually putting the cursor onto an animal and get coordinates in each frame.
It seems to me that the speed of the playback(showing each images) runs quasi at the original speed of the video. Is there a possibility to speed the proceess up, for a faster analysis?
Many thanks for your suggestions!
[datfile,location,indx] = uigetfile({'*.mpg'});
v = VideoReader(datfile);
vidObj = VideoReader(datfile);
numFrames = ceil(vidObj.FrameRate*vidObj.Duration); %if framerate is stable
currAxes = axes;
koord=zeros(numFrames,2);
set (gcf, 'WindowButtonMotionFcn',@mouseMove2);
numFrames=0;
y1=1;
y2=v.Height;
%%
while hasFrame(v)
numFrames=numFrames+1;
i=numFrames;
vidFrame = readFrame(v);
image(vidFrame, 'Parent', currAxes);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%
C = get (gca, 'CurrentPoint');
koord(i,1)= round(C( 1,1));
koord(i,2)= round(C( 1,2));
x(1,i)=C( 1,1);
y(1,i)=C( 1,2);
currAxes.Visible = 'off';
line(x,y,'Color','r','LineWidth',2)
%
f= waitbar(i/length(koord));
clear vidFrame
end
  3 Comments
Levente Gellért
Levente Gellért ongeveer 3 uur ago
Dear dpb, silly mistake, just corrected, no change in speed.
Thanks
lg
dpb
dpb ongeveer 2 uur ago
How about edit original Q? to reflect actual implementation so everybody will be on the same page?
Couldn't you just set the original figure and then just set the 'CData' property to the new image data and dispense with all the other machinations?
I'd think the speed would be controlled by how long it takes you to select the new point; if you weren't doing that it would zip right on through, wouldn't/doesn't it?

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 19 Nov 2025 at 16:25
seems to me the simplest change to gain speed is to remove this line : clear vidFrame
I could get a speed increase of about 60%
other minor improvements in the code can also be done :
[datfile,location,indx] = uigetfile({'*.mpg'});
vidObj = VideoReader(datfile);
numFrames = ceil(vidObj.FrameRate*vidObj.Duration); %if framerate is stable
currAxes = axes;
koord=zeros(numFrames,2);
set (gcf, 'WindowButtonMotionFcn',@mouseMove2);
k=0;
y1=1;
y2=vidObj.Height;
%%
tic
while hasFrame(vidObj)
k=k+1;
vidFrame = readFrame(vidObj);
image(vidFrame, 'Parent', currAxes);
% set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%
C = get (gca, 'CurrentPoint');
% koord(k,1)= round(C( 1,1));
% koord(k,2)= round(C( 1,2));
x(1,k)=C( 1,1);
y(1,k)=C( 1,2);
currAxes.Visible = 'off';
line(x,y,'Color','r','LineWidth',2)
%
f= waitbar(k/numFrames);
% clear vidFrame
end
toc
  1 Comment
Levente Gellért
Levente Gellért ongeveer 6 uur ago
Dear Mathieu Noe, after applying your suggested changes I do not see any relevant speed-up shet running the code, Thanks anyway! Best lg

Sign in to comment.

Categories

Find more on Just for fun in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!