当前位置:文档之家› MATLAB仿真实验报告

MATLAB仿真实验报告

MATLAB 仿真实验报告课题名称:MATLAB 仿真——图像处理学院:机电与信息工程学院专业:电子信息科学与技术年级班级:2012级电子二班一、实验目的1、掌握MATLAB处理图像的相关操作,熟悉相关的函数以及基本的MATLAB语句。

2、掌握对多维图像处理的相关技能,理解多维图像的相关性质3、熟悉Help 命令的使用,掌握对相关函数的查找,了解Demos下的MATLAB自带的原函数文件。

4、熟练掌握部分绘图函数的应用,能够处理多维图像。

二、实验条件MATLAB调试环境以及相关图像处理的基本MATLAB语句,会使用Help命令进行相关函数查找三、实验内容1、nddemo.m函数文件的相关介绍Manipulating Multidimensional ArraysMATLAB supports arrays with more than two dimensions. Multidimensional arrays can be numeric, character, cell, or structure arrays.Multidimensional arrays can be used to represent multivariate data. MATLAB provides a number of functions that directly support multidimensional arrays. Contents :●Creating multi-dimensional arrays 创建多维数组●Finding the dimensions寻找尺寸●Accessing elements 访问元素●Manipulating multi-dimensional arrays操纵多维数组●Selecting 2D matrices from multi-dimensional arrays从多维数组中选择二维矩阵(1)、Creating multi-dimensional arraysMultidimensional arrays in MATLAB are created the same way astwo-dimensional arrays. For example, first define the 3 by 3 matrix, and then add a third dimension.The CAT function is a useful tool for building multidimensional arrays. B =cat(DIM,A1,A2,...) builds a multidimensional array by concatenating(联系起来)A1, A2 ... along the dimension DIM. Calls to CAT can be nested(嵌套).(2)、Finding the dimensions SIZE and NDIMS return the size and number of dimensions of matrices.(3)、Accessing elements To access a single element of a multidimensional array, use integer subscripts(整数下标).(4)、Manipulating multi-dimensional arraysRESHAPE, PERMUTE, and SQUEEZE are used to manipulate n-dimensional arrays. RESHAPE behaves as it does for 2D arrays. The operation of PERMUTE is illustrated below.Let A be a 3 by 3 by 2 array. PERMUTE(A,[2 1 3]) returns an array with the row and column subscripts reversed (dimension 1 is the row, dimension 2 is the column, dimension 3 is the depth and so on). Similarly, PERMUTE(A,[3,2,1]) returns an array with the first and third subscripts interchanged.A = rand(3,3,2);B = permute(A, [2 1 3]);%permute:(转置)C = permute(A, [3 2 1]);(5)、Selecting 2D matrices from multi-dimensional arrays Functions like EIG that operate on planes or 2D matrices do not accept multi-dimensional arrays as arguments. To apply such functions to different planes of the multidimensional arrays, use indexing or FOR loops.For example: A = cat( 3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], ...[6 4 7; 6 8 5; 5 4 3]);% The EIG function is applied to each of the horizontal 'slices' of A.for i = 1:3eig(squeeze(A(i,:,:))) %squeeze 除去size为1的维度endans =10.3589-1.00001.6411ans =21.22930.3854 + 1.5778i0.3854 - 1.5778ians =13.3706-1.6853 + 0.4757i-1.6853 - 0.4757iINTERP3, INTERPN, and NDGRID are examples of interpolation and data gridding functions that operate specifically on multidimensional data. Here is an example of NDGRID applied to an N-dimensional matrix.示例程序x1 = -2*pi:pi/10:0;x2 = 2*pi:pi/10:4*pi;x3 = 0:pi/10:2*pi;[x1,x2,x3] = ndgrid(x1,x2,x3);z = x1 + exp(cos(2*x2.^2)) + sin(x3.^3);slice(z,[5 10 15], 10, [5 12]); axis tight;程序运行结果:2、题目要求:编写程序,改变垂直于X轴的三个竖面的其中两个面的形状,绘制出图形。

3、题目解答程序:clear all;close all;clc;x1 = -2*pi:pi/10:0;x2 = 2*pi:pi/10:4*pi;x3 = 0:pi/10:2*pi;[x1,x2,x3] = ndgrid(x1,x2,x3);%生成绘制3-D图形所需的网格数据z = x1 + exp(cos(2*x2.^2)) + sin(x3.^3);slice(z,10, 10, [5 12]); %用slice画四维图像,颜色表示第四维的数值axis tight;%axis tight是使坐标系的最大值和最小值和上述的数据范围一致[xsp,ysp,zsp] = sphere;%绘制球体xsp=6*xsp;ysp=6*ysp;zsp=6*zsp;hsp = surface(xsp+0.8,ysp+10,zsp+11);xd = get(hsp,'XData');yd = get(hsp,'YData');zd = get(hsp,'ZData');delete(hsp)%删除图形对象处理hsphold onhslicer = slice(z,xd,yd,zd);axis tighthold offhsp = surface(xsp+21.1,ysp+10,zsp+11);xd = get(hsp,'XData');%获得X轴数据yd = get(hsp,'YData');%获得Y 轴数据zd = get(hsp,'ZData');%获得Z 轴数据delete(hsp)%删除图形对象处理hsphold onhslicer = slice(z,xd,yd,zd);axis tightview(5,2)%从通过方位角5度、俯视角2度的地方观看图像hold off所得图形:-5051015202510202468101214161820四、实验思考我们的题目主要是对多维图像进行处理,首先我们在Help命令下,查阅了nddemo.m 函数的相关介绍,知道了怎样把两个二维矩阵相联系,形成三维矩阵,我们认真分析了nddemo.m函数文件的相关程序,查找了相关函数的使用方法小组成员也进行了细致的讨论,最终找到了解决问题的方法。

以上只是我们的一种结题思路,我们还会继续努力,争取实现多种变换方式,例如把两个球面换成抛物面或是其他曲面。

这次的实验我们的收获很大。

五、小组总结这次小学期的Matlab实验具有一定的挑战难度,以前并没有做过类似的实验,不过我们小组三人相互配合,共同努力,最后还是比较好地完成了任务。

由于我们的实验要把两个平面转换成曲面,我们就打算转换成球面比较方便。

但是,以前没有学过相关的函数,所以我们上网找些资料,自学了”sphere”、“ndgrid”、“surface”、“delete”等函数的使用方法,利用”sphere”函数返回的值来生成我们想要的球面。

但是事情总是没有那么简单的,在编写程序的过程中我们也遇到了种种的困难,比如球心坐标的计算错误,在运行程序时出现各种错误报告,有时还不经意地把中文字符当成了英文字符,给自己造成了很大的困扰。

相关主题