matlabmatlab-coder

why does strel failed in MATLAB Coder


Using MATLAB Coder in 2019b, the following line:

se = strel('line',20,85);

that MATLAB Coder is unable to effectively describe:

1.All inputs must be constant.

2.Function call failed

why is Coder having difficulty in strel?

does matlab coder support strel?


Solution

  • MATLAB Coder does support strel. I tried the following simple example in R2019b and noticed it worked.

    % Function foo.m
    function y = foo
        se = strel('line',20,85);
        y = numel(se.Neighborhood);
    end
    
    % Codegen command in command window
    >> codegen foo -report
    

    From the error messages you posted, it looks like your inputs to strel are not constant, which is why codegen is failing. Refer to the section on C/C++ code generation section in the documentation here: https://www.mathworks.com/help/images/ref/strel.html which states the following:

    Usage notes and limitations:

    • strel supports the generation of C code (requires MATLAB® Coder™). For more information, see Code Generation for Image Processing.
    • All input arguments must be compile-time constants.
    • The methods associated with strel objects are not supported in code generation.
    • Arrays of strel objects are not supported.

    Example showing imerode based on comment:

    % Function foo.m
    function y = foo(edgeimg)
        se = strel('line',20,85);
        y = imerode(edgeimg, se.Neighborhood);
    end
    
    % Codegen command in command window
    % The image must be 2D or 3D according to doc
    >> edgeimg = imread('blah.png'); 
    >> codegen foo -args edgeimg -report