Matlab: installing the 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.

Install C++/CUDA OpenCV API in Matlab by typing in Matlab:


Note that despite the warnings about g++ version mismatch, the examples worked fine from g++-6. You can easily switch compiler versions.

Matlab computer vision example directory

This directory contains Computer Vision Toolbox examples from the Mathworks.


Foreground Detector example Matlab

cd ~/Documents/MATLAB/SupportPackages/R2017a/opencvinterface/toolbox/vision/supportpackages/visionopencv/example/ForegroundDetector

mexOpenCV backgroundSubtractorOCV.cpp


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

ORB matching feature using CUDA GPU and OpenCV from Matlab

You can use the ORB example if you don’t have a GPU


mexOpenCV detectORBFeaturesOCV_GPU.cpp -lmwgpu -lmwocvgpumex -largeArrayDims


You will see the rotated cameraman matched with the keypoints from the original cameraman.

use OpenCV from Python instead

I’ll still be using Python/OpenCV without Matlab, or even C++/OpenCV, which looks easier than using MEX with Matlab. I am glad Mathworks has taken this step nonetheless.

alternative: mexopencv

The mexopencv package is user-friendly–you use it much like any other Matlab toolbox, with regular Matlab code. No need to code in C++. It also adds OpenCV to GNU Octave.

Leave a Comment