今天学习http://blog.csdn.net/lyqmath/article/details/6139043 lyqmath的代码
我将其代码复制到一个txt文档,然后署名‘test face’,并更改后缀名为‘.m’。使用matlab运行,报错“Undefined function or method 'test' for input arguments of type 'char'”,看到就傻眼了。Google了一下,看到帖子http://hi.baidu.com/liuzhiliangliang/blog/item/cdb1be189658d0bf4aedbcb2.html 看到重命名文件,我想可能是我的M文件名出错了,然后修改为‘test2’,之后运行正常。
n = ndims(A)
n = ndims(A) returns the number of dimensions in the array A. The number of dimensions in an array is always greater than or equal to 2. Trailing singleton dimensions are ignored. A singleton dimension is any dimension for which size(A,dim) = 1.
“n = ndims(A)”返回在阵列A中维数的数目。在一个阵列中的维数的数目总是大于或等于2。“Trailing singleton dimensions ”这个维数被忽略(不清楚双引号里面的是什么意思,直译为:拖尾的单例维数)。单例维数(singleton dimension)是“size(A,dim) = 1”这样的维数。(关于size会在后面介绍)
ndims(x) is length(size(x)).
d= size(X)
[m,n] = size(X)
m = size(X,dim)
[d1,d2,d3,...,dn] = size(X),
d = size(X) returns the sizes of each dimension of array X in a vector d with ndims(X) elements. If X is a scalar, which MATLAB software regards as a 1-by-1 array, size(X) returns the vector [11]. 一个矢量d包含“ndims(X)”个元素,返回阵列X的各维的大小到d。如果X是一个标量,则MATLAB软件视其一个1*1的阵列,size(X)返回矢量[1 1]。
[m,n] = size(X) returns the size of matrix X in separate variables m and n. 返回有单独变量m和n的矩阵X的大小。
m = size(X,dim) returns the size of the dimension of X specified by scalar dim. 返回指定标量dim的X的维数大小
[d1,d2,d3,...,dn] = size(X), for n > 1, returns the sizes of the dimensions of the array X in the variables d1,d2,d3,...,dn, provided the number of output arguments n equals ndims(X). If n does not equal ndims(X), the following exceptions hold:
返回阵列X的维数大小到变量d1,d2,d3,...,dn中,提供的输出参数n等于ndims(X)。如果n不等于ndims(X),异常处理如下:
n < ndims(X) |
di equals the size of the ith dimension of X for 1<=i di等于X第i个维数的大小,但是dn等于X剩余维数的大小,也就是,维数n到ndims(X) (待查:感觉翻译得很不准确) |
n > ndims(X) |
size returns ones in the "extra" variables, that is, those corresponding to ndims(X)+1 through n. size返回到一个临时变量中,也就是,那些从n相对应到ndims(X)+1 |
Note For a Java array, size returns the length of the Java array as the number of rows. The number of columns is always 1. For a Java array of arrays, the result describes only the top level array. |
The size of the second dimension of rand(2,3,4) is 3. ‘rand(2,3,4)’的第二维的大小是3。
m = size(rand(2,3,4),2)
m =
3
Here the size is output as a single vector. 这里的大小是用一个单一向量输出
d = size(rand(2,3,4))
d =
2 3 4
Here the size of each dimension is assigned to a separate variable. 这里的每个维数大小被分配给分开的变量。
[m,n,p] = size(rand(2,3,4))
m =
2
n =
3
p =
4
If X= ones(3,4,5), then 如果 X = ones(3,4,5)
[d1,d2,d3] = size(X)
d1 = d2 = d3 =
3 4 5
But when the number of output variables is less than ndims(X): 但是当输出变量的大小小于 ndims(X)时
[d1,d2] = size(X)
d1 = d2 =
3 20
The "extra" dimensions are collapsed into a single product. 额外维数被收缩到一个单一结果
If n> ndims(X), the "extra" variables all represent singleton dimensions: 如果‘n> ndims(X)’,“额外的“变量均表现出单件模式的维度
[d1,d2,d3,d4,d5,d6] = size(X)
d1 = d2 = d3 =
3 4 5
d4 = d5 = d6 =
1 1 1
I = rgb2gray(RGB)
newmap = rgb2gray(map)
I = rgb2gray(RGB) converts the truecolor image RGB to the grayscale intensity image I. rgb2gray converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance.
转换真色图像RGB为灰度, 亮度图I。rgb2gray转换RGB图为灰度图去除色调和色饱和度信息同时保留亮度。
newmap = rgb2gray(map) returns a grayscale colormap equivalent to map.
Note A grayscale image is also called a gray-scale, gray scale, or gray-level image. |
If the input is an RGB image, it can be of class uint8, uint16, single, or double. The output image I is of the same class as the input image. If the input isa colormap, the input and output colormaps are both of class double.
Convert an RGB image to a grayscale image.
I = imread('board.tif');
J = rgb2gray(I);
figure, imshow(I),figure, imshow(J);
Convert the colormap to a grayscale colormap.
[X,map] =imread('trees.tif');
gmap = rgb2gray(map);
figure, imshow(X,map),figure, imshow(X,gmap);
rgb2gray converts RGB values to grayscale values by forming aweighted sum of the R, G, and B components:
0.2989 * R + 0.5870 * G +0.1140 * B
Note that these are the same weights used by the rgb2ntsc function to compute the Y component.
BW= im2bw(I, level)
BW = im2bw(X, map, level)
BW = im2bw(RGB, level)
BW = im2bw(I, level) converts the grayscale image I to a binary image. The output image BW replaces all pixels in the input image with luminance greater than level with the value 1 (white) and replaces all other pixels with the value 0 (black). Specify level in the range [0,1]. This range is relative to the signal levels possible for the image's class. Therefore, a level value of 0.5 is midway between black and white, regardless of class. To compute the level argument, you can use the function graythresh.If you do not specify level, im2bw uses the value 0.5.
BW = im2bw(X, map, level) converts the indexed image X with colormap map to a binary image.
BW = im2bw(RGB, level) converts the truecolor image RGB to a binary image.
If theinput image is not a grayscale image, im2bw converts the input image to grayscale, and then converts thisgrayscale image to binary by thresholding.
Theinput image can be of class uint8, uint16, single, int16, or double, and must benonsparse. The output image BW is of class logical. Iand X must be 2-D. RGB images are M-by-N-by-3.
level= graythresh(I)
[level EM] = graythresh(I)
level = graythresh(I) computes a global threshold (level) that can beused to convert an intensity image to a binary image with im2bw.level is a normalized intensity value that lies in therange [0, 1].
The graythresh function uses Otsu's method, which chooses thethreshold to minimize the intraclass variance of the black and white pixels.
Multidimensionalarrays are converted automatically to 2-D arrays using reshape. The graythresh function ignores any nonzero imaginary part of I.
[level EM] = graythresh(I) returns the effectiveness metric, EM, as the second output argument. Theeffectiveness metric is a value in the range [0 1] that indicates theeffectiveness of the thresholding of the input image. The lower bound isattainable only by images having a single gray level, and the upper bound isattainable only by two-valued images.
Theinput image I can be of class uint8, uint16, int16, single,or double and it must be nonsparse. The return value level is a double scalar. The effectiveness metric EM is a double scalar.
I = imread('coins.png');
level = graythresh(I);
BW = im2bw(I,level);
imshow(BW)
[1] Otsu, N., "A Threshold Selection Methodfrom Gray-Level Histograms," IEEE Transactions on Systems, Man, and Cybernetics,Vol. 9, No. 1, 1979, pp. 62-66.
figure
figure('PropertyName',propertyvalue,...)
figure(h)
h = figure(...)
For a list of properties, see Figure Properties.
figure creates figure graphics objects. Figure objects are the individual windows on the screen in which the MATLAB software displays graphical output.
figure creates a new figure object using default property values. This automatically becomes the current figure and raises it above all other figures on the screen until a new figure is either created or called.
figure('PropertyName',propertyvalue,...) creates a new figure object using the values of the properties specified. For a description of the properties, see Figure Properties. MATLAB uses default values for any properties that you do not explicitly define as arguments.
figure(h) does one of two things, depending on whether or not a figure with handle h exists. If h is the handle to an existing figure,figure(h) makes the figure identified by h the current figure, makes it visible, and raises it above all other figures on the screen. The current figure is the target for graphics output. If h is not the handle to an existing figure, but is an integer, figure(h) creates a figure and assigns it the handle h. figure(h) where h is not the handle to a figure, and is not an integer, is an error.
h = figure(...) returns the handle to the figure object.
C = cat(dim, A, B)
C = cat(dim, A1, A2, A3, A4, ...)
C = cat(dim, A, B)concatenates the arrays A and B along array dimension dim.
C = cat(dim, A1, A2, A3, A4, ...)concatenates all the input arrays (A1, A2, A3, A4, and so on) along array dimension dim.
For nonempty arrays, cat(2, A, B) is the same as [A, B], and cat(1, A, B) is the same as [A; B].
When used with comma-separated list syntax, cat(dim, C{:}) or cat(dim, C.field) is a convenient way to concatenate a cell or structure array containing numeric matrices into a single matrix.
For information on combining unlike integer types, integers with nonintegers, cell arrays with non-cell arrays, or empty matrices with other elements, see Combining Unlike Classes in the Programming Fundamentals documentation.
Given
A = B = 1 2 5 6 3 4 7 8
concatenating along different dimensions produces
The commands
A = magic(3); B = pascal(3); C = cat(4, A, B);
produce a 3-by-3-by-1-by-2 array.
hold on
hold off
hold all
hold
hold(axes_handle,...)
The hold function determines whether new graphics objects are added to the graph or replace objects in the graph. hold toggles theNextPlot property between the add and replace states.
hold on retains the current plot and certain axes properties so that subsequent graphing commands add to the existing graph. If no current axes exist before you call hold on, MATLAB creates new axes and retains the default properties. However, some axes properties change to accommodate additional graphics objects. For example, the axes' limits increase when the data requires them to do so. hold onsets the NextPlot property of the current figure and axes to add.
hold off resets axes properties to their defaults before drawing new plots. hold off is the default. hold off sets the NextPlot property of the current axes to replace.
hold all holds the plot and the current line color and line style so that subsequent plotting commands do not reset the ColorOrder andLineStyleOrder property values to the beginning of the list. Plotting commands continue cycling through the predefined colors and linestyles from where the last plot stopped in the list.
hold toggles the hold state between adding to the graph and replacing the graph.
hold(axes_handle,...) applies the hold to the axes identified by the handle axes_handle. The hold function sets the NextPlot property of the current figure and the current axes. If several axes objects exist in a figure window, each axes has its own hold state. hold also creates an axes if one does not exist.
Test the hold state using the ishold function.
[X,Y] = meshgrid(xgv,ygv)
[X,Y,Z] = meshgrid(xgv,ygv,zgv)
[X,Y] = meshgrid(gv)
[X,Y,Z] = meshgrid(gv)
[X,Y] = meshgrid(xgv,ygv) replicates the grid vectors xgv and ygv to produce a full grid. This grid is represented by the output coordinate arrays X and Y. The output coordinate arrays X and Y contain copies of the grid vectors xgv and ygv respectively. The sizes of the output arrays are determined by the length of the grid vectors. For grid vectors xgv and ygv of length M and N respectively, X and Y will have Nrows and M columns.
[X,Y,Z] = meshgrid(xgv,ygv,zgv) produces three-dimensional coordinate arrays. The output coordinate arrays X, Y, and Z contain copies of the grid vectors xgv, ygv, and zgv respectively. The sizes of the output arrays are determined by the length of the grid vectors. For grid vectors xgv, ygv, and zgv of length M, N, and P respectively, X, Y, and Z will have N rows, M columns, and P pages.
[X,Y] = meshgrid(gv) is the same as [X,Y] = meshgrid(gv,gv). In other words, you can reuse the same grid vector in each respective dimension. The dimensionality of the output arrays is determined by the number of output arguments.
[X,Y,Z] = meshgrid(gv) is the same as [X,Y,Z] = meshgrid(gv,gv,gv). Again, the dimensionality of the output arrays is determined by the number of output arguments.
The output coordinate arrays are typically used to evaluate functions of two or three variables. They are also frequently used to create surface and volumetric plots.
xgv,ygv,zgv |
Grid vectors specifying a series of grid point coordinates in the x, y and z directions, respectively. |
gv |
Generic grid vector specifying a series of point coordinates. |
X,Y,Z |
Output arrays that specify the full grid. |
The meshgrid function is similar to ndgrid, however meshgrid is restricted to 2-D and 3-D while ndgrid supports 1-D to N-D. The coordinates output by each function are the same, but the shape of the output arrays in the first two dimensions are different. For grid vectors x1gv,x2gv and x3gv of length M, N and P respectively, meshgrid(x1gv, x2gv) will output arrays of size N-by-M while ndgrid(x1gv, x2gv) outputs arrays of size M-by-N. Similarly, meshgrid(x1gv, x2gv, x3gv) will output arrays of size N-by-M-by-P while ndgrid(x1gv, x2gv, x3gv) outputs arrays of size M-by-N-by-P. See Grid Representation in the MATLAB Mathematics documentation for more information.
Y = round(X)
Y = round(X) rounds the elements of X to the nearest integers. Positive elements with a fractional part of 0.5 round up to the nearest positive integer. Negative elements with a fractional part of -0.5 round down to the nearest negative integer. For complex X, the imaginary and real parts are rounded independently.
a = [-1.9, -0.2, 3.4, 5.6, 7.0, 2.4+3.6i] a = Columns 1 through 4 -1.9000 -0.2000 3.4000 5.6000 Columns 5 through 6 7.0000 2.4000 + 3.6000i round(a) ans = Columns 1 through 4 -2.0000 0 3.0000 6.0000 Columns 5 through 6 7.0000 2.0000 + 4.0000i
y = linspace(a,b)
y = linspace(a,b,n)
The linspace function generates linearly spaced vectors. It is similar to the colon operator ":", but gives direct control over the number of points.
y = linspace(a,b) generates a row vector y of 100 points linearly spaced between and including a and b.
y = linspace(a,b,n) generates a row vector y of n points linearly spaced between and including a and b. For n < 2, linspace returns b.
Create a vector of 100 linearly spaced numbers from 1 to 500:
A = linspace(1,500);
Create a vector of 12 linearly spaced numbers from 1 to 36:
A = linspace(1,36,12);
To graph selected variables, use the Plot Selector in the Workspace Browser, or use the Figure Palette Plot Catalog. Manipulate graphs in plot edit mode with the Property Editor. For details, see Plotting Tools — Interactive Plotting in the MATLAB Graphics documentation and Creating Graphics from the Workspace Browser in the MATLAB Desktop Tools documentation.
mesh(X,Y,Z)
mesh(Z)
mesh(...,C)
mesh(...,'PropertyName',PropertyValue,...)
mesh(axes_handles,...)
h = mesh(...)
mesh(X,Y,Z) draws a wireframe mesh with color determined by Z, so color is proportional to surface height. If X and Y are vectors,length(X) = n and length(Y) = m, where [m,n] = size(Z). In this case, (X(j), Y(i), Z(i,j)) are the intersections of the wireframe grid lines; X and Y correspond to the columns and rows of Z, respectively. If X and Y are matrices, (X(i,j), Y(i,j), Z(i,j)) are the intersections of the wireframe grid lines.
mesh(Z) draws a wireframe mesh using X = 1:n and Y = 1:m, where [m,n] = size(Z). The height, Z, is a single-valued function defined over a rectangular grid. Color is proportional to surface height.
mesh(...,C) draws a wireframe mesh with color determined by matrix C. MATLAB performs a linear transformation on the data in C to obtain colors from the current colormap. If X, Y, and Z are matrices, they must be the same size as C.
mesh(...,'PropertyName',PropertyValue,...) sets the value of the specified surface property. Multiple property values can be set with a single statement.
mesh(axes_handles,...) plots into the axes with handle axes_handle instead of the current axes (gca).
h = mesh(...) returns a handle to a Surfaceplot graphics object.
mesh does not accept complex inputs.
A mesh is drawn as a Surfaceplot graphics object with the viewpoint specified by view(3). The face color is the same as the background color (to simulate a wireframe with hidden-surface elimination), or none when drawing a standard see-through wireframe. The current colormap determines the edge color. The hidden command controls the simulation of hidden-surface elimination in the mesh, and theshading command controls the shading model.
B = zeros(n)
B = zeros(m,n)
B = zeros([m n])
B = zeros(m,n,p,...)
B = zeros([m n p ...])
B = zeros(size(A))
Y = zeros
zeros(m, n,...,classname)
zeros([m,n,...],classname)
B = zeros(n) returns an n-by-n matrix of zeros. An error message appears if n is not a scalar.
B = zeros(m,n) or B = zeros([m n]) returns an m-by-n matrix of zeros.
B = zeros(m,n,p,...) or B = zeros([m n p ...]) returns an m-by-n-by-p-by-... array of zeros.
B = floor(A)
B = floor(A) rounds the elements of A to the nearest integers less than or equal to A. For complex A, the imaginary and real parts are rounded independently.
ind = find(X)
ind = find(X, k)
ind = find(X, k, 'first')
ind = find(X, k, 'last')
[row,col] = find(X, ...)
[row,col,v] = find(X, ...)
ind = find(X) locates all nonzero elements of array X, and returns the linear indices of those elements in vector ind. If X is a row vector, then ind is a row vector; otherwise, ind is a column vector. If X contains no nonzero elements or is an empty array, then ind is an empty array.
ind = find(X, k) or ind = find(X, k, 'first') returns at most the first k indices corresponding to the nonzero entries of X. k must be a positive integer, but it can be of any numeric data type.
ind = find(X, k, 'last') returns at most the last k indices corresponding to the nonzero entries of X.
[row,col] = find(X, ...) returns the row and column indices of the nonzero entries in the matrix X. This syntax is especially useful when working with sparse matrices. If X is an N-dimensional array with N > 2, col contains linear indices for the columns. For example, for a 5-by-7-by-3 array X with a nonzero element at X(4,2,3), find returns 4 in row and 16 in col. That is, (7 columns in page 1) + (7 columns in page 2) + (2 columns in page 3) = 16.
[row,col,v] = find(X, ...) returns a column or row vector v of the nonzero entries in X, as well as row and column indices. If X is a logical expression, then v is a logical array. Output v contains the non-zero elements of the logical array obtained by evaluating the expression X. For example,
A= magic(4) A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 [r,c,v]= find(A>10); r', c', v' ans = 1 2 4 4 1 3 ans = 1 2 2 3 4 4 ans = 1 1 1 1 1 1
Here the returned vector v is a logical array that contains the nonzero elements of N where
N=(A>10)
L = bwlabel(BW, n)
[L, num] = bwlabel(BW, n)
L = bwlabel(BW, n) returns a matrix L, of the same size as BW, containing labels for the connected objects in BW. The variable n can have a value of either 4 or 8, where 4 specifies 4-connected objects and 8 specifies 8-connected objects. If the argument is omitted, it defaults to 8.
The elements of L are integer values greater than or equal to 0. The pixels labeled 0 are the background. The pixels labeled 1 make up one object; the pixels labeled 2 make up a second object; and so on.
[L, num] = bwlabel(BW, n) returns in num the number of connected objects found in BW.
The functions bwlabel, bwlabeln, and bwconncomp all compute connected components for binary images. bwconncomp replaces the use ofbwlabel and bwlabeln. It uses significantly less memory and is sometimes faster than the other functions.
Input Dimension | Output Form | Memory Use | Connectivity | |
---|---|---|---|---|
bwlabel | 2-D | Double-precision label matrix | High | 4 or 8 |
bwlabeln | N-D | Double-precision label matrix | High | Any |
bwconncomp | N-D | CC struct | Low | Any |
To extract features from a binary image using regionprops with default connectivity, just pass BW directly into regionprops, i.e.,regionprops(BW).
To compute a label matrix having a more memory-efficient data type (e.g., uint8 versus double), use the labelmatrix function on the output of bwconncomp. For more information, see the reference page for each function.
You can use the MATLAB find function in conjunction with bwlabel to return vectors of indices for the pixels that make up a specific object. For example, to return the coordinates for the pixels in object 2, enter the following:
[r, c] = find(bwlabel(BW)==2)
You can display the output matrix as a pseudocolor indexed image. Each object appears in a different color, so the objects are easier to distinguish than in the original image. For more information, see label2rgb.
BW can be logical or numeric, and it must be real, two-dimensional, and nonsparse. L is of class double.
Label components using 4-connected objects. Notice objects 2 and 3; with 8-connected labeling, bwlabel would consider these a single object rather than two separate objects.
BW = logical ([1 1 1 0 0 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 1 1 1 0 0 1 1 0 1 1 1 0 0 0 0 0]); L = bwlabel(BW,4) L = 1 1 1 0 0 0 0 0 1 1 1 0 2 2 0 0 1 1 1 0 2 2 0 0 1 1 1 0 0 0 3 0 1 1 1 0 0 0 3 0 1 1 1 0 0 0 3 0 1 1 1 0 0 3 3 0 1 1 1 0 0 0 0 0 [r, c] = find(L==2); rc = [r c] rc = 2 5 3 5 2 6 3 6
bwlabel uses the general procedure outlined in reference [1], pp. 40-48:
Run-length encode the input image.
Scan the runs, assigning preliminary labels and recording label equivalences in a local equivalence table.
Resolve the equivalence classes.
Relabel the runs based on the resolved equivalence classes.
[1] Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Volume I, Addison-Wesley, 1992, pp. 28-48.
STATS = regionprops(BW, properties)
STATS = regionprops(CC, properties)
STATS = regionprops(L, properties)
STATS = regionprops(..., I, properties)
STATS = regionprops(BW, properties) measures a set of properties for each connected component (object) in the binary image, BW. The image BW is a logical array; it can have any dimension.
STATS = regionprops(CC, properties) measures a set of properties for each connected component (object) in CC, which is a structure returned by bwconncomp.
STATS = regionprops(L, properties) measures a set of properties for each labeled region in the label matrix L. Positive integer elements of L correspond to different regions. For example, the set of elements of L equal to 1 corresponds to region 1; the set of elements of Lequal to 2 corresponds to region 2; and so on.
STATS = regionprops(..., I, properties) measures a set of properties for each labeled region in the image I. The first input toregionprops—either BW, CC, or L—identifies the regions in I. The sizes must match: size(I) must equal size(BW), CC.ImageSize, or size(L).
STATS is a structure array with length equal to the number of objects in BW, CC.NumObjects, or max(L(:)). The fields of the structure array denote different properties for each region, as specified by properties.
rectangle
rectangle('Position',[x,y,w,h])
rectangle('Curvature',[x,y])
rectangle('PropertyName',propertyvalue,...)
h = rectangle(...)
For a list of properties, see Rectangle Properties.
rectangle draws a rectangle with Position [0,0,1,1] and Curvature [0,0] (i.e., no curvature).
rectangle('Position',[x,y,w,h]) draws the rectangle from the point x,y and having a width of w and a height of h. Specify values in axes data units.
Note that, to display a rectangle in the specified proportions, you need to set the axes data aspect ratio so that one unit is of equal length along both the x and y axes. You can do this with the command axis equal or daspect([1,1,1]).
rectangle('Curvature',[x,y]) specifies the curvature of the rectangle sides, enabling it to vary from a rectangle to an ellipse. The horizontal curvature x is the fraction of width of the rectangle that is curved along the top and bottom edges. The vertical curvature y is the fraction of the height of the rectangle that is curved along the left and right edges.
The values of x and y can range from 0 (no curvature) to 1 (maximum curvature). A value of [0,0] creates a rectangle with square sides. A value of [1,1] creates an ellipse. If you specify only one value for Curvature, then the same length (in axes data units) is curved along both horizontal and vertical sides. The amount of curvature is determined by the shorter dimension.
rectangle('PropertyName',propertyvalue,...) draws the rectangle using the values for the property name/property value pairs specified and default values for all other properties. For a description of the properties, see Rectangle Properties.
h = rectangle(...) returns the handle of the rectangle object created.
好的先学习这么多函数
Matlab中怎么操作换行输入,怎么续行输入?
1. Shift + Enter
2. 句末加三个点'...'
自:http://www.mathworks.se/help/techdoc/
我是MATLAB小菜鸟,好好学习