c++ - Matlab Codegen 构建错误

标签 c++ matlab matlab-coder

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

"??? Unless 'rows' is specified, the first input must be a vector. If the vector is variable-size, the either the first dimension or the second must have a fixed length of 1. The input [] is not supported. Use a 1-by-0 or 0-by-1 input (e.g., zeros(1,0) or zeros(0,1)) to represent the empty set."

然后它指向 [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 

最佳答案

仅供引用,如果您使用的是 MATLAB R2013b 或更新版本,您可以将 error(nargchk(1,2,nargin)) 替换为 narginchk(1,2).

如错误消息所述,对于 codegen unique 要求输入是 vector ,除非传递“行”。

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

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

您可以执行以下操作之一:

使 id 成为 vector ,并将其视为计算的 vector 。而不是声明:

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

你可以使用:

id = 1:numel(I)

那么 id 就是一个行 vector 。

您也可以保持代码不变并执行如下操作:

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

显然,这会生成一个拷贝,idtemp,但它可能会减少对代码的更改。

关于c++ - Matlab Codegen 构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25839277/

相关文章:

python - 我怎样才能在 C++ 中实现这个结果?数组指向数组

matlab - 颜色和标记的图例条目

c - MATLAB 嵌入式 C 函数问题

algorithm - 在 Matlab 中查找唯一二进制排列的快速方法

c - mex 文件和使用 coder.ceval 调用的函数之间有什么区别吗?

c++ - 在 Visual Studio 或 g++ 上运行 Matlab Coder 输出项目

c - Matlab Coder 是否仅生成单线程 C 应用程序?

c++ - 无符号类型和 C++

c++ - 用于分析等价物的 AppCode 构建

c++ - Arduino:LiquidCrystal 无法使用以太网库进行打印