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.
To install the support package, use one of the following methods:
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.
visionSupportPackagesin 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:
The OpenCV Importer app opens to a Welcome page.
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
.mfile format. To browse a saved file from previous import, select Load a file saved from a previous import. Click Next.
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
.cppfile 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
.hppfile 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.
To find functions and types that are supported for import, analyze your library by clicking Next. Once the analysis is complete, click Next.
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.
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
Outputoption in the I/O Type column drop-down list, and then click Next.
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
Matdata 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.ImageTypedata 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.
A Simulink library
Projectname_Lib.slxis 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.
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,
Does not support
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_typesignals are not supported across referenced SIL model boundaries.