0

我正在尝试使用 codegen 将下面的 Matlab 代码转换为 C++。但是它在构建时失败了,我得到了错误:

"??? 除非指定 'rows',否则第一个输入必须是向量。如果向量是可变大小的,则第一个维度或第二个维度必须具有固定长度 1。不支持输入 [] . 使用 1×0 或 0×1 输入(例如 zeros(1,0) 或 zeros(0,1))来表示空集。

然后它指向 [id,m,n] = unique(id); 成为罪魁祸首。为什么它不构建以及修复它的最佳方法是什么?

function [L,num,sz] = label(I,n) %#codegen

% Check input arguments
error(nargchk(1,2,nargin));
if nargin==1, n=8; end

assert(ndims(I)==2,'The input I must be a 2-D array')

sizI = size(I);
id = reshape(1:prod(sizI),sizI);
sz = ones(sizI);

% Indexes of the adjacent pixels
vec = @(x) x(:);
if n==4 % 4-connected neighborhood
idx1 = [vec(id(:,1:end-1)); vec(id(1:end-1,:))];
idx2 = [vec(id(:,2:end)); vec(id(2:end,:))];
elseif n==8 % 8-connected neighborhood
idx1 = [vec(id(:,1:end-1)); vec(id(1:end-1,:))];
idx2 = [vec(id(:,2:end)); vec(id(2:end,:))];
idx1 = [idx1; vec(id(1:end-1,1:end-1)); vec(id(2:end,1:end-1))];
idx2 = [idx2; vec(id(2:end,2:end)); vec(id(1:end-1,2:end))];
else
error('The second input argument must be either 4 or 8.')
end

% Create the groups and merge them (Union/Find Algorithm)
for k = 1:length(idx1)
root1 = idx1(k);
root2 = idx2(k);

while root1~=id(root1)
id(root1) = id(id(root1));
root1 = id(root1);
end
while root2~=id(root2)
id(root2) = id(id(root2));
root2 = id(root2);
end

if root1==root2, continue, end
% (The two pixels belong to the same group)

N1 = sz(root1); % size of the group belonging to root1
N2 = sz(root2); % size of the group belonging to root2

if I(root1)==I(root2) % then merge the two groups
if N1 < N2
    id(root1) = root2;
    sz(root2) = N1+N2;
else
    id(root2) = root1;
    sz(root1) = N1+N2;
end
end
end

while 1
id0 = id;
id = id(id);
if isequal(id0,id), break, end
end
sz = sz(id);

% Label matrix
isNaNI = isnan(I);
id(isNaNI) = NaN;
[id,m,n] = unique(id);
I = 1:length(id);
L = reshape(I(n),sizI);
L(isNaNI) = 0;

if nargout>1, num = nnz(~isnan(id)); end 
4

2 回答 2

1
  1. 删除存储在变量中的匿名函数vec并制作vec一个子函数:

    function y = vec(x)
    coder.inline('always');
    y = x(:);
    
  2. 如果没有该'rows'选项,unique函数的输入总是被解释为一个向量,而输出总是一个向量,无论如何。因此,例如,如果矩阵的所有元素都是唯一的,则类似id = unique(id)的东西会产生效果。将输入设为向量并没有什么坏处。所以换行id = id(:)id

    [id,m,n] = unique(id);
    

    [id,m,n] = unique(id(:));
于 2014-09-16T23:03:39.777 回答
1

仅供参考,如果您使用的是 MATLAB R2013b 或更高版本,则可以替换error(nargchk(1,2,nargin))narginchk(1,2).

正如错误消息所说,对于 codegenunique要求输入是向量,除非传递了“rows”。

如果您查看报告(单击显示的“打开报告”链接)并将鼠标悬停在 id 上,您可能会看到它的大小既不是1-by-N也不是N-by-1unique如果您在此处搜索,可以看到对的要求unique

http://www.mathworks.com/help/coder/ug/functions-supported-for-code-generation--alphabetical-list.html

您可以做以下几件事之一:

制作id一个向量并将其视为计算的向量。而不是声明:

id = reshape(1:prod(sizI),sizI);

你可以使用:

id = 1:numel(I)

然后id将是一个行向量。

您还可以保持代码原样并执行以下操作:

[idtemp,m,n] = unique(id(:));
id = reshape(idtemp,size(id));

显然,这将导致复制, idtemp, 但它可能涉及对您的代码的较少更改。

于 2014-09-16T20:48:44.817 回答