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, first click the Add-Ons drop-down
list on the MATLAB®
Home tab, and then select
In the Add-Ons Explorer window, find and click the Computer Vision Toolbox Interface for OpenCV in Simulink support package, and then click Install.
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 import wizard 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
format. To browse a saved file from previous import, select Load a
file saved from a previous import. Click
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.
.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
Include files: Header files path. Specify
.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 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
To select the input/output types, double-click the
Output option in the I/O
Type column drop-down list, and then click
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
100. You can change the default size based on your
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.
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 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.
CV_type signals are not supported across referenced SIL