Monday, 31 March 2014

Compiling OpenCV-3.0 with Matlab Support

A big uppercase HELLO to everyone!  I am back and after a long time (yet again) I am going to write a tutorial. The thing I am able to achieve here is awesome for us computer vision researchers. Yes! you heard it correct, exciting stuff.

I have been using OpenCV for quite sometime now. As good as it is for real-time computer vision applications, it can also be time consuming when it comes to exploring and implementing new research designs. Matlab on the other hand has always been flexible and a quick work around to achieve my research goals. The only problem, though, with matlab is that it is not real-time or even worse is that if you plan to implement code in OpenCV for real-time application, you would have to write the algorithms all over again as the usage of Matlab toolboxes is different than using the same methods in OpenCV.

Now comes the fun part, what if you can access OpenCV function calls within Matlab code? What if you can have easily transferable code from Matlab to C++?  This is all possible now with the OpenCV 3.0 Dev including matlab mex wrappers, which really is a good big step in the right direction. So lets start compiling the code.

What you will need?

1. A copy of latest OpenCV 3.0 Dev source code from:
2. Latest CMake binaries: cmake- (current version)
3. A copy of Matlab (32 bit or 64 bit)
4. Latest Python 2.7.6 Binaries (architecture should be same as Matlab -> 32 bit or 64 bit): python-2.7.6.msi
5. Microsoft Visual Studio (32 bit or 64 bit)

Additional requirements

- Patience 
- Lots of Patience


- A cup of coffee or tea (hahaha)

Some words on architecture

- In order to avoid all possible errors, it is extremely important that the architecture of all the libraries and compiler you use  is consistent. For this particular tutorial, make sure that the version of Matlab, Python and Microsoft Visual Studio has the same architecture (i.e. all of them should either be 32 bit or 64 bit). I have compiled OpenCV using 64bit architecture (as my Matlab was 64 bit). Essentially the same tutorial might work with 32bit versions.

Ok, I have these. What to do next?

The process which follows is a typical code compilation process using CMAKE to generate the source and Visual C++ to compile the binaries.

Generating the Code using CMAKE

First extract the OpenCV source to a folder, this folder will be used during compiling step. Next install both CMAKE and Python and make sure that you have a copy of Matlab and Visual studio installed. Also, as mentioned above, make sure that all the libraries and compilers used have the same architecture.

Open CMAKE gui, click browse source. Select the folder containing the above extracted OpenCV source code. Repeat the same process to specify build folder (using browse build option). For build version you can specify a new folder with a convenient location.

Click Configure to specify the compilers for compiling this source code.

Select the compiler as Visual Studio 10 or Visual Studio 10 Win64 (in case you are using 64 bit binaries). Select use default native compilers and click Finish.

At this point CMAKE will check your system configurations, compilers and will start generating a code for you in the build directory. This might take a couple of minutes to complete, depending on your system specifications.

On completion of this step, you will see some values marked red and there will be some diagnostic output in the lower part of the gui.

At this stage make sure that both Python and Matlab binaries are detected (as highlighted above). Also, if you scroll through the diagnostic output you should be able to spot the following lines:

Found PythonInterp: C:/Python27/python.exe (found suitable version "2.7.6", minimum required is "2.7")
Found PythonLibs: C:/Python27/libs/python27.lib (found suitable exact version "2.7.6")
Found JNI: C:/Program Files/Java/jdk1.7.0_17/lib/jawt.lib
Found Matlab: C:/Program Files/MATLAB/R2012b/bin/mex.bat
VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or set $VTK_DIR enviroment variable to VTK install subdirectory with VTKConfig.cmake file (for windows)
Warning: Matlab bindings will only be built in Release configurations
Warning: Matlab bindings will only be built in Release configurations
Trying to generate Matlab code
Trying to generate Matlab code - OK
Trying to compile mex file
Trying to compile mex file - OK

If everything is OK and similar to the output above, you can proceed by clicking on Configure to get rid of all the RED values. You may repeat this several times before you get all the values marked WHITE.

Once all the values are updated and marked WHITE click on Generate to generate the code. After the code is generated, you can close CMAKE.

Next stop: Compile Code

To compile the code, browse to the folder where you generated the build code. You will be able to find a *.sln extension project file named OpenCV.sln. Double click on this file to open it in Visual Studio.

In Visual Studio, select Release as build version from top drop-down menu.

Next start compiling the code by selecting Build-> Build ALL_BUILD from the build menu on the top. Remember in the start of this post I mentioned that you might need coffee/tea and a lot of patience? It was for this moment. So sit back and relax and let the Visual Studio do its work. 

After the compiler finishes compiling the code, use the solution explorer to find INSTALL option. Select it by clicking on it once and then again select Build -> Build INSTALL from the build menu on the top. Once completed successfully you would have compiled and installed OpenCV binaries (including Matlab Mex wrappers) in a folder named install inside the build folder you specified in CMAKE. For me these were found in: 

If you have managed to come to this point with no errors, then congratulations!! You have done everything right! Now you have the OpenCV code compiled and ready to use within Matlab.

Wohooo! My first Matlab-OpenCV code

Before you can use all the OpenCV awesomeness in Matlab, you need to set your system Environment Variables to point at OpenCV Binaries. To do this follow the instructions in my previous post to add the path to bin folder to the Environment Variables. For my above examples this path is: 


Open Matlab. Select Set Path and Add with Subfolders and browse to the directory named matlab inside the install folder. For the above example this folder is at: 

"Hello OpenCV!", said Matlab.

To check if everything is working fine, try the simple example below.
close all
clear all

% testing opencv basic funcionality

inImage = cv.imread('m.jpg');
cv.imshow('showImg', inImage);
Make sure you have the corresponding image in the same directory where your *.m file is located. 
If everything works fine (fingers crossed!) you will be able to see the image in a HighGUI window.

ARGHHH!! I cant get rid of some compilation errors

If you are unable to compile the code because of some stupid errors that you cant understand then this section is for you. I am uploading the binaries I compiled, which might work for you.

(Just a reminder) Ideally these should be compiled using the compilers you have and on your system so everything is set up accordingly. So these might not work for everyone out there, but still if you have a set up which comes close to the one mentioned below, you might be able to use these without any trouble.

The tools I have used to compile these:
-Matlab 2012b (64 bit)
-Microsoft Visual Studio 2010 (64 bit)
-Python 2.7.6 (64 bit)

The pre-compiled binaries (including the mex wrappers) can be downloaded from here
Setting up and using these libraries is similar to the instructions in the tutorial above.

Thats it for now! I will be back (hopefully soon) with more stuff. 


  1. i got this error after following your tutorial.

    Invalid MEX-file 'C:\opencv-master\opencv-master_build_3\install\matlab\+cv\imshow.mexw64': The specified module could not be found

    what could be the problem

    1. if everything else if going fine and you have compiled the libraries successfully but you can not find the mex files, then the problem is that you are compiling debug version for binaries.

      Please make sure that you select and compile release version in visual studio.

  2. This comment has been removed by the author.

  3. Hi,
    I followed your instructions, had some minor issues with paths to python which I sorted. But still cannot make VS (2008 in my case) to generate mex files. I am sure Release is picked everywhere. It copies m files but mex are not created. Any magic settings under my VS can cause Release to behave as it was Debug?

    1. It looks like the matlab Mex compiler is somehow not working in your case.

      If only thing different is VS 2008 then you may want to try compiling with VS 2010.

      Another thing you can do is try using the pre-compiled opencv binaries that include mex files.
      These can be found here:

    2. Those binaries do not work on my machine unfortunately, no idea for what reason
      ??? Error using ==> cv.imread
      Invalid MEX-file 'path_\opencv3.0\install\matlab\+cv\imread.mexw64':
      The specified module could not be found.

      I installed VS 2010, needed to get Win SDK 7.1, installed things in wrong order, reinstalled, CMAKE shows everything is ok but on compilation:
      Cannot open include file: 'ammintrin.h': No such file or directory then the
      What's more VS loads the project for ages and hangs during build and its even difficult to cancel it I kill it.

      It looks something is f....d up, I will have to change job to become gardener, begger or apply for social benefits

    3. This process of compiling opencv with matlab is relatively new. This may be the reason why this is only in developer version of opencv, and not the stable release.

      It was really difficult for me to compile it with the above process, took a lot of time to figure out everything. I hope you find a solution.

      Otherwise i really do like the alternate career options towards the end of your comment.. haha.

  4. Hi. hope you will be fine. please tell me what is wrong with my installation process.
    1) I am getting popup error massage at configuration step. "Error in configuration process, project file may be invalid."
    2) My Matlab not configured while python configuration is all ok.
    3) In lower tab its show in red this massage..
    CMake Error at cmake/OpenCVGenConfig.cmake:84 (export):
    export called with target "opencv_world" which requires target "zlib" that is not in the export list.

  5. Ok everyone.
    I got it working, only cause I could get the latest Matlab (2014a) and VS 2010.
    Don't try it with VS 2008 + Matlab 2010a unless you are masochist. I did it before and failed long hours.

    Anyway all my experimental code seems to work so far. However I tried to run code from:
    and get assertion failure (see my comment there).

    Thank M Asad for help. I don't have to go begging :) well... not yet at least.

  6. Hello,

    Why I have the following error :

    Undefined function 'VideoCaptureBridge' for input arguments of type 'char'.

    Error in cv/VideoCapture (line 15)
    this.ptr_ = VideoCaptureBridge('new', varargin{:});

  7. Thank you for the tutorial.
    After playing for a while with my PC (update drivers, set environment variables) I can use your binaries!

    However when I try to do everything by myself from the start, my MATLAB R2014a cannot identity my cv class after "Select Set Path and Add with Subfolders". There was no compilation errors during compilation process in Visual Studio 2010 Ultimate before that. I don't have a clue why this happened :D

    1. just fix it, there are some incomplete lines that make current 'cv.m' invalid for MATLAB

  8. i followed your instruction but i am stuck with matlab part. CMake shows
    mex: C:/Program Files/MATLAB/R2013a/bin/mex.bat
    Compiler/generator: Not working (bindings will not be generated)

    i am using windows 7 32bit OS

    tools used are

    matlab 2013a (32bit)
    visual c++ prefessional 2010 sp1 (32bit)
    cmake 2.8 (32bit)
    python 2.7 (32bit)

    can you help me identify what might be the problem? thanks in advance.

    1. looks like a problem with you mex compiler. Try google search with "how to make mex compiler work on matlab 2013a."

      Alternatively you can try using the precompiled binaries towards the end of this tutorial.

  9. I have the same problem: Compiler/generator: Not working (bindings will not be generated).
    I am using Matlab 2013a(64), VS12(64), cmake 3.0.1(32), and Python 2.7.8(64).
    I don't think it is related to problem with mex compiler as I can compile other libraries using mine.
    Also, should there be Matlab folder under Modules in OpenCV 3.0? currently there isn't. Has it been removed?

  10. I have had the same problem, now solved after reading in your post about the missing of the matlab folder. It is because it has been moved in the opencv_contrib site, i.e., in the /module folder, togheter with a lot of other contribs. After obtained the code, add it in cmake and select the desired modules. Everything works fine for a 64 bit configuration with on windows7, msvc2010, matlab2013a, python2.7, Qt5.0, vtk6.2 but Cvv, saliency, line_descriptor, surface_matching, ximgproc modules didn't compile on my machine.

  11. Hi all , where is the OpenCV 3.0 Dev source code , is it the master code from ?

    1. Hi,

      It has been moved to this folder: . Follow the instructions on the page to setup.


  12. Hi
    We have been doing the same setup ...But we are not able to detect PYTHON and MATLAB in the configure step.
    Do help us with it.A

    1. that is because the Matlab source has been moved to:

      Please follow the intstruction on the page to setup.

  13. Hi,
    I have a trouble that "*Bridge" funciton file cannot found, just like as the comment written by Mgmdo Mahmood, 12 May 2014 at 07:24.

    I'm using Win7Pro64bit, VS2010, Matlab2012b, and I tried to compile opencv_3.0.0_beta + matlab bindings (downloaded from opencv_contrib).
    Thanks to the instruction in this entry, I compiled them successfully and the test code using cv.imread and cv.imshow completely works.
    But when I use the m-files that call ***Bridge (for example, builds\install\matlab\+cv\ORB.m calls ORBBridge), the "Undefined function" error occured.
    The same problem occurred in the pre-compiled binaries uploaded in this entry.

    I searched the ORBBridge.mexw64 but I can find only the source file (ex. ORBBridge.cpp).
    The source file is seemed to be located in builds\modules\matlab\src\private, but not to be compiled.

    Could you help me?

  14. Could you give an example of a call to an opencv method that returns a complex structure and not an image, e.g. a feature detector.?

  15. There have been changes in the file layout of OpenCV since the first posting of this blog. As M notes above, the Matlab wrapper module has been moved to a "contrib" section which is not in the official OpenCV release. In addition, the script in this module depends on a now-deprecated The latter file used to be part of the OpenCV java module, but is no longer there. It can be found in search. Its future is not clear.

  16. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

  17. This comment has been removed by the author.

  18. Hi,
    I have tried this work around of compiling OpenCV with MATLAB. I downloaded latest opencv and opencv_contrib, compiled properly with Visual Studio (everything is fine with Cmake, python is found, Matlab is enabled. Apparently, no errors were found during the compilation of the solution. The folder "matlab" in the install folder is also generated. However the mex functions are not generated.
    I tried to use the basic function calls like cv.imread, cv.imshow, etc but the function call fails. I have checked and found that the mex functions are not generated in the matlab folder of the build.

    MATLAB Version: (R2015b) t
    Operating System: Microsoft Windows 10 Home Version 10.0 64 Bit
    Visual Studio: Visual Studio 2017 with the C++ compiler
    Mex compiler: mingw64 gcc
    Python: Python 3.6 64 Bit
    Java Version: Java 1.7.0_60-b19

    Please let me know if there is any resolution for this or at least provide the steps to reproduce.

    P.S: I also tried with versions mentioned in the steps above and added the matlab folder with mex files from git but it was not helpful.