This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Install and Use Computer Vision System Toolbox OpenCV Interface

Use the OpenCV Interface files to integrate your OpenCV C++ code into MATLAB® and build MEX-files that call OpenCV functions. The support package also contains graphics processing unit (GPU) support.


After you install third-party support files, you can use the data with the Computer Vision System Toolbox™ product. Use one of two ways to install the Add-on support files.

  • 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.


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.

Support Package Contents

The OpenCV Interface support files are installed in the visionopencv folder. To find the path to this folder, type the following command:

The visionopencv folder contain these files and folder.

example folderTemplate Matching, Foreground Detector, and Oriented FAST and Rotated BRIEF (ORB) examples, including a GPU version. Each subfolder in the example folder contains a README.txt file with step-by-step instructions.
registry folderRegistration files.
mexOpenCV.m fileFunction to build MEX-files.
README.txt fileHelp file.

The mex function uses prebuilt OpenCV libraries, which ship with the Computer Vision System 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 bitMicrosoft® Visual Studio® 2015 Professional
Linux® 64 bitgcc-4.9.3 (g++)
Mac 64 bitXcode 6.2.0 (Clang++)

Create MEX-File from OpenCV C++ file

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.

  1. Change your current working folder to the example/TemplateMatching folder:


  2. Create the MEX-file from the source file:

    mexOpenCV matchTemplateOCV.cpp

  3. Run the test script, which uses the generated MEX-file:


Use the OpenCV Interface C++ API

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 with compute capability 2.0 or higher. See the Parallel Computing Toolbox™ System Requirements, The GPU utility functions require the Parallel Computing Toolbox software.


Check that MATLAB struct represents feature points


Convert MATLAB feature points struct to OpenCV KeyPoint vector


Convert OpenCV KeyPoint vector to MATLAB struct


Convert a MATLAB struct representing a rectangle to an OpenCV CvRect


Convert OpenCV CvRect to a MATLAB struct


Convert OpenCV CvBox2D to a MATLAB struct


Convert vector<cv::Rect> to M-by-4 mxArray of bounding boxes


Convert 2-element mxArray to cv::Size


Convert column major mxArray to row major cv::Mat for image

ocvMxArrayToMat_{DataType}Convert column major mxArray to row major cv::Mat for generic matrix

Convert row major cv::Mat to column major mxArray for image

ocvMxArrayFromMat_{DataType}Convert row major cv::Mat to column major mxArray for generic matrix.

Convert numeric vectorT to mxArray


Converts vector<cv::Point2f> to mxArray

GPU FunctionDescription

Create cv::gpu::GpuMat from gpuArray


Create gpuArray from cv::gpu::GpuMat

Create Your Own OpenCV MEX-files

Call the mxArray function with your source file.

mexOpenCV yourfile.cpp

For help creating MEX files, at the MATLAB command prompt, type:

help mexOpenCV

Run OpenCV Examples

Each example subfolder in the OpenCV Interface 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.

 Run Template Matching Example

 Run Foreground Detector Example

 Run Oriented FAST and Rotated BRIEF (ORB) Detector Example

 Run Detect ORB Features (GPU Version) Example

See Also

| |

Related Topics