speeding up mpg video frame reading

18 visualizaciones (últimos 30 días)
Levente Gellért
Levente Gellért el 19 de Nov. de 2025 a las 13:04
Comentada: Levente Gellért hace alrededor de 11 horas
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 comentarios
Levente Gellért
Levente Gellért el 19 de Nov. de 2025 a las 15:32
Dear dpb, silly mistake, just corrected, no change in speed.
Thanks
lg
dpb
dpb el 19 de Nov. de 2025 a las 16:09
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?

Iniciar sesión para comentar.

Respuestas (1)

Mathieu NOE
Mathieu NOE el 19 de Nov. de 2025 a las 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 comentario
Levente Gellért
Levente Gellért hace alrededor de 7 horas
Dear Mathieu Noe, after applying your suggested changes I do not see any relevant speed-up shet running the code, Thanks anyway! Best lg

Iniciar sesión para comentar.

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by