MATLAB。将绘制的线条叠加到现有的 .avi 影片上
MATLAB. Overlay plotted lines onto an existing .avi movie
我想将一系列已知坐标作为线绘制到已知宽度和高度的图形上。然后导入一个相同高度和宽度的.avi电影,然后将这两个组合起来,让线条覆盖视频。
我一直在查看 google 和 Whosebug,但不确定从哪里开始。任何指针都会收到。我正在使用 Matlab R2015a。
谢谢,
克里斯
这是一种方法。代码有注释。我创建了一个虚拟电影,但如果你有一个,你可以跳过前几个步骤,如图所示。诀窍是使用 hold on
和 drawnow
来正确刷新显示。您可以添加更多行或修改几乎任何您想要的内容。如有不明之处请追问。
clear
clc
close all
%//================
%// Make video. If you already have one skip this step.
xyloObj = VideoReader('xylophone.mp4');
vidWidth = xyloObj.Width;
vidHeight = xyloObj.Height;
mov = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),...
'colormap',[]);
%//================
hf = figure;
set(hf,'position',[150 150 vidWidth vidHeight]);
%// Read frames and put into structure to display in an axes.
k = 1;
while hasFrame(xyloObj)
mov(k).cdata = readFrame(xyloObj);
k = k+1;
end
%// For animated gif
filename = 'OverlayGif.gif';
NumFrames = k;
hold on
for n = 1:15
imshow(mov(n).cdata);
%// Add line. The drawnow is crucial.
line([round(vidWidth/4) round(3*vidWidth/4)],[round(vidHeight/2) round(vidHeight/2)],'Color',rand(1,3),'LineWidth',4)
drawnow
%// Make animated gif (for demo only. You don't need that).
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
动画 gif 中的示例输出。您可以丢弃用于创建它的 for
循环的末尾。
希望对您有所帮助!
我想将一系列已知坐标作为线绘制到已知宽度和高度的图形上。然后导入一个相同高度和宽度的.avi电影,然后将这两个组合起来,让线条覆盖视频。
我一直在查看 google 和 Whosebug,但不确定从哪里开始。任何指针都会收到。我正在使用 Matlab R2015a。
谢谢, 克里斯
这是一种方法。代码有注释。我创建了一个虚拟电影,但如果你有一个,你可以跳过前几个步骤,如图所示。诀窍是使用 hold on
和 drawnow
来正确刷新显示。您可以添加更多行或修改几乎任何您想要的内容。如有不明之处请追问。
clear
clc
close all
%//================
%// Make video. If you already have one skip this step.
xyloObj = VideoReader('xylophone.mp4');
vidWidth = xyloObj.Width;
vidHeight = xyloObj.Height;
mov = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),...
'colormap',[]);
%//================
hf = figure;
set(hf,'position',[150 150 vidWidth vidHeight]);
%// Read frames and put into structure to display in an axes.
k = 1;
while hasFrame(xyloObj)
mov(k).cdata = readFrame(xyloObj);
k = k+1;
end
%// For animated gif
filename = 'OverlayGif.gif';
NumFrames = k;
hold on
for n = 1:15
imshow(mov(n).cdata);
%// Add line. The drawnow is crucial.
line([round(vidWidth/4) round(3*vidWidth/4)],[round(vidHeight/2) round(vidHeight/2)],'Color',rand(1,3),'LineWidth',4)
drawnow
%// Make animated gif (for demo only. You don't need that).
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if n == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
动画 gif 中的示例输出。您可以丢弃用于创建它的 for
循环的末尾。
希望对您有所帮助!