Use OpenCV2.1.0 with Matlab 2010a

Side Note: First draft on Mar 27 2011.

OpenCV and Matlab are often “competitors”, as a lot of image/video processing techniques are implemented in both of them. However, there’re times you want to combine the strength/functions of the two. And luckily Matlab provides C/C++ interface.

Below are the step-by-step configurations for setting up Matlab environment for calling OpenCV functions.

1. Download and Install OpenCV.

2. Set up C/C++ compiler for Matlab.

  • Start up matlab 2010a, enter “mex –setup”. The screen capture below shows this step in detail,


3. Edit the mexopts.bat file.

  • The file location can be found at the second last line of the screen capture.
  • Add “set OPENCVDIR=C:OpenCV2.1” at the beginning of the file just below the line “set VCINSTALLDIR=%VSINSTALLDIR%VC”.
  • Add “%OPENCVDIR%includeopencv;” at the begnning of the the “set INCLUDE=” line
  • Add “%OPENCVDIR%lib;%OPENCVDIR%bin;” at the beginning of the “set LIB=” line
  • Add “cv210.lib highgui210.lib cvaux210.lib cxcore210.lib” at the line “set LINKFLAGS=” at the position before “/nologo”.

4. Edit PATH environment variable

Append “C:OpenCV2.1lib” at the end of the PATH.


5. Restart Matlab and you can start using OpenCV from Matlab by using the Matlab C++ interface.