Home > OpenCv, basic, matlab > 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

http://www.mathworks.com/matlabcentral/fx_files/21818/1/OpenCV_And_MEX_Files_quick_guide.pdf

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

fullfile(prefdir,’mexopts.bat’)

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

The C++ opencv code is following

////////////////////////////////////////////////////////////////////////
//
//showImageMt.cpp
//
// 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++)
data[i*step+j*channels+k]=255-data[i*step+j*channels+k];

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

// wait for a key
cvWaitKey(0);

};

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;

/*if(argc<2){
printf("Usage: main <image-file-name>\n\7");
exit(0);
}*/

char *name = "lena.jpg";
// load an image
img=cvLoadImage(name);
if(!img){
printf("Could not load image file: %s\n",name);
exit(0);
}
justWork(img);
// release the image
cvReleaseImage(&img );
return;
}

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

    thank you :)

    • mohsenam
      April 3, 2009 at 8:56 pm | #2

      No problem :)

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

    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 | #4

    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.
    Bye
    Mohsen Ali

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

    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 | #6

    hi, how is the file mexopts.bat

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

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

    set OPENCVDIR=C:\ARCHIV~1\OpenCV

    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 | #9

      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

  1. No trackbacks yet.