Home > basic, matlab, OpenCv > connecting OpenCv with Matlab; Basic

connecting OpenCv with Matlab; Basic

It took much of the time, but I was able to connect OpenCv with matlab.

Much thanks to the


I took the introductory program given in the http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html

Also help from

Writing C Functions in Matlab form Jason Laska http://cnx.org/content/m12348/latest/

And modified it to run through matlab.

I named it showImageMt.cpp

So the Matlab code for compiling it was

mex showImageMt.cpp

But for that you have setup the file ‘mexopts.bat‘ as described in the above pdf . You might not able to find it by searching the matlab folder. You can find the path by running the following command in the Matlab


The contents of the file could be see by type(fullfile(prefdir,’mexopts.bat’))

The C++ opencv code is following

// This is a simple, introductory OpenCV program. The program reads an
// image from a file, inverts it, and displays the result.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

#include "mex.h"

void justWork(IplImage *img){
int height,width,step,channels;
uchar *data;
int i,j,k;

// get the image data
height    = img->height;
width     = img->width;
step      = img->widthStep;
channels  = img->nChannels;
data      = (uchar *)img->imageData;
printf("Processing a %dx%d image with %d channels\n",height,width,channels);

// create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);

// invert the image
for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)

// show the image
cvShowImage("mainWin", img );

// wait for a key


void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){

if (nrhs != 0)
mexErrMsgTxt("One input required.");
else if (nlhs > 0)
mexErrMsgTxt("Too many output arguments");

IplImage* img = 0;

printf("Usage: main <image-file-name>\n\7");

char *name = "lena.jpg";
// load an image
printf("Could not load image file: %s\n",name);
// release the image
cvReleaseImage(&img );

Categories: basic, matlab, OpenCv Tags: , , ,
  1. prasad
    April 3, 2009 at 5:06 am

    thank you 🙂

    • mohsenam
      April 3, 2009 at 8:56 pm

      No problem 🙂

  2. zeynab
    June 14, 2009 at 4:53 am

    thank you:) could you give anothere code example for program like this:
    read data from matlab via mex and convert mxarray into IplImage*
    best wishes

  3. mohsenam
    June 14, 2009 at 7:43 am

    Sorry Zeynab, I am working on something so will not be able to post the solution soon.
    However, as you can see from the justWork() code, we can create an IplImage object and point it’s imageData pointer to any suitable data-array.
    So if you can first transfer an array b/w the matlab and C/c++ (for which you can find many tutorials and code snippets online ), then just setup height and width of the IplImage, allocate proper memory and copy the transfered array to the imageData.

    I hope it helps.
    I will try to post proper code in some days.
    Mohsen Ali

  4. zeynab
    June 15, 2009 at 5:19 am

    Thanks alot:)
    I’ll try to do what u said and wait for your post…
    God bless you

  5. Luis
    August 12, 2009 at 10:12 pm

    hi, how is the file mexopts.bat

  6. Luis
    August 14, 2009 at 7:05 pm

    thanks, i edit my mexopts but not compiles….add:


    set LIB=%OPENCVDIR%\lib;%OPENCVDIR%\bin

    set INCLUDE=%OPENCVDIR%\cxcore\include;%OPENCVDIR%\cv\include;%OPENCVDIR%\cvaux\include;%OPENCVDIR%\otherlibs\highgui

    set LINKFLAGS= -tmpdir “%OUTDIR%.” -dll “%MATLAB%\extern\lib\win32\lcc\%ENTRYPOINT%.def” -L”%MATLAB%\sys\lcc\lib” -libpath “%LIBLOC%” “%LIB_NAME%2.obj” cv.lib highgui.lib cvaux.lib cxcore.lib

    This is error:

    lcc preprocessor error: C:\DOCUME~1\PROFESOR\MISDOC~1\MATLAB\showImageMt.cpp:4 Could not find include file

    lcc preprocessor error: C:\DOCUME~1\PROFESOR\MISDOC~1\MATLAB\showImageMt.cpp:5 Could not find include file

    thanks for your help

    • mohsenam
      August 31, 2009 at 5:03 am

      It has been while I have revisited OpenCV, but your mexopts.bat looks fine (although in here the paths looked chopped off but they were fine in the edit mode).
      Ok lets check few things
      1) instead of giving C:\ARCHIV~1\OpenCV, give proper Folder Name.
      2) check if the files are accessible
      3) mexopts is saved correctly.
      If anything else came to my mind, I will post it.
      Thanks Luis, sorry for late reply.
      Mohsen Ali

      • dewa
        March 24, 2011 at 9:41 pm

        sorry i am new with matlab mex and opencv. I managed to compile the showImageMt.cpp to get testopencv.mexw64. How to call/run it from matlab? Thanks Dewa.

  7. dewa
    March 24, 2011 at 9:43 pm

    sorry I meant I got showImageMt.mexw64

    • mohsenam
      June 20, 2011 at 6:55 am

      Hi dewa I hope you were able to run it. To know more about how to run mex files google mex and matlab. You will much get detailed and better introductions to those than I can provide here.

      Mohsen Ali

  8. June 2, 2011 at 9:30 pm

    I having troubles editing the mexopts.bat as well… The error reads that cv.h could not be found – I believe the reason is because I downloaded the latest openCV-2.2.0 and here are alot of different folders, so I dont know how to change the mexopts. Can somebody send me the link to the old opencv library, for which the above pdf explanation works.. Or maybe If somebody already changed the mexopts.bat for opencv-2.2.0 to send me the example so I’ll know how to change myself..
    Tnx in advance, hope somebody replies

    • mohsenam
      June 20, 2011 at 7:07 am

      Hi Gaan Fok,
      I don’t have link to the old OpenCv, but please can you provide more information. For example are you unable to find the mexopts? or even after setting the mexopts you are unable to compile
      Mohsen Ali

  9. George
    October 16, 2011 at 6:26 pm

    Hi all,

    Nice post Mohsen Ali!

    I recently prepared a more detailed guide to connecting Matlab with OpenCV. Please find it at the following link



    • mohsenam
      January 4, 2012 at 6:13 pm

      Thanks George. your page is quite helpful. Please feel free to provide more links that could be helpful

  10. ZAFAR
    January 4, 2012 at 4:01 pm

    Hi mohsenan
    I try to compile your code following all the instruction you mentioned but get some error like
    fatal error LNK1181: cannot open input file ‘cv.lib’
    I am using opencv2.3.1 with VS2010 on Win7 64bit
    kindly provide me solution of this thanks in advance

  11. mohsenam
    January 4, 2012 at 6:07 pm

    you have to setup the library path so that it can access the cv.lib, did you setup the paths correctly?

    • ZAFAR
      January 4, 2012 at 8:13 pm

      I have done all the setup mentioned in the above pdf but still not working and got error like
      fatal error C1083: Cannot open include file: ‘cv.h’: No such file or directory

      • mohsenam
        January 8, 2012 at 9:31 am

        i can only say is that recheck the paths, there is one comment on the same post which gives link to another post by some other author, also describing how to connect. Please check if that helps. If it helps then please share that information with us also. Thanks

  12. Maik
    September 25, 2012 at 8:41 am

    Hello, thanks for your post. I managed to get opencv recognized but, as soon as i try to include any file (like cv.h) i get an errore from c++ “error C2371: ‘char16_t’: ridefinition” on yvals.h file of visual studio. It happens with all the versions of opencv since 2.1 to 2.4.
    When i try to compile a mex without inclusion of opencv everything is fine. thank you

    • mohsenam
      October 6, 2012 at 10:04 pm

      What is the Matlab version? and Visual Studio version?
      Did you checked another link mentioned in the comments?

  13. George
    November 18, 2012 at 5:25 pm

    Hi again,

    Excpept for my link, the following link may be helpful



    • mohsenam
      November 19, 2012 at 1:18 pm

      thanks George.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: