本站分享:大数据、数据分析师考试认证培训,包括:Python培训Excel培训Matlab培训SPSS培训SAS培训R语言培训Hadoop培训Amos培训Stata培训Eviews培训广告位

matlab制作及生成avi,gif动画_matlab培训

matlab培训 cdadata 501℃ 0评论

matlab制作及生成avi,gif动画

关键词:matlab 生成avimatlab 生成动画matlab 图片生成动画matlab gif动画

一、动画的制作
Matlab中动画实现的方法主要有下面三种
1.电影动画:从不同的视角拍下一系列对象的图形,并保存到变量中,然后按照一定的顺序像电影一样播放。
http://www.matlabsky.com/thread-593-1-1.html

%录制电影动画
for j=1:n
%
%这里输入我们的绘图命令
%
M(j) = getframe;
end
movie(M)
%单帧显示方法
f = getframe(gcf);
colormap(f.colormap);
image(f.cdata);

2.擦除动画:画在图形窗口中按照一定的算法连续擦除和重绘图形对象,表现为动画,这个也是MATLAB中使用最多的方法。
http://www.matlabsky.com/thread-240-1-1.html

%擦除重绘模式动画
%选择一个擦除模式
set(h,’erasemode’,erasemode)%h是需要执行动画图像的句柄,一般都是由line或者plot创建
%
%需要执行一些图形计算命令
%
%循环语句中更新坐标数据,一般使用for或者while
for i=1:n
%
%必要的MATLAB命令
%
set(h,’xdata’,xdata,’ydta’,ydata)%更新图像的坐标数据
drownnow%刷新屏幕
%
%其它Matlab语句
%
end
3.质点动画:用comet()等函数绘制彗星图,它能演示一个质点的运动
http://www.matlabsky.com/thread-594-1-1.html
comet(xdata,ydata,p)
p是指彗星的尾巴的长度,可以是常数或者size(x)大小的向量
二、动画的保存
下面再讲述下生成的动画如何保存。
http://www.matlabsky.com/thread-595-1-1.html

MATLAB动画保存只对电影动画有意义,因为其他两种都是实时动画,一眨眼过去了,而电影动画是先将动画一帧一帧的保存下来,在使用movie函数播放。它的好处是,运行一次MATLAB程序就可以播放无数次,只要你的帧数据还在。
但是这还是不方便,由于它没法脱离MATLAB环境,很讨厌。还好MATLAB为我们提供了movie2avi函数,它可以把动画直接转换成avi文件,而avi文件则可以脱离Matalb环境而在其他地方运行了。

1:保存成avi文件

几个必要的函数:

AVIFILE Create a new AVI file
AVIOBJ = AVIFILE(FILENAME) creates an AVIFILE object AVIOBJ with the
default parameter values. If FILENAME does not include an extension,
then ‘.avi’ will be used. Use AVIFILE/CLOSE to close the file opened by
AVIFILE. Use “clear mex” to close all open AVI files.

GETFRAME Get movie frame.
GETFRAME returns a movie frame. The frame is a snapshot
of the current axis. GETFRAME is usually used in a FOR loop
to assemble an array of movie frames for playback using MOVIE.
For example:
for j=1:n
plot_command
M(j) = getframe;
end
movie(M)
GETFRAME(H) gets a frame from object H, where H is a handle
to a figure or an axis.

ADDFRAME Add video frame to AVI file.
AVIOBJ = ADDFRAME(AVIOBJ,FRAME) appends the data in FRAME to AVIOBJ,
which is created with AVIFILE.

例子:

——————————————————————————————–

clc;
clear;
fig=figure;
aviobj=avifile(‘example.avi’);
n=50;
x=0:pi/n:2*pi;
y=sin(x);
k=0;
for t=0:pi/n:2*pi
k=k+1;
x(k)=t;
y(k)=sin(t);
H=plot(x,y,x(k),y(k),’or’);
grid
MOV=getframe(fig);
aviobj=addframe(aviobj,MOV);
end
close(fig)
aviobj=close(aviobj)

——————————————————————————————–

2:直接保存gif动画
%%%构造gif图像的帧,
nn=getframe(gcf);
%%转换为可以直接输出的格式(这会使图像丢失)
%如果要制作彩色的图像,你只能把生成的彩色图像单独制作(使用其他软件)
im=frame2im(nn);
[I,map]=rgb2ind(im,256);

if i1==1

imwrite(I,map,’out.gif’,’gif’,’loopcount’,inf)

else

imwrite(I,map,’out.gif’,’gif’,’writemode’,’apend’)

end

几个必要的函数:

figure属性:

属性名:NextPlot
属性值:new | {add} | replace | replacechildren
释义: How to add next plot.

Determines which figure MATLAB uses to display graphics output. If the value of the current figure is:
new — Create a new figure to display graphics (unless an existing parent is specified in the graphing function as a property/value pair).
add — Use the current figure to display graphics (the default).
replace — Reset all figure properties except Position to their defaults and delete all figure children before displaying graphics (equivalent to clf reset).
replacechildren — Remove all child objects, but do not reset figure properties (equivalent to clf).
The newplot function provides an easy way to handle the NextPlot property. For more information, see the axes NextPlot property and Controlling Graphics Output.

FRAME2IMReturn image data associated with movie frame.
[X,MAP] = FRAME2IM(F) returns the indexed image X and associated
colormap MAP from the single movie frame F.

RGB2INDConvert RGB image to indexed image.
RGB2IND converts RGB images to indexed images using one of three different methods: uniform quantization, minimum variance quantization,and colormap approximation. RGB2IND dithers the image unless you specify ‘nodither’ for DITHER_OPTION.
[X,MAP] = RGB2IND(RGB,N) converts the RGB image to an indexed image X using minimum variance quantization. MAP contains at most N colors. N must be <= 65536.
[…] = RGB2IND(…,DITHER_OPTION) enables or disables dithering. DITHER_OPTION is a string that can have one of these values:
‘dither’ dithers, if necessary, to achieve better color
resolution at the expense of spatial
resolution (default)
‘nodither’ maps each color in the original image to the
closest color in the new map. No dithering is
performed.
Example
——-
RGB = imread(‘ngc6543a.jpg’);
[X,map] = rgb2ind(RGB,128);
figure, image(X), colormap(map)
axis off
axis image

例子:

——————————————————————————————–

Z = peaks;
surf(Z)
axis tight
set(gca,’nextplot’,’replacechildren’,’visible’,’off’)
f = getframe;
[im,map] = rgb2ind(f.cdata,256,’nodither’);
im(1,1,1,20) = 0;
for k = 1:20
surf(cos(2*pi*k/20)*Z,Z)
f = getframe;
im(:,:,1,k) = rgb2ind(f.cdata,map,’nodither’);
end
imwrite(im,map,’DancingPeaks.gif’,’DelayTime’,0,’LoopCount’,inf)
——————————————————————————————–

或者:

Z = peaks;

surf(Z)
axis tight

Zl=zlim;
for k = 1:20
surf(cos(2*pi*k/20)*Z,Z)

zlim(Zl);
f = getframe;
im=frame2im(f);
[I,map] = rgb2ind(im,256);
if k==1
imwrite(I,map,’out.gif’,’gif’,’loopcount’,inf,’Delaytime’,0.02)
else
imwrite(I,map,’out.gif’,’gif’,’writemode’,’append’,’Delaytime’,0.02)
end
end
——————————————————————————————–

对图片进行旋转时,图像大小改变的处理办法:

如下面的程序:

如果没有set(gcf,’units’,’normalized’,’position’,[0.3 0.2 0.4 0.3])这一句时,则图像的大小会随着角度的改变而改变(读者可以自己试下)。

改变的方法很简单,就是在第一次做完图后,在动画之前,将上面这句贴上去。

lat0=-90:90;
long0=-180:179;
dv=randn(length(lat0),length(long0))*0.1+sin(repmat(lat0′,1,length(long0))/180*2*pi);
% % plotting
figure
sphere;
h = findobj(gcf, ‘Type’, ‘surface’);
set(h, ‘CData’, dv, ‘FaceColor’, ‘texturemap’)
axis equal;
ylabel(‘long = -180′)
axis image off
c1=colormap;
set(gcf,’colormap’,flipud(c1));
colorbar
set(gcf,’units’,’normalized’,’position’,[0.3 0.2 0.4 0.3])
for i1=0:30:360
view(i1,0)
drawnow
end

另外还有一个简单的办法:

在绘图后,加上axis vis3d便可保证大小不变了.

AXIS VIS3D freezes aspect ratio properties to enable rotation of
3-D objects and overrides stretch-to-fill

如下面的例子:

lat0=-90:90;
long0=-180:179;
dv=randn(length(lat0),length(long0))*0.1+sin(repmat(lat0′,1,length(long0))/180*2*pi);
% % plotting
figure
sphere;
h = findobj(gcf, ‘Type’, ‘surface’);
set(h, ‘CData’, dv, ‘FaceColor’, ‘texturemap’)
axis equal;
ylabel(‘long = -180′)
axis image off
c1=colormap;
set(gcf,’colormap’,flipud(c1));
colorbar
axis vis3d
for i1=0:30:360
view(i1,0);
drawnow
end

转载请注明:数据分析 » matlab制作及生成avi,gif动画_matlab培训

喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址