speeding up mpg video frame reading

조회 수: 19 (최근 30일)
Levente Gellért
Levente Gellért 2025년 11월 19일 13:04
댓글: Levente Gellért 2025년 11월 21일 11:58
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
Levente Gellért
Levente Gellért 2025년 11월 19일 15:32
Dear dpb, silly mistake, just corrected, no change in speed.
Thanks
lg
dpb
dpb 2025년 11월 19일 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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Mathieu NOE
Mathieu NOE 2025년 11월 19일 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
Levente Gellért
Levente Gellért 대략 8시간 전
Dear Mathieu Noe, after applying your suggested changes I do not see any relevant speed-up shet running the code, Thanks anyway! Best lg

댓글을 달려면 로그인하십시오.

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by