2014-09-13 57 views
0

我試圖將一些Matlab代碼更改爲C++,但是當我使用%#codegen時,出現「代碼生成不支持匿名函數」的錯誤出現在vec = @(x) x(:);旁邊。下面是Matlab函數。我可以更改哪些內容來消除此錯誤?Matlab Codegen:不支持匿名函數

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 

回答

3

您可以使用下面的功能和評論匿名函數

function x=vec(id) 
    x = id(:); 
end 

這基本上沒有什麼相同的匿名是實現

+0

感謝。什麼定義類型是函數vec的「id」參數?例如。單(1 x Inf)? – user3926194 2014-09-14 05:13:52

+0

@ user3926194我不明白你的問題。你問什麼是'id'的數據類型?它與傳遞給'vec'的任何內容相同。 – P0W 2014-09-14 06:30:02