Main Content

Code Generation for Image Processing

Some Image Processing Toolbox™ functions enable you to generate standalone C code, C code that depends on a precompiled, platform-specific shared library, or both. Generating code requires MATLAB® Coder™.

For a complete list of Image Processing Toolbox functions that support code generation, see Functions Supporting Code Generation. For an example of using code generation, see Generate Code for Object Detection.

Types of Code Generation Support in Image Processing Toolbox

Image Processing Toolbox offers three types of code generation support.

  • Functions that generate standalone C code. You can incorporate this code into applications that run on many platforms, such as ARM processors. An example of a function that supports only standalone C code is immse.

  • Functions that generate C code that depends on a platform-specific shared library (.dll, .so, or .dylib). Use of a shared library preserves performance optimizations in these functions, but this limits the target platforms on which you can run this code to only platforms that can host MATLAB. To view a list of host platforms, see system requirements. An example of a function that supports only C code that depends on a shared library is bwpack.

  • Functions that generate standalone C code or C code that depends on a shared library, depending on which target platform you specify in MATLAB Coder configuration settings. If you specify the generic MATLAB Host Computer target platform, then these functions generate C code that depends on a shared library. If you specify any other target platform, then these functions generate standalone C code. An example of a function that supports both standalone C code and C code that depends on a shared library is regionprops.

The diagram illustrates the difference between generating C code and generating code that uses a shared library.

Generate Code with Image Processing Functions

In generated code, each supported toolbox function has the same name, arguments, and functionality as its Image Processing Toolbox counterpart. To use code generation with image processing functions, follow these steps:

  • Write your MATLAB function or application as you would normally, using functions from the Image Processing Toolbox.

  • Add the %#codegen compiler directive at the end of the function signature. This directive instructs the MATLAB code analyzer to diagnose issues that would prohibit successful code generation.

  • Open the MATLAB Coder (MATLAB Coder) app, create a project, and add your file to the project. In the app, you can check the readiness of your code for code generation. For example, your code may contain functions that are not enabled for code generation. Make any modifications required for code generation.

  • Generate code by clicking Generate on the Generate Code page of the MATLAB Coder app. You can choose to generate a MEX file, a shared library, a dynamic library, or an executable.

    Even if you addressed all readiness issues identified by MATLAB Coder, you might still encounter build issues. The readiness check only looks at function dependencies. When you try to generate code, MATLAB Coder might discover coding patterns that are not supported for code generation. View the error report and modify your MATLAB code until you get a successful build.

See Also

(MATLAB Coder) | (MATLAB Coder)

Related Examples

More About