Main Content

Install and Use Computer Vision Toolbox Interface for OpenCV in Simulink

You can import OpenCV code to a Simulink model by using the OpenCV Importer application. The OpenCV Importer application is available only after you install the Computer Vision Toolbox™ Interface for OpenCV in Simulink® support package.

Installation

To install the support package, first click the Add-Ons drop-down list on the MATLAB® Home tab, and then select Get Add-Ons. In the Add-Ons Explorer window, find and click the Computer Vision Toolbox Interface for OpenCV in Simulink support package, and then click Install.

Import OpenCV Code into Simulink

To start the OpenCV Importer, click Apps on the MATLAB Toolstrip. Click the down arrow to show more options. Under My Apps, click the OpenCV Importer app icon. Alternatively, you can start the OpenCV Importer from the command-line interface. At the MATLAB command line, enter:

Simulink.OpenCVImporter

The OpenCV import wizard opens to a Welcome page.

  1. In the Project name field, specify a name for your import. You can either start a new import or load files saved from a previous import. The projects are saved in .m file format. To browse a saved file from previous import, select Load a file saved from a previous import. Click Next.

  2. In the Specify OpenCV Library page, specify your C++ library information. If you import a previously saved project file, all the fields are autopopulated.

    • Project root folder: A writable folder path where you want to save your output files (wrapper files and Simulink library).

    • Source files: OpenCV source file path. Specify the .cpp file format. If you provide an absolute path, then the wizard uses the file from the specified location. If you do not provide the absolute path, then the wizard uses the path relative to the project root.

    • Include files: Header files path. Specify the .hpp file format. If you provide an absolute path, then the wizard uses the file from the specified location. If you do not provide the absolute path, then the wizard uses the path relative to the project root.

    • Include paths: Define any additional include folders (Folder information). MATLAB OpenCV include files are included.

    • Library paths: Specify the path to external library files.

    If you have multiple files or folders to specify, use a semicolon-separated list of files or folders. Click Next.

  3. To find functions and types that are supported for import, analyze your library by clicking Next. Once the analysis is complete, click Next.

  4. In the What to Import page, the functions that Computer Vision Toolbox Interface for OpenCV in Simulink supports are listed. Select the functions that you want to import into Simulink library and click Next.

  5. Each I/O Type corresponds to the OpenCV function argument to map into the Simulink model. These different I/O Type are supported:

    • Input- for input arguments

    • Output- for output arguments

    • InputOutput- for input output arguments

    To select the input/output types, double-click the Output option in the I/O Type column drop-down list, and then click Next.

  6. In the Create Simulink Library page, you can generate either just a subsystem block or a subsystem block and a C Caller block of the selected function. A C Caller block integrates your OpenCV data into Simulink. The generated subsystem block contains C Caller blocks configured by using data conversion blocks.

    To generate a subsystem block and a C Caller block, select Create a single C-caller block for the OpenCV function, and then click Next.

    If the OpenCV code contains a Mat data type, the default output size is (720,1280,3) and the default underlying type is uint8. For vectors, the default size is 100. You can change the default size based on your model requirements.

  7. A Simulink library Projectname_Lib.slx is created from your OpenCV code into a project root folder. The library contains a subsystem block and a C Caller block.

    You can drag any of these blocks to your model, connect them to the existing blocks in the model, and run the model simulation.

    The wizard also creates wrapper files for source and header files.

Limitations

The Computer Vision Toolbox Interface for OpenCV in Simulink support package:

  • Uses OpenCV as part of MATLAB third-party support. You can get the OpenCV additional capabilities in Add-Ons (Computer Vision Toolbox).

  • Does not support external OpenCV libraries (for instance, opencv_contrib).

  • Does not support InputArrary, OutputArray, and InputOutputArray data types.

  • Requires Microsoft® Visual Studio® 2015 or 2017 Professional and Community editions for Windows 64 operating system. For more information on compilers, see Compilers Used to Build OpenCV Libraries.

  • Supports C++ code generation that uses row-major array layout.

See Also

|

Related Topics