matlab制作及生成avi,gif動畫


轉載:http://hi.baidu.com/imheaventian/item/25fb64c3181e7f56bcef6911

 

一、動畫的制作
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM