Install Matlab integrated OpenCV C++/CUDA/MEX support

To use OpenCV from Matlab as integrated by the Mathworks, you will need to write your OpenCV calls in C++ and/or CUDA, using MEX. This works on Linux, Mac and Windows.

Install

  1. type in Matlab:

    visionSupportPackages
    1. select “Computer Vision System Toolbox OpenCV Interface by MathWorks Computer Vision System Toolbox Team” and install.

    Note: the examples require particular compilers depending on Matlab version and operating system.

    Examples

    This directory contains Computer Vision Toolbox examples from the Mathworks. Find the Matlab OpenCV example directory, in Matlab:

    fileparts(which('mexOpenCV'))

on Windows, Matlab OpenCV examples are under something like:

C:\ProgramData\MATLAB\SupportPackages\R2019a\toolbox\vision\supportpackages\visionopencv\example

The examples below assume you’re starting from this directory. See the README.txt in each directory for compilation details. Some examples require a CUDA GPU.

Foreground Detector

Compile example

cd ForegroundDetector

mexOpenCV backgroundSubtractorOCV.cpp

If the example fails to compile due to compiler mismatch, follow the instructions given in the error message. For example, on Windows with Matlab R2019a, Visual Studio C++ 2015 compiler is required. This can be obtained by downloading the Visual Studio compiler, and adding under the Visual Studio installer options “Individual Components” → VC++2015 toolset for desktop. This consumes an additional 3 GB of hard drive space!

Run the OpenCV Matlab demo:

testBackgroundSubtractor

You will see a Video Player window pop up with cars driving by, with the cars detected outlined in white rectangles.

mexopencv

The mexopencv package is user-friendly. Use it much like any other Matlab toolbox, with regular Matlab code. No need to code in C++ as with the Mathworks Matlab OpenCV support built-in from the factory. It also adds OpenCV to GNU Octave.