Use the OpenCV Interface files to integrate your OpenCV C++ code into MATLAB® and build MEX-files that call OpenCV functions.
After you install third-party support files, you can use the data with the Computer Vision Toolbox™ product. To install the Add-on support files, use one of the following methods:
Select Get Add-ons from the Add-ons drop-down menu from the MATLAB desktop. The Add-on files are in the “MathWorks Features” section.
Type visionSupportPackages in a MATLAB Command Window and follow the prompts.
Note
You must have write privileges for the installation folder.
When a new version of MATLAB software is released, repeat this process to check for updates. You can also check for updates between releases.
The Computer Vision Toolbox Interface for OpenCV in MATLAB support files are installed in the visionopencv folder.
To find the path to this folder, type the following
command:
fileparts(which('mexOpenCV')) visionopencv folder contain these files and folder.
| Files | Contents |
|---|---|
example folder | Template Matching, Foreground Detector, and Oriented FAST and
Rotated BRIEF (ORB) examples. Each subfolder in the example folder
contains a README.txt file with step-by-step
instructions. |
registry folder | Registration files. |
mexOpenCV.m file | Function to build MEX-files. Note GPU support for this function will be removed in a future release.
|
README.txt file | Help file. |
The mex function uses prebuilt OpenCV
libraries, which ship with the Computer Vision Toolbox product. Your compiler must be compatible with the one used to build the
libraries. The following compilers are used to build the OpenCV libraries for
MATLAB host:
| Operating System | Compatible Compiler |
|---|---|
| Windows® 64 bit | Microsoft® Visual Studio® 2015 or later |
| Linux® 64 bit | gcc-4.9.3 (g++) |
| Mac 64 bit | Xcode 6.2.0 (Clang++) |
This example creates a MEX-file from a wrapper C++ file and then tests the newly
created file. The example uses the OpenCV template matching algorithm wrapped in a C++
file, which is located in the example/TemplateMatching folder.
Change your current working folder to the
example/TemplateMatching
folder:
cd(fullfile(fileparts(which('mexOpenCV')),'example',filesep,'TemplateMatching'))Create the MEX-file from the source file:
mexOpenCV matchTemplateOCV.cpp
Run the test script, which uses the generated MEX-file:
testMatchTemplate
The mexOpenCV interface utility functions convert data between
OpenCV and MATLAB. These functions support CPP-linkage only. GPU support is available on
glnxa64, win64, and Mac platforms. The GPU-specific utility functions support CUDA
enabled NVIDIA GPU. See the Parallel Computing Toolbox™
System
Requirements and GPU Support by Release (Parallel Computing Toolbox). The GPU utility
functions require the Parallel Computing Toolbox software.
The Computer Vision Toolbox Interface for OpenCV in MATLAB API supports OpenCV version 4.2.0.
| Function | Description |
|---|---|
ocvCheckFeaturePointsStruct | Check that MATLAB struct represents feature points |
ocvStructToKeyPoints | Convert MATLAB feature points struct to OpenCV
|
ocvKeyPointsToStruct | Convert OpenCV |
ocvMxArrayToCvRect | Convert a MATLAB struct representing a rectangle to an OpenCV
|
ocvCvRectToMxArray | Convert OpenCV |
ocvCvBox2DToMxArray | Convert OpenCV |
ocvCvRectToBoundingBox_{DataType} | Convert |
ocvMxArrayToSize_{DataType} | Convert 2-element |
ocvMxArrayToImage_{DataType} | Convert column major |
ocvMxArrayToMat_{DataType} | Convert column major mxArray to row major
cv::Mat for generic matrix |
ocvMxArrayFromImage_{DataType} | Convert row major |
ocvMxArrayFromMat_{DataType} | Convert row major cv::Mat to column major
mxArray for
generic matrix. |
ocvMxArrayFromVector | Convert numeric |
ocvMxArrayFromPoints2f | Converts |
Note
The GPU Functions listed below will be removed in a future release.
| GPU Function | Description |
|---|---|
ocvMxGpuArrayToGpuMat_{DataType} | Create |
ocvMxGpuArrayFromGpuMat_{DataType} | Create |
Call the mxArray function with your source
file.
mexOpenCVyourfile.cpp
For help creating MEX files, at the MATLAB command prompt, type:
help mexOpenCV
Each example subfolder in the Computer Vision Toolbox Interface for OpenCV in MATLAB support package contains all the files you need to run the example. To run
an example, you must call the mexOpenCV function with one of the
supplied source files.