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 commentaires

v = VideoReader(datfile);
vidObj = VideoReader(datfile);
...
Why do you have two copies of the same apparent thing?
Levente Gellért
Levente Gellért le 19 Nov 2025
Dear dpb, silly mistake, just corrected, no change in speed.
Thanks
lg
dpb
dpb le 19 Nov 2025
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?

Connectez-vous pour commenter.

Réponses (1)

Mathieu NOE
Mathieu NOE le 19 Nov 2025
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

2 commentaires

Levente Gellért
Levente Gellért le 21 Nov 2025
Dear Mathieu Noe, after applying your suggested changes I do not see any relevant speed-up shet running the code, Thanks anyway! Best lg

Connectez-vous pour commenter.

Catégories

Produits

Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by