简体   繁体   English

MATLAB inputParser中的异常行为

[英]anomalous behaviour in MATLAB inputParser

I'm using inputParser to validate inputs to an adaptive thresholding routine in Matlab 2016a. 我正在使用inputParser验证Matlab 2016a中自适应阈值程序的输入。

The input is: 输入是:

AdaptBinarize2(Image, 'WeightedNiblack', 'meanWeight', 0.3, 'varianceWeight', 0.5, 'meanWindow', 8, 'varianceWindow', 8);

Where the name-value pairs 'meanWindow', 8 and 'varianceWindow', 8 are of interest. 其中名称 - 值对'meanWindow',8和'varianceWindow',8是有意义的。

The inputParser is a local function in the same file as AdaptBinarize: inputParser是与AdaptBinarize在同一文件中的本地函数:

function p = parseInputs(I, algorithm, varargin)

p = inputParser;

deft = 0.5; %default t, mean weight
defs = 2*floor((size(I,1)+size(I,2))/32)+1; %default s, window size
defk = 0; %default k, variance weight
defm = 128; %default m, Sauvola variance term (max variance of uint8)
defPol = 'Bright'; %foreground polarity, 'Bright' or 'Dark'
defGPU = 'False'; %whether to use CUDA

addRequired(p, 'I', @isnumeric);
addRequired(p, 'algorithm', @ischar);

addOptional(p, 'windowSize', defs, @isint);
addOptional(p, 'meanWindow', defs, @isint); %<--- this sucks
addOptional(p, 'varianceWindow', defs, @isint);

addOptional(p, 'meanWeight', deft, @isnumeric);
addOptional(p, 'varianceWeight', defk, @isnumeric);
addOptional(p, 'magicNumber', defm, @isint);

addOptional(p, 'polarity', defPol, @ischar);
addOptional(p, 'UseGPU', defGPU, @ischar);

parse(p,I,algorithm, varargin{:})

end

I'm using @isint as a validation function. 我正在使用@isint作为验证功能。 isint is defined as a local function in the same file as AdaptBinarize2(): isint在与AdaptBinarize2()相同的文件中定义为本地函数:

function bool = isint(x)

epsilon = 10^-9;

if mod(x, 1) < epsilon;
    bool = true;
else
    bool = false;
end

end

When entering the input, I get the error message : 输入输入时,我收到错误消息:

Error using AdaptBinarize2>parseInputs (line 371)
The value of 'meanWindow' is invalid. It must satisfy the function: isint.

Error in AdaptBinarize2 (line 67)
p = parseInputs(I, algorithm, varargin{:}); 
%check all the name-value pairs and datatypes. local function.

isint(8) = true. isint(8)= true。 If I change the meanWindow check handle to @isnumeric the parsing is succesful. 如果我将meanWindow检查句柄更改为@isnumeric,则解析成功。 All the others sanity checks which use @isint pass. 所有其他使用@isint传递的健全性检查。 Including varianceWindow, which is input at the same time with the same value and still passes. 包括varianceWindow,它同时以相同的值输入并仍然通过。

When extracting the values from the inputParser struct after changing the @isint to @isnumeric the values are the same with the same datatype. 在将@isint更改为@isnumeric之后从inputParser结构中提取值时,值与相同的数据类型相同。

If I switch the lines of meanWindow and varianceWindow in inputParser the exact same behaviour happens in reverse, with varianceWindow failing unless I change the test and meanWindow passing. 如果我在inputParser中切换meanWindow和varianceWindow的行,则反向完全相同的行为,varianceWindow失败,除非我更改测试和meanWindow传递。 The order of input doesn't seem to have any effect. 输入顺序似乎没有任何影响。

What is happening and why? 发生了什么,为什么?

Never mind, rubber ducked it. 没关系,橡胶躲了起来。 The syntax was wrong, I though that addOptional() would work like addParameter() works really. 语法错了,我虽然addOptional()会像addParameter()一样工作。 addOptional() is a positional argument, so the name string-identifier is not enough to push stuff into their right places. addOptional()是一个位置参数,因此名称string-identifier不足以将东西推送到正确的位置。 addParameter() can sort them with just the name like I tried to. addParameter()可以使用我想要的名称对它们进行排序。

So instead of having a good routine that exhibits anomalous behaviour I had a broken routine that worked purely by chance. 因此,我没有一个表现出异常行为的良好程序,而是一个完全按机会运作的破坏程序。 It even output images that looked like what I expected with the inputs shuffled! 它甚至输出看起来像我预期的输入洗牌的图像!

I think you need to use addParameter rather than addOptional . 我认为你需要使用addParameter而不是addOptional

addOptional indicates that the argument is an optional argument. addOptional表示参数是可选参数。 Optional arguments follow on from the first argument, must be in order, but later ones can be omitted and are replaced by defaults. 从第一个参数开始的可选参数必须按顺序排列,但后面的参数可以省略并替换为默认值。

addParameter indicates that the argument is a parameter-value pair . addParameter表示参数是参数值对 Parameter-Value pair arguments can come in any order, and are passed in as a pair with the name of the argument and its value. 参数 - 值对参数可以按任何顺序排列,并作为参数名称及其值传递。

PS prior to R2013b, use addParamValue rather than addParameter . 在R2013b之前的PS,使用addParamValue而不是addParameter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM