Matlab绘制图像及图像的处理

一 绘制函数图像

matlab平面绘制函数图像有多个函数,plot,ezplot等。

1.1 plot函数

查看matlab的帮助文件可知plot函数的调用格式有


PLOT Linear plot.

PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix,
    then the vector is plotted versus the rows or columns of the matrix,
    whichever line up. If X is a scalar and Y is a vector, disconnected
    line objects are created and plotted as discrete points vertically at
    X.

PLOT(Y) plots the columns of Y versus their index.
    If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)).
    In all other uses of PLOT, the imaginary part is ignored.

Various line types, plot symbols and colors may be obtained with
    PLOT(X,Y,S) where S is a character string made from one element
    from any or all the following 3 columns:

  b blue                       . point                           - solid
  g green                      o circle                          : dotted
  r red                        x x-mark                          -. dashdot
  c cyan                       + plus -- dashed
  m magenta                    * star (none) no line
  y yellow                     s square
  k black                      d diamond
  w white                      v triangle (down)
  ^ triangle (up)
  < triangle (left)
  > triangle (right)
  p pentagram
  h hexagram

For example, PLOT(X,Y,‘c+:‘) plots a cyan dotted line with a plus
    at each data point; PLOT(X,Y,‘bd‘) plots blue diamond at each data
    point but does not draw any line.

PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by
    the (X,Y,S) triples, where the X‘s and Y‘s are vectors or matrices
    and the S‘s are strings.

For example, PLOT(X,Y,‘y-‘,X,Y,‘go‘) plots the data twice, with a
    solid yellow line interpolating green circles at the data points.

 

1.2 ezplot函数

查看matlab的帮助文件可知plot函数的调用格式有

EZPLOT   Easy to use function plotter
    EZPLOT(FUN) plots the function FUN(X) over the default domain
    -2*PI < X < 2*PI, where FUN(X) is an explicitly defined function of X.

    EZPLOT(FUN2) plots the implicitly defined function FUN2(X,Y) = 0 over
    the default domain -2*PI < X < 2*PI and -2*PI < Y < 2*PI.  

    EZPLOT(FUN,[A,B]) plots FUN(X) over A < X < B.
    EZPLOT(FUN2,[A,B]) plots FUN2(X,Y) = 0 over A < X < B and A < Y < B.

    EZPLOT(FUN2,[XMIN,XMAX,YMIN,YMAX]) plots FUN2(X,Y) = 0 over
    XMIN < X < XMAX and YMIN < Y < YMAX.

    EZPLOT(FUNX,FUNY) plots the parametrically defined planar curve FUNX(T)
    and FUNY(T) over the default domain 0 < T < 2*PI.

    EZPLOT(FUNX,FUNY,[TMIN,TMAX]) plots FUNX(T) and FUNY(T) over
    TMIN < T < TMAX.

    EZPLOT(FUN,[A,B],FIG), EZPLOT(FUN2,[XMIN,XMAX,YMIN,YMAX],FIG), or
    EZPLOT(FUNX,FUNY,[TMIN,TMAX],FIG) plots the function over the
    specified domain in the figure window FIG.

    EZPLOT(AX,...) plots into AX instead of GCA or FIG.

    H = EZPLOT(...) returns handles to the plotted objects in H.

    Examples:
    The easiest way to express a function is via a string:
       ezplot(‘x^2 - 2*x + 1‘)

    One programming technique is to vectorize the string expression using
    the array operators .* (TIMES), ./ (RDIVIDE), .\ (LDIVIDE), .^ (POWER).
    This makes the algorithm more efficient since it can perform multiple
    function evaluations at once.
       ezplot(‘x.*y + x.^2 - y.^2 - 1‘)

    You may also use a function handle to an existing function. Function
    handles are more powerful and efficient than string expressions.
       ezplot(@humps)
       ezplot(@cos,@sin)

    EZPLOT plots the variables in string expressions alphabetically.
       subplot(1,2,1), ezplot(‘1./z - log(z) + log(-1+z) + t - 1‘)
    To avoid this ambiguity, specify the order with an anonymous function:
       subplot(1,2,2), ezplot(@(z,t)1./z - log(z) + log(-1+z) + t - 1)

    If your function has additional parameters, for example k in myfun:
       %-----------------------%
       function z = myfun(x,y,k)
       z = x.^k - y.^k - 1;
       %-----------------------%
    then you may use an anonymous function to specify that parameter:
       ezplot(@(x,y)myfun(x,y,2))

注意plot函数的输入是数值的向量或者标量(也可以是多维的矩阵),而ezplot的第一个输入是函数句柄。

二 matlab绘制图像后的标注处理

2.1 gcf参量

gcf  可以获取当前窗口句柄

2.2 为x轴及y轴设置标注,为图像绘制网格线

xlabel()及ylabel()为x轴及y轴设置标注

另外利用 grid或者grid on来为图像绘制网格线。

2.3 legengd为各个图线做说明

legend()函数,查看matlab的使用帮助可知legend函数的调用格式有

 LEGEND Display legend.
    LEGEND(string1,string2,string3, ...) puts a legend on the current plot
    using the specified strings as labels. LEGEND works on line graphs,
    bar graphs, pie graphs, ribbon plots, etc.  You can label any
    solid-colored patch or surface object.  The fontsize and fontname for
    the legend strings matches the axes fontsize and fontname.

    LEGEND(H,string1,string2,string3, ...) puts a legend on the plot
    containing the handles in the vector H using the specified strings as
    labels for the corresponding handles.

    LEGEND(M), where M is a string matrix or cell array of strings, and
    LEGEND(H,M) where H is a vector of handles to lines and patches also
    works.

    LEGEND(AX,...) puts a legend on the axes with handle AX.

    LEGEND OFF removes the legend from the current axes and deletes
    the legend handle.
    LEGEND(AX,‘off‘) removes the legend from the axis AX.

    LEGEND TOGGLE toggles legend on or off.  If no legend exists for the
    current axes one is created using default strings. The default
    string for an object is the value of the DisplayName property
    if it is non-empty and otherwise it is a string of the form
    ‘data1‘,‘data2‘, etc.
    LEGEND(AX,‘toggle‘) toggles legend for axes AX

    LEGEND HIDE makes legend invisible.
    LEGEND(AX,‘hide‘) makes legend on axes AX invisible.
    LEGEND SHOW makes legend visible. If no legend exists for the
    current axes one is created using default strings.
    LEGEND(AX,‘show‘) makes legend on axes AX visible.

    LEGEND BOXOFF  makes legend background box invisible when legend is
    visible.
    LEGEND(AX,‘boxoff‘) for axes AX makes legend background box invisible when
    legend is visible.
    LEGEND BOXON makes legend background box visible when legend is visible.
    LEGEND(AX,‘boxon‘) for axes AX making legend background box visible when
    legend is visible.

    LEGH = LEGEND returns the handle to legend on the current axes or
    empty if none exists. 

    LEGEND(...,‘Location‘,LOC) adds a legend in the specified
    location, LOC, with respect to the axes.  LOC may be either a
    1x4 position vector or one of the following strings:
        ‘North‘              inside plot box near top
        ‘South‘              inside bottom
        ‘East‘               inside right
        ‘West‘               inside left
        ‘NorthEast‘          inside top right (default for 2-D plots)
        ‘NorthWest‘           inside top left
        ‘SouthEast‘          inside bottom right
        ‘SouthWest‘          inside bottom left
        ‘NorthOutside‘       outside plot box near top
        ‘SouthOutside‘       outside bottom
        ‘EastOutside‘        outside right
        ‘WestOutside‘        outside left
        ‘NorthEastOutside‘   outside top right (default for 3-D plots)
        ‘NorthWestOutside‘   outside top left
        ‘SouthEastOutside‘   outside bottom right
        ‘SouthWestOutside‘   outside bottom left
        ‘Best‘               least conflict with data in plot
        ‘BestOutside‘        least unused space outside plot
    If the legend does not fit in the 1x4 position vector the position
    vector is resized around the midpoint to fit the preferred legend size.
    Moving the legend manually by dragging with the mouse or setting
    the Position property will set the legend Location property to ‘none‘.

    LEGEND(...,‘Orientation‘,ORIENTATION) creates a legend with the
    legend items arranged in the specified ORIENTATION. Allowed
    values for ORIENTATION are ‘vertical‘ (the default) and ‘horizontal‘.

    [LEGH,OBJH,OUTH,OUTM] = LEGEND(...) returns a handle LEGH to the
    legend axes; a vector OBJH containing handles for the text, lines,
    and patches in the legend; a vector OUTH of handles to the
    lines and patches in the plot; and a cell array OUTM containing
    the text in the legend.

    Examples:
        x = 0:.2:12;
        plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));
        legend(‘First‘,‘Second‘,‘Third‘);
        legend(‘First‘,‘Second‘,‘Third‘,‘Location‘,‘NorthEastOutside‘)

        b = bar(rand(10,5),‘stacked‘); colormap(summer); hold on
        x = plot(1:10,5*rand(10,1),‘marker‘,‘square‘,‘markersize‘,12,...
                 ‘markeredgecolor‘,‘y‘,‘markerfacecolor‘,[.6 0 .6],...
                 ‘linestyle‘,‘-‘,‘color‘,‘r‘,‘linewidth‘,2); hold off
        legend([b,x],‘Carrots‘,‘Peas‘,‘Peppers‘,‘Green Beans‘,...
                  ‘Cucumbers‘,‘Eggplant‘)

2.4 text函数在图像某点做标注

查看text函数在matlab中的使用帮助

 TEXT   Text annotation.
    TEXT(X,Y,‘string‘) adds the text in the quotes to location (X,Y)
    on the current axes, where (X,Y) is in units from the current
    plot. If X and Y are vectors, TEXT writes the text at all locations
    given. If ‘string‘ is an array the same number of rows as the
    length of X and Y, TEXT marks each point with the corresponding row
    of the ‘string‘ array.

    TEXT(X,Y,Z,‘string‘) adds text in 3-D coordinates.

    TEXT returns a column vector of handles to TEXT objects, one
    handle per text object. TEXT objects are children of AXES objects.

    The X,Y pair (X,Y,Z triple for 3-D) can be followed by
    parameter/value pairs to specify additional properties of the text.
    The X,Y pair (X,Y,Z triple for 3-D) can be omitted entirely, and
    all properties specified using parameter/value pairs.

    Execute GET(H), where H is a text handle, to see a list of text
    object properties and their current values. Execute SET(H) to see a
    list of text object properties and legal property values.

例如

plot(0:pi/20:2*pi,sin(0:pi/20:2*pi))
text(pi,0,‘ \leftarrow sin(\pi)‘,‘FontSize‘,18)

得到的图像为

2.5 设置使绘制的图像窗口不显示

set(h,‘visible‘,‘off‘);其中h可以是图像的函数句柄,也可以是数字1,2(当前图像的名字是figure(1)就是1,反之figure(2)就是2)要设置图像显示  set(h,‘visible‘,‘on‘)
h=figure(1);
x=0:0.01:1;
y=sin(x);
plot(x,y);
set(h,‘visible‘,‘off‘); 

 2.6 设置使绘制的图像工具栏不显示

figure(‘menubar‘,‘none‘);可去掉菜单栏

可以在打开你的m文件之后,在Matlab的命令行输入以下指令来恢复显示Scope的Figure菜单栏:

>> set(0,‘ShowHiddenHandles‘,‘on‘);

>> set(gcf,‘menubar‘,‘figure‘)

2.7 绘制的图像自动保存

2.7.1 saveas命令

查看saveas命令的matlab帮助

 SAVEAS Save Figure or Simulink block diagram in desired output format
    SAVEAS(H,‘FILENAME‘)
    Will save the Figure or Simulink block diagram with handle H to file
    called FILENAME.
    The format of the file is determined from the extension of FILENAME.

    SAVEAS(H,‘FILENAME‘,‘FORMAT‘)
    Will save the Figure or Simulink block diagram  with handle H to file
    called FILENAME in the format specified by FORMAT. FORMAT can be the
    same values as extensions of FILENAME.
    The FILENAME extension does not have to be the same as FORMAT.
    The specified FORMAT overrides FILENAME extension.

    Valid options for FORMAT are:

    ‘fig‘  - save figure to a single binary FIG-file.  Reload using OPEN.
    ‘m‘    - save figure to binary FIG-file, and produce callable
             M-file for reload.
    ‘mfig‘ - same as M.
    ‘mmat‘ - save figure to callable M-file as series of creation commands
             with param-value pair arguments.  Large data is saved to MAT-file.
             Note: MMAT Does not support some newer graphics features. Use
                   this format only when code inspection is the primary goal.
                   FIG-files support all features, and load more quickly. 

saveas的三个参数:(1)图形句柄,如果图形窗口标题栏是“Figure 3”,则句柄就是3.可以用gcf参量抓取当前图像句柄。

(2)文件名。是字符串。

(3)单引号字符串,指定存储格式。

实际上可以只输入两个参量,利用文件名直接强制转换成我们想要的图片格式,例如

saveas(gcf,[‘D:\MATLAB7\work‘,‘mypicture‘,‘.jpg‘]);

或者直接

saveas(gcf,‘D:\MATLAB7\work\mypicture.jpg‘);

2.7.2 print命令

print命令本来是matlab用来打开打印机的命令,也可以用来保存图像。

print的三个参数:(1)图形句柄,如果图形窗口标题栏是“Figure 3”,则句柄就是3.用gcf可以获取当前窗口句柄。

(2)单引号字符串,指定存储格式。

png格式:‘-dpng‘

jpeg:    ‘-djpeg‘,

tiff: ‘-dtiff‘

bmp: ‘-dbitmap‘

(3)文件名。

例如

>> x=-pi:2*pi/300:pi; >> y=sin(x);>> plot(x,y);>> print(gcf,‘-dpng‘,‘abc.png‘)   % 保存为png格式的图片。

>> figure(2)     %新建一个句柄为2的图形窗口。

>> plot(x,cos(x));    %在句柄为2的图形窗口上画图。

>> grid

>> print(2,‘-djpeg‘,‘C:\abc.jpeg‘); %将句柄为2的图形保存为jpeg/jpg格式的图片,文件名为‘C:\abc.jpeg‘。

时间: 2024-10-05 10:37:17

Matlab绘制图像及图像的处理的相关文章

用MATLAB绘制的一个单词“LOVE”的图像

APEC放假最后一天啦,在家里鼓捣MATLAB,突然想到用MATLAB里的函数图像画一个好玩的东西.想来想去,就画成了这个样子: 这个图像是由以下四个方程的图像构成的 1)y=1/(x+4.5)-4.5 2)((x+2)/1.5)^2+(y/2.5)^2=1 3)y=|-4x+5|-1 4)x=-2.1|sin(y)|+4.6 制作的方式如下: 1)在MATLAB程序中上方的菜单中选择 New→Script 2)在脚本界面输入下面的代码,保存到DrawStringLove.m function

C# 绘制Mandelbrot集合图像

关于MandelbrotSet的定义,可以参考英文版维基百科条目 Mandelbrot Set 本程序是一个单窗体程序,里面只有一个PictureBox控件pcbMS,用于放置绘制好的图像 一.23次迭代的黑白版本 1)生成图像 2)程序源码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syst

matlab各类数据l图像之间的转化

matlab各类数据图像之间的转化 rgb类型转化为二值的过程如下: 1.采用命令im2double将rgb类型转化三维的double >> str='E:\programing\Eigenface_PAC\Face\image_0001.jpg'; >> A=imread(str); >> imshow(A); 2.用命令imresize调整图像的尺寸大小 >> B=imresize(A,[529 529]); >> imshow(B); 3.

用Matplotlib绘制二维图像

唠叨几句: 近期在做数据分析,需要对数据做可视化处理,也就是画图,一般是用Matlib来做,但Matlib安装文件太大,不太想直接用它,据说其代码运行效率也很低,在网上看到可以先用Java做数据处理,然后调用Matlib来画图,另外,还可以使用Matplotlib,它是用Python写的类似Matlib的库,能实现Matlib的功能,而且画图的质量很高,可用于做论文发表.找了一天的资料,终于出图了. Matplotlib需要配合numpy,scipy才能使用,具体安装步骤稍后补充. 正文: 用M

【转载】matlab练习程序(图像Haar小波变换)

matlab练习程序(图像Haar小波变换) 关于小波变换我只是有一个很朴素了理解.不过小波变换可以和傅里叶变换结合起来理解. 傅里叶变换是用一系列不同频率的正余弦函数去分解原函数,变换后得到是原函数在正余弦不同频率下的系数. 小波变换使用一系列的不同尺度的小波去分解原函数,变换后得到的是原函数在不同尺度小波下的系数. 不同的小波通过平移与尺度变换分解,平移是为了得到原函数的时间特性,尺度变换是为了得到原函数的频率特性. 小波变换步骤: 1.把小波w(t)和原函数f(t)的开始部分进行比较,计算

matlab ( octave ) imwrite 保存图像详解

刚刚写了imshow, 想了想发现imwrite和imshow是完全一致的, 所以根据上篇文章简单写写imwrite用法. 上篇文章链接: http://blog.csdn.net/watkinsong/article/details/38535341 采用图像: imwrite() 中, 如果参数为uint8类型, 那么期待的参数像素值范围为0-255, 如果参数矩阵为double类型, 那么期待的像素值范围为0-255. 在imwrite中, 如果你将读取的图像转换为double类型, 直接

Python绘制不同激活函数图像

1 """ 2 功能:Python绘制不同激活函数图像 3 姓名:侯俊龙 4 日期:2019/12/07 5 """ 6 7 import matplotlib.pyplot as plt 8 import numpy as np 9 10 x = np.linspace(-10,10) 11 # 绘制sigmoid图像 12 fig = plt.figure() 13 y_sigmoid = 1/(1+np.exp(-x)) 14 ax = f

用matlab绘制幂函数

用matlab绘制幂函数 下周轮到我做论文汇报了,刚好前两天看了网格水印的文章,就决定汇报前两天看到的那篇论文了.在准备ppt的过程中,绘制了一些幂函数,感觉matlab真的是很强大啊,可以绘制各种曲线.下面就简要介绍一下如何用matlab绘制幂函数的曲线. 上图绘制的曲线是Y = X^k,k的取值可以从曲线上看出.曲线上的“k=xxx”是截图后在绘图工具中添加的,便于直观的查看k与曲线的对应.在如上图所示的曲线中,我们设置横坐标X的取值范围为[0,1]. 绘制k=0.25的曲线代码如下 x=0

使用Matlab绘制三维图的几种方法

以下六个函数都可以实现绘制三维图像: surf(xx,yy,zz); surfc(xx,yy,zz); mesh(xx,yy,zz); meshc(xx,yy,zz); meshz(xx,yy,zz); waterfall(xx,yy,zz); plot3(xx,yy,zz); 其中值得说明的是如何构造出对应的数据出来(xx, yy, zz)出来.下面通过一段标准的代码段进行展示如何构造出相应的数据. x=-1:0.1:1; y=-1:0.1:1; [xx,yy]=meshgrid(x,y);