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™ OpenCV Interface for 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 OpenCV Interface for 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 Code Generation, click the OpenCV Importer app icon. 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. 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, then 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 an absolute
path is provided, then the wizard uses the file from the
specified location. If the absolute path is not provided, then
the wizard uses the path relative to the project root.
Include files: Header files path. Specify
.hpp file format. If an absolute path is
provided, then the wizard uses the file from the specified
location. If the absolute path is not provided, then the wizard
uses the path relative to the project root.
Include paths: Define any additional OpenCV specific include folders. (Folder information). If you do not specify any include folders, then the default folder path is used.
Library paths: Specify the path to external library files.
Use a semicolon-separated list of files or folders if you have multiple files or folders to specify. Click Next.
To find functions and types that are supported for import, analyze your library by clicking Next. Once the analysis is done, click Next.
In the What to Import page, all the functions or subset of the functions 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. 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
Mat data type, the default
output size is (
720,1280,3) and the default underlying
uint8. For vectors, the default size is
A Simulink library
created from your OpenCV code into a project root folder. The library
contains the subsystem block and the C caller block.
The wizard also creates wrapper files for source and header files.
The Computer Vision Toolbox OpenCV Interface for Simulink support package:
Uses OpenCV shipped as part of the MATLAB third-party support. You can get the OpenCV additional capabilities in Add-Ons (Computer Vision Toolbox).
Does not support external OpenCV libraries (e.g.
Does not support
Requires Microsoft® Visual Studio® 2015 Professional or Microsoft Visual Studio 2017 for Windows 64 operating system. For more information on compilers, see Compiler used to build OpenCV libraries.