一、处理可变数量的输入和输出
利用nargin和nargout进行检测
T = testhv(4, 5) |
该函数体中使用nargin返回2,使用nargout返回1.
函数nargchk可用于一个M函数体中,以检测传递函数的参量数目是否正确。
msg = nargchk(low, high, number)
参量number介于low与high之间返回空矩阵,否则返回错误信息。通常可这样使用:
function G = testhv2(x, y, z)
...
error(nargchk(2, 3, nargin));
...
|
键入仅有一个输入变量的语句
testhv2(6);
将产生错误消息
Not enough input arguments
同时执行终止。
通常,写出具有可变数目的输入变量和输出变量的函数时十分有用的。这里,我们使用变量varargin和变量varargout。varargin和varargout鼻血使用小写形式。例如,
function [m, n] = testhv3(varargin)
将输入的变量数读取到函数testhv3中,而
function [varargout] = testhv4(m, n, p)
则通过函数testhv4返回输出的变量数。若函数testhv3有一个固定的输入变量x,后跟输入变量的可变数目,则调用
function [m, n] = testhv3(x, varargin)
函数时,会导致varargin由用户提供的第二个输入变量开始运行。varargout的情形与此类似。一个函数的输入变量和输出变量的个数是可变的。
当varargin用做一个函数的输入变量时,Matlab会将其置入一个单元数组中,该数组接受由用户输入的变量数。由于varargin是一个单元数组,所以此类配置的一个重要方面是对函数的调用可包括输入的混合集。例如,若我们要使用假设函数testhv3的代码来处理此项操作,则它能很好的接受输入的混合集,如
[m, n] = testhv3(f, [0 0.5 1.5], A, 'label');
其中,f是一幅图像,下一个变量时一个长度为3的行向量,A是一个矩阵,'label'是一个字符串。
二、亮度变换的另一个M函数
在这一节中,我们将开发一个计算如下变换功能的函数:负片变换、对数变换、gamma变换和对比度拉伸变换。选用这些变换是因为随后我们将用到它们。此外,我们将进一步说明编写亮度变换M函数所涉及的机理。在编写该函数时,我们将用到函数changeclass,其语法为:
g = changeclass(newclass, f)
function image = changeclass(class, varargin)
%CHANGECLASS changes the storage class of an image.
%
%
%
%
%
%
switch class
case 'uint8'
case 'uint16'
case 'double'
otherwise
end
|
此函数将图象f转换成有参数newclass指定的类别,并输出图像g。newclass的有效值是'uint8', 'uint16', 和'double'。
function g = intrans(f, varargin)
%INTRANS Performs intensity (gray-level) transformations.
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
% Verify the correct number of inputs.
error(nargchk(2, 4, nargin))
% Store the class of the input for use later.
classin = class(f);
% If the input is of class double, and it is outside the range
% [0, 1], and the specified transformation is not 'log', convert the
% input to the range [0, 1].
if strcmp(class(f), 'double') & max(f(:))> 1 & ...
else % Convert to double, regardless of class(f).
end
% Determine the type of transformation specified.
method = varargin{1};
% Perform the intensity transformation specified.
switch method
case 'neg'
case 'log'
case 'gamma'
case 'stretch'
otherwise
end
% Convert to the class of the input image.
g = changeclass(classin, g);
|
例3.3 函数intrans的说明
要说明函数intrans,我们可以利用下面的例子,左边为原始图,右边为经对比度拉伸后的图像。
>> f = imread('Fig0306(a)(bone-scan-GE).tif');
>> figure(1)
>> imshow(f)
>> g = intrans(f, 'stretch', mean2(im2double(f)), 0.9);
>> figure(2)
>> imshow(g)
|
三、亮度标度的M函数
当处理图像是,象素值域由负到正的现象是很普遍的。尽管在中间计算过程中没有问题,但当我们想利用8bit或16bit格式保存或查看一幅图像时,就会出现问题。在这种情况下,我们通常希望吧图像标度在全尺度,即最大范围[0, 255]或[0, 65535]。下列名为gscale的M函数可以实现此功能。此外,此函数能将输出映射到一个特定的范围。
function g = gscale(f, varargin)
%GSCALE Scales the intensity of the input image.
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
if length(varargin) == 0 % If only one argument it must be f.
else
end
if strcmp(class(f), 'double') & (max(f(:))> 1 | min(f(:)) < 0)
end
% Perform the specified scaling.
switch method
case 'full8'
case 'full16'
case 'minmax'
otherwise
end
|
调用语法为:
g = gscale(f, method, low, high)
其中,f是将被标度的图像,method的有效值为'full8'和'full16',前者将输出标度为全范围[0, 255],后者将输出标度为全范围[0, 65535]。若使用这两个有效值之一,则可在两种变换中省略参数low与high。method的第三个有效值为'minmax',此时我们必须给出low与high在范围[0, 1]内的值。若选用的是'minmax',则映射的结果