matlab四合一(dream)(7)

2019-04-22 19:42

6.图片读取与绘制:

使用imread读入12110015.jpg(我家两只猫的照片)。然后绘制原图与其R,G,B分量的图,共四张图。当你用imread读入图片后会发现这是一个三维矩阵,每一页的维度表示像素大小,三页分别表示R,G,B的颜色编码。如果你只要绘制红色,可以将后两页的值置为0来进行绘制。使用subplot,绘制为2×2的样式。结果参考下图:

500100015002000500100015002000100020003000100020003000500100015002000500100015002000100020003000100020003000 % im=displayRGB(filename)

% reads in the jpg file in filename (filename should include the % extension), displays the original image as well as each color layer in a

% figure separately, and returns this composite image in im. the original % image is resampled so the largest final dimension is 800 pixels and the % aspect ratio is preserved. x=imread('12110015.jpg'); subplot(2,2,1),imshow(x); a=x(:,:,1); aa=x(:,:,2); aaa=x(:,:,3);

r=cat(3,a,zeros(size(a)),zeros(size(a))); subplot(2,2,2),imagesc(r);

g=cat(3,zeros(size(aa)),aa,zeros(size(aa))); subplot(2,2,3),imagesc(g);

b=cat(3,zeros(size(aaa)),zeros(size(aaa)),aaa); subplot(2,2,4),imagesc(b); end

7.动画模拟Brown运动:

声明一个函数叫brown2D(N)。这个函数只有一个输入参数为N,表示用来模拟Brown运动的点数。每一个点初始位置为(0,0),plot所有的点使用‘.‘作为marker。设置xlim和ylim为-1~1,axis设置为square。做1000次模拟,即写一个1000次的循环。每次更新

点的x,y坐标值,更新的位置使用randn生成,方差为0.005。加入N为100,那么对于每一个x,y都需要使用randn来更新其坐标值。更新的值等于此次生成的随机变量值加上上一次的坐标值,在播放动画时,使用pause来停顿0.01秒。最好使用set来设置xdata和ydata属性的方式来更新x,y的坐标值。运行程序会看到点从中心开始向四周方向随机漂移。结果可参考我制作的avi视频,brown2D.avi。

% brown2D(N) %

% simulates the Brownian motion of N points. all N points begin in the same

% location and diffuse away from it as time passes. The animation runs for

% about 10 seconds function brown2D(N) % make a figure figure; axis square

axis([-1 1 -1 1]);

% make the x and y positions of the points x=zeros(N,1); y=zeros(N,1); h=plot(x,y,'.k');

% run a loop to display the points for n=1:1000

% update the positions x=x+randn(size(x))*.005; y=y+randn(size(y))*.005; % plot them

set(h,'xdata',x,'ydata',y); axis([-1 1 -1 1]) pause(0.01); end

8.Julia分形集动画制作:

第三次做的Julia函数可以用来绘制julia分形图,现在改成一个动画,我们只需要在迭代过程中,对每次更新的A矩阵都做绘制,使用drawnow,这样会把每一次更新的A都进行更新绘制就形成了一个很酷的动画。这里不使用pause是因为julia分形的迭代计算耗费较长的时间,因此我们无法定义pause多长时间,而使用drawnow则可以无需等待,很快的更新结果。程序运行结果参考我制作的avi视频,JuliaAni.avi。

% juliaAnimation(zMax,c,N) %

% displays an animation of a Julia Set %

% inputs:

% zMax - the complex numbers for which escape velocities are computed will

% be 500 values between -zMax and zMax for both real and imaginary % part

% c - the complex number c, which is a parameter in the Julia set % N - the maximum number of iterations to use when computing escape % velocities; also equals the number of frames in the animation %

% EXAMPLES

% juliaAnimation(1,-.4+i*.6,75); % juliaAnimation(1,-.8+i*.156,100);

% juliaAnimation(.75,-.297491+i*.641051,150); function juliaAnimation(zMax,c,N)

% make the complex grid, with real and imaginary part going from -zMax to % zMax

res=500; % resolution, number of points on a side of the grid temp=linspace(-zMax,zMax,res); [R,I]=meshgrid(temp,temp); Z=R+1i*I;

% initialize the map to have 'escape velocities' all equal to N M=N*ones(size(Z));

% loop N times, each time generating the new Z and visualizing it figure for n=1:N

% calculate Zn for the current n Z=Z.^2+c;

% find where the modulus is >2 inds=find(abs(Z)>2);

% set these inds in M to contain the loop iteration M(inds)=n;

% set these inds in Z to be nan Z(inds)=nan;

imagesc(temp,temp,atan(0.1*M)); axis xy drawnow

end


matlab四合一(dream)(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2018年郑州市初中毕业年级适应性测试

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: