How to synchronise video to matlab plot
조회 수: 35 (최근 30일)
이전 댓글 표시
I have a video of chopping a potato wherein the chopping activites are captured by a pressure sensor. The data is intepretated and plotted with matlab.
How can I synchronise the video to the plot as to know how the pressure values changes as the video plays.
댓글 수: 0
채택된 답변
Saumik Kumar Dey
2019년 11월 27일
According to my understanding, you want to play the video and plot pressure variation with time along with that. You can achive that by two subplots one for playing the video feed and the other for plotting the pressure data. You can use the matlab class "VideoReader" for reading the frames of you video file.
Consider the following example as a guideline to achieve your goal. Make sure the initial and final experiment times of the video matches with that of pressure data.
%% Setup the subplots
ax1 = subplot(2,1,1); % For video
ax2 = subplot(2,1,2); % For pressure plot
%% Setup VideoReader object
filename = 'SomeVideoFileName';
v = VideoReader(filename);
nFrames = v.Duration*v.FrameRate; % Number of frames
% Display the first frame in the top subplot
vidFrame = readFrame(v);
image(vidFrame, 'Parent', ax1);
ax1.Visible = 'off';
%% Load the pressure data
t = 0:0.01:v.Duration; % Cooked up for this example, use your actual data
y = sin(t);
nDataPoints = length(t); % Number of data points
step = round((nDataPoints/nFrames));
index = 1:step: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);
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
Note: This example considers "nDataPoints > nFrames". The other condition can be handled similarly.
댓글 수: 11
추가 답변 (1개)
youjarr
2023년 2월 23일
Hey guys,
perfect code, thank you very much.
I have two questions:
How can I save the fig to reopen the file with the video?
Because when I do savefig it is not opening.
How can I replay within the fig?
Thank you very much.
댓글 수: 1
youjarr
2023년 2월 23일
편집: youjarr
2023년 2월 23일
And I am getting an Error:
Index exceeds the number of array elements. Index must not exceed 361.
Error in VideoSubplot (line 50)
set(h1,'YData',y1(1:index1(j)), 'XData', t02(1:index1(j)))
I thought the code handles the different length of video and measured data?
My nFrames is smaller then my nDataPoints
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!