How can I sync the video along with the plot?

8 vues (au cours des 30 derniers jours)
Sujan Ponnappa
Sujan Ponnappa le 16 Déc 2019
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

Réponses (1)

Rajesh Ranjan
Rajesh Ranjan le 18 Déc 2019
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)

Catégories

En savoir plus sur Movie dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by