How can I sync the video along with the plot?

조회 수: 7 (최근 30일)
Sujan Ponnappa
Sujan Ponnappa 2019년 12월 16일
답변: Rajesh Ranjan 2019년 12월 18일
I have a code below to play the video and plot the data at the same time. But I am not able to sync the video properly with the graph.
How can I achieve this? I have also attached the raw excel file of the data I want to plot
%% Setup the subplots
ax1 = subplot(2,1,1); % For video
ax2 = subplot(2,1,2); % For pressure plot
%%% Setup VideoReader object
filename = 'slice1.avi';
v = VideoReader(filename);
frameratevideo=v.FrameRate;
nFrames = v.Duration*v.FrameRate; % Number of frames
% Display the first frame in the top subplot
vidFrame = readFrame(v);
v.CurrentTime = 16;
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
%%% Load the pressure data
t = ts; % your actual data
y = ys;
nDataPoints = length(t); % Number of data points
%step = round((nFrames/nDataPoints));
index = 1:0.44:nDataPoints;
i = 2;
% Diplay the plot corresponds to the first frame in the bottom subplot
h = plot(ax2,t(1:index(i)),y(1:index(i)),'-k');
% Fix the axes
ax2.XLim = [t(1) t(end)];
ax2.YLim = [min(y) max(y)];
%%% Animate
while hasFrame(v)
pause(1/v.FrameRate);
%pause(0.01)
vidFrame = readFrame(v);
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
i = i + 1;
set(h,'YData',y(1:index(i)), 'XData', t(1:index(i)))
end

답변 (1개)

Rajesh Ranjan
Rajesh Ranjan 2019년 12월 18일
Sujan, this is how I do it and it comes out synced.
v = VideoReader('testVideo.avi')
frameratevideo=v.FrameRate;
nFrames = v.Duration*v.FrameRate; % Number of frames
v.CurrentTime = 0.0;
x = 0:0.1:10
y = sin(x)
myVideo = VideoWriter('myVideoFile'); %open video file
myVideo.FrameRate = 5; %same as the input video
open(myVideo)
figure(1)
set(gcf,'Position',[100 100 800 400])
sub1=subplot(1,2,1)
sub2=subplot(1,2,2)
h2=animatedline('marker','o');
i=0;
%%% Animate
while hasFrame(v)
pause(1/v.FrameRate);
%pause(0.01)
vidFrame = readFrame(v);
image(vidFrame, 'Parent', sub1);
sub1.Visible = 'off';
i = i + 1;
addpoints(h2,x(i),y(i));
drawnow
%pause(0.1)
pause(1/v.FrameRate);
frame=getframe(gcf);
writeVideo(myVideo, frame);
% set(h,'YData',y(1:index(i)), 'XData', t(1:index(i)))
end
close(myVideo)

카테고리

Help CenterFile Exchange에서 Movie에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by