File Exchange

image thumbnail

OpenCV And MEX Files quick guide

version (280 KB) by Rachel Esman
How to use openCV library in MATLAB


Updated 21 Oct 2008

No License

This document was created as part of a final project for a BSc degree at the Academic College of Tel-Aviv Yaffo, by Rachely Esman and Yoad Snapir, under the supervision of Tal Hassner.
As part of the project, we used Intel's OpenCV library, calling its functions from MATLAB. We found the subject tricky and thus decided to share the experience of our work with others describing the common pitfalls.
You can use this document freely according to the copyrights noted below but under your sole responsibility.

Comments and Ratings (12)


rana (view profile)

I am using opencv 245 but i can't find the library cv245.lib



Adding _SECURE_SCL=1 to COMPFLAGS in mexopts.bat file solved this problem for me.

I am using Matalb2012b-64bit, VS2010-Professional, and OpenCV 2.4.5.

I am getting this error, would you please let me know what I need to do? I am using matlab 2010a and visual studio 2008, OpenCV 2.4.2

mex OpenCVTest2.cpp
OpenCVTest2.cpp(2) : fatal error C1083: Cannot open include file: 'cv.hpp': No such file or directory

C:\PROGRA~2\MATLAB\R2010A\BIN\MEX.PL: Error: Compile of 'OpenCVTest2.cpp' failed.

??? Error using ==> mex at 222
Unable to complete successfully.


Aakash (view profile)


Zhou (view profile)

First I copied your method without any modification but I failed. Then I found my directories are different from yours. I used Visual Studio 8.0 as the compiler and I built a debug and release version respectively. Then the .lib files should be in OPENCVDIR\VS2005\lib\debug,OPENCVDIR\VS2005\lib\release,OPENCVDIR\VS2005\bin\debug,OPENCVDIR\VS2005\bin\release respectively.
(Btw, the VS2005 directory is not there at first place, but was built when I used CMake to make the files, containing the built .sln and other files. You can name it differently, but the you should refer to compiled .libs.)
Also the .h files are in different places, possibly because I use a different version of OpenCV(I use 2.0). In this case it's in OPENCVDIR\include\opencv.

I still thank the author for making the principle right. All you have to pay attetion to is the concrete path names.


Art (view profile)

I have read this paper and try it. I use Matlab 2010, OpenCV1.1 and Visual Studio 2008. There is a big trouble: when I call mex-function, that contains a call of OpenCV functions ( such as cvNamedWindow, cvLoadImage and cvDestroyAllWindows, cvReleaseImage, etc. ) it's look all right, but there is a fail later, when I call matlab functions of Calibration Toolbox for Matlab. For example, I call one mex-function and after that the toolbox loads images and crush comes, but crush comes not at the momemnt of mex-function calling, but much later. In fact Matlab crushes on that code:
mask = exp(-((-wintx:wintx)'/(wintx)).^2) * exp(-((-winty:winty)/(winty)).^2);
In other words, it looks like crush happens, when there were computing intensive operations =)
It looks very strange that during the time after a calling mex-function and before crush, windows are creating and some matlab code works fine.
Could anybody help me solve this problem? Thanks

Joaquim Luis

It was me who removed all contribs from FEX.
I got too pissed off with TMW. See

Anyway, cvlib_mex as well as my other files are now available at

Hi, I was looking for the wrapper lib cvlib_mex, but I cannot find a working link to download it!

I would like to test it under Linux and the newest Matlab version...


Joaquim Luis

You would have saved yourself s a lot of work if you had searched the FEX and found "cvlib_mex" wich, BTW, uses only C (not C++).



MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: Image Aquisition

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor