开发者

OpenCV, Matlab and STL containers

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 withi开发者_高级运维n 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?


I fought with this very problem in the last two days. The problem is this:
libmex.dll (and a whole Matlab) uses Microsoft.VC80.CRT (version=8.0.50727.4053)
But your OpenCV uses Microsoft.VC90.CRT (version=9.0.21022.8)

So you can use the previous version of VC (VS 2005 with SP1 as far as I know), or as a workaround, you can use gcc (MINGW) (in this case they use totally different stl, so they can't interfere).
I did the latter and it works, and it will work with the next versions of Matlab (hopefully).


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.


I had a similar problem in the past few days, and was able to resolve the issue with some help from the friendly folks at MathWorks.

From the original post at http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll-segmentation-error :

You are probably seeing an incompatibility between the stl library and or compiler options used by your pre-compiled dll and those used by MATLAB and the MEX command. MATLAB 2009b was built with MSVC 2005.

You may be able to fix the problem by changing the options used by mex or by building your mex file directly with MSVC. One example of an option that may effect things is SECURE_SCL=0. I would start by building your test program with the options MATLAB is using to find the problematic option then try removing that option when building the mex file.

Because of this sort of incompatibility use of stl objects in the api's of third party compiled libraries is usually a bad idea.

Following his advice, I removed the SECURE_SCL=0 option from the mex options file at

C:\Users\ThePit\AppData\Roaming\MathWorks\MATLAB\R2009b\mexopts.bat

Then recompiled the mex file, now everything works like a charm - the function is returning the correct data and segmentation error no longer occurs.


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: How does the C++ STL vector template store its objects in the Visual Studio compiler implementation?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜