C Code Generation

Generate C/C++ code and MEX functions for toolbox functions, objects, and blocks

Several Computer Vision Toolbox™ functions have been enabled to generate C/C++ code. In general, the code you generate from Computer Vision Toolbox blocks is portable ANSI® C code. After you generate the code, you can deploy it on another machine.

For a list of Code Generation supported functions, usage notes, and limitations, refer to

For some Computer Vision Toolbox functions, code generation includes creation of a shared library.

To use code generation with computer vision functions, follow these steps:

  1. Write your Computer Vision Toolbox function or application as you would normally, using functions from the Computer Vision Toolbox.

  2. Add the %#codegen compiler directive to your MATLAB® code.

  3. Open the MATLAB Coder™ app, create a project, and add your file to the project. Once in MATLAB Coder, 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.

  4. Generate code by clicking Generate in the Generate Code dialog box. You can choose to build a MEX file, a C/C++ shared library, a C/C++ dynamic library, or a C/C++ 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.

For more information about code generation, see the MATLAB Coder documentation and the Introduction to Code Generation with Feature Matching and Registration example.


To generate code from MATLAB code that contains Computer Vision Toolbox functionality, you must have the MATLAB Coder software.


ToOpenCVConverts Simulink data types to OpenCV data types
FromOpenCVConverts OpenCV data types to Simulink data types


Install and Use Computer Vision Toolbox OpenCV Interface for Simulink

Import OpenCV code into Simulink®.

Convert RGB Image to Grayscale Image by Using OpenCV Importer

Convert an RGB image to a grayscale image by using OpenCV Importer.

Draw Different Shapes by Using OpenCV Code in Simulink

Draw different shapes by using OpenCV Importer.

Smile Detection by Using OpenCV Code in Simulink

Detect intensity of smile in an image by using OpenCV Importer.

Introduction to Code Generation with Feature Matching and Registration

This example shows how to use the MATLAB® Coder™ to generate C code for a MATLAB file.

Code Generation for Depth Estimation From Stereo Video

This example shows how to use the MATLAB® Coder™ to generate C code for a MATLAB function, which uses the stereoParameters object produced by Stereo Camera Calibrator app or the estimateCameraParameters function.

Code Generation for Face Tracking with PackNGo

This example shows how to generate code from Face Detection and Tracking Using the KLT Algorithm example with packNGo function.

Code Generation for Object Detection by Using YOLO v2

Generate CUDA® code for object detection using YOLO v2.

Multicore Simulation of Video Processing System

This example shows how to run a video processing system on multiple cores using dataflow execution domain in Simulink®.