matlab制作及生成avi,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文件

几个必要的函数:

AVIFILECreate 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.

GETFRAMEGet 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.

ADDFRAMEAdd 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

喜欢 (0)or分享 (0)

matlab 彗星图速度控制,matlab制作及生成avi,gif动画_matlab培训相关推荐

  1. matlab p图,【MATLAB】P图神器,初露锋芒:第一周作业(剧透)

    做完第一周Matlab作业,深感MatLab之强大.(都第几周了,才做第一周作业...) 不在上图像处理这门课的同学,也可以试试在Matlab敲这些代码哦~ 用Matlab P图可有意思呢~ Writ ...

  2. matlab gif生成器,matlab制作及生成avi,gif动画

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

  3. matlab三维图 魔方,matlab制作魔方图片

    2.绘制三维曲面的函数 Matlab 提供了 mesh 函数和 surf 函数来绘制三维曲面图...例 520 绘制三维图形. 1 绘制魔方阵的三维条形图 2 以三维杆图形式绘制曲线...... 图像 ...

  4. Matlab动图保存——GIF制作与视频制作

    在Matlab绘制动图时,若想保存成GIF或视频,可参考以下代码. (1)GIF格式 gif_flag = 1; % 是否保存 if gif_flag == 1filename = 'gif_name ...

  5. matlab 泡泡图,使用matlab绘制2维、3维气泡图

    在学习模糊c均值聚类时,突然想到能否将每个样本对所属簇的奴属度(C)用气泡图的形式表示出来,这样就可以在一张图上同时获得分类与奴属度(C)两类信息.在matlab中没有绘制气泡图的专用函数,不过可以通 ...

  6. matlab 三维图 输出,MATLAB三维曲线图绘制并输出到指定文件夹(突出几组不同数据)...

    1.首先生成全为0的一维数组(X,Y1,Z1).全为1的一维数组(X,Y2,Z2).正弦曲线(X,Y3,Z3).注意绘制三维图时,X,Y,Z矢量长度要相同! 2.MATLAB中绘制三维曲线指令plot ...

  7. 给matlab图加图注,matlab学习5-数据可视化4-gai.ppt

    matlab语言丰富的图形表现方法,使得数学计算结果可以方便地.多样性地实现了可视化,这是其它语言所不能比拟的.;第一节 符号函数绘图第二节 图形编辑第三节 2D数据图第四节 3D数据图第五节 MAT ...

  8. matlab潮汐图,使用MATLAB拟合工具箱进行潮汐调和分析

    第 31 卷 第 3 期 2012 年 9 月 海 洋 技 术 OCEAN TECHNOLOGY Vol. 31,No.3 Sep,2012 使用 MATLAB 拟合工具箱进行潮汐调和分析 江海东 1 ...

  9. matlab 能谱图,基于Matlab平台上γ能谱光滑处理

    核 动 力 工 程 Nuclear Power Engineering 第 28 卷 第 3 期 2 0 0 7 年6 月 Vol. 28. No.3 Jun. 2 0 0 7 文章编号:0258-0 ...

最新文章

  1. vs2015 python 调试dll
  2. 全球及中国二乙氨基羟基苯甲酰基苯甲酸己酯行业规模分析与市场需求预测报告2022版
  3. 报表中如何控制附件的上传和下载权限
  4. 计算机科学课程体系核心内容,计算机科学教育的课程体系之研究
  5. [Python学习] Django 权限控制
  6. Ubuntu16.04安装系统之后软件无法安装
  7. CentOS 7第一次使用时的基础配置
  8. android 使用ffmpeg 调用命令实现视频转gif(ffmpeg 学习三)
  9. QCSPCChart SPC控制图工具软件是面向对象的工具包
  10. 移动 APP 的测试方法,以及移动专项测试的思路与方法
  11. 电脑上最值得安装的软件,这10款里一定有你想要的
  12. 网络安全之身份认证---双因子身份认证技术
  13. 最新综述:深度学习图像三维重建最新方法及未来趋势
  14. 中国计算机科技前沿网,专委动态科技前沿-中国计算机学会专委.PDF
  15. 现在学习软件测试好找工作吗
  16. x86服务器虚拟化程度,X86服务器虚拟化之IBM企业级X架构
  17. 【python练习,6.15】(霍兰德人格分析雷达图等)
  18. 【逻辑漏洞技巧拓展】————5、密码逻辑漏洞
  19. [附源码]SSM计算机毕业设计毕业生离校管理系统JAVA
  20. 悦享数据API接口调用教程

热门文章

  1. MediaCoder CUDA H.264 编码器测试报告
  2. 另类万圣节:十三种令程序员们夜不能寐的恐怖噩梦
  3. 新闻发稿公司及软文发稿渠道整体操作过程解密
  4. PhotoShop自学笔记
  5. #debug# 用cv2读的图片送到dataset前不要转chw
  6. 智慧城市同城V4 v2.1.7 同城 同城小程序 同城信息
  7. viso直线相交不跳线不弯曲
  8. HTML、HTM、MHT 图标修复方法
  9. 软件测试工程师工资怎么样?高吗?
  10. 基于艾宾浩斯遗忘曲线的英语词汇学习微信小程序——一些思路和想法