views:

184

answers:

2

Many functions in the latest release of OpenCV require the use of STL containers. I run into problems when trying to use them in a Matlab MEX file. I am compiling the MEX files from within Matlab. Both OpenCV and Matlab use the "/MD" flag which is "Multithreaded DLL" for code generation.

Compiler: MSVC++ 9.0 Matlab 2010a OpenCV latest from SVN, 2.11 I think.

The code I am using is very simple:

vector<KeyPoint> keypoints_vec;
SurfFeatureDetector surf;
surf.detect(cvImg,keypoints_vec);

This compiles but crashes when run in a Matlab MEX file. The crash is within OpenCV in vector::resize. The old interface (without STL containers) works fine but is deprecated. How can I use STL containers between Matlab and OpenCV?

+1  A: 

A long time ago I had problems with Matlab <-> VS interop. It might be some microsoft visual c++ runtime library discrepancy. Check what runtime lib is required by matlab and what version does your visual studio have. I remember using Depends to get the dll dependencies for my program. Check your call stack after crashing (by attaching your msdev debugger) it might give you some hints.

It was a long time ago so I'm just giving hints of what I remember.

code-gijoe
I haven't used Spy++ yet. I just made sure that the same compiler was used and that the same switches (/MD) was passed, which I though would be enough to have them use the same version.
Ben
Sorry I said Spy++ it's actually Depends (Dependency walker). Check this out:http://msdn.microsoft.com/en-us/library/ms235265(VS.80).aspx
code-gijoe
Check if it crashes inside a Matlab dll or inside a MS runtime dll (by attaching your debugger).
code-gijoe
It crashes inside opencv_features2d211.dll when OpenCV tries to call vector::resize.
Ben
Well, my best advice would be to compile OpenCV in debug so you can check why it's crashing. Have you tried giving the vector an initial size? Some big value.
code-gijoe
A: 

The data in a vector should still be stored as a single contiguous block

std::vector<int> data;
int *array = &data[0]; 
int *array = &data.front(); 

Should give you 'c' style pointers to the data, try passing these to matlab

see also: http://stackoverflow.com/questions/1062601/how-does-the-c-stl-vector-template-store-its-objects-in-the-visual-studio-compi

Martin Beckett
You misunderstood my problem. The problem is not passing data to Matlab. It is the fact that the above code crashes.
Ben