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, use one of the following methods:

  • If you are viewing the current page using installed documentation, click .

  • Select Get Add-ons from the Add-ons drop-down menu from the MATLAB® toolstrip. In the Add-Ons Explorer window, find and click the Computer Vision Toolbox Interface for OpenCV in Simulink support package, and then click Install.

  • Type visionSupportPackages in a MATLAB Command Window and follow the prompts.

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 Importer app 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 app uses the file from the specified location. If you do not provide the absolute path, then the app 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 app uses the file from the specified location. If you do not provide the absolute path, then the app uses the path relative to the project root.

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

    • Library files: Specify the 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.

    To use a Simulink.ImageType data type for your images, select Configure library to use Simulink.ImageType signals and configure parameters Default Color Format of Simulink.ImageType signal and Default Array layout of Simulink.ImageType signal 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 app 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 InputArray, OutputArray, and InputOutputArray data types.

  • Requires Microsoft® Visual Studio® 2015 or later Professional and Community editions for Windows 64 operating system. For more information on compilers, see Portable C Code Generation for Functions That Use OpenCV Library.

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

  • CV_type signals are not supported across referenced SIL model boundaries.

See Also

|

Related Topics