Search code examples
matlabimage-processingcode-generationmatlab-cvstmatlab-coder

Alternatives to extrinsic functions such as imread and other functions during code generation in MATLAB


As you may know, extrinsic functions are not outputted during the code generation process. Are there alternatives to these functions and/or solutions to this problem? My code generation error report is shown below :

Code Generation Error Report

I am surprised that I can't output size and rgb2gray either. Since these are essential to my program, I cannot avoid them.

Help will be much appreciated!


Solution

  • This is a good question, and I see similar questions fairly frequently. As I started using MATLAB Coder, one of the biggest pitfalls was the constant search for supported functions. I sympathize with your frustration, and I have a few tips, having been through this.

    First, to your direct question, while imread isn't supported by Coder, size and rgb2gray are. Probably Coder is complaining about these because they have been passed mxArrays from the call to imread, which is fine when it is extrinsic, but not ok for separate generation. That's just a guess. A very useful tool in writing code is the list of Coder supported functions: List of Functions supported in MATLAB Coder

    But even with those two, to replace imread is not a tiny task. You'll have to find another library that supports the particular file you're working with, and then stitch that in using coder.ceval. Alternatively, if you can find a pure MATLAB implementation of it, that might help.

    Are you targeting a pure C library or a MEX file? If you intend to use this code within the MATLAB environment, you can always use imread separately and then pass the data.

    And now to some more general observations: MATLAB Coder isn't a perfect MATLAB to C translation system. It's extremely powerful, and I've been able to write some very large projects with it, but if what you want is the ability to run any MATLAB code without MATLAB around, you should look at MATLAB Compiler, a different add-on. There's a very good Q and A about this here: MATLAB Compiler vs MATLAB Coder

    When writing projects in MATLAB Coder, it's really best to start from scratch, knowing you're targeting C code ultimately. There are so many gotchas in the conversion from MATLAB to C that you have to be always vigilant while writing the MATLAB code.

    One tool that helps is to right-click on a file in the "Current Folder" list that usually resides on the left-side of the main window, and select "Check Code Generation Readiness." You'll get a great report of potential problems in the file. I recommend using this often.

    Another useful tool is to always put the %#codegen tag into your code. This alerts the MATLAB editor that the .m file is intended for code generation, so it provides extra context-sensitive information while you're writing the file. This helps enormously.

    The most commonly missing functions for code generation are file IO functions. There are some good reasons for this, but it's frustrating nonetheless.

    When you stitch in external C code, you use the coder.ceval function, which can provide excellent access to external libraries. Using this well is a whole other topic, outside the scope of this question.

    If you can indicate exactly what kind of files you're interested in reading (PNG, BMP, TIFF, etc.) perhaps someone may be able to identify a good external library for you to use.