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.


  1. type in Matlab:

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


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


on Windows, Matlab OpenCV examples are under something like:


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:


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


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.