speeding up mpg video frame reading
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Levente Gellért
am 19 Nov. 2025 um 13:04
Kommentiert: Levente Gellért
vor etwa 10 Stunden
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 Kommentare
dpb
am 19 Nov. 2025 um 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?
Antworten (1)
Mathieu NOE
am 19 Nov. 2025 um 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
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!