最新版本的 OpenCV 中的许多功能都需要使用 STL 容器。我在尝试在 Matlab MEX 文件中使用它们时遇到了问题。我正在 Matlab 中编译 MEX 文件。 OpenCV 和 Matlab 都使用“/MD”标志(即“多线程 DLL”)来生成代码。
编译器:MSVC++9.0
MATLAB 2010a
SVN 最新的 OpenCV,我认为是 2.11。
我使用的代码非常简单:
vector<KeyPoint> keypoints_vec;
SurfFeatureDetector surf;
surf.detect(cvImg,keypoints_vec);
可以编译,但在 Matlab MEX 文件中运行时会崩溃。崩溃发生在 OpenCV 中的 vector::resize 中。旧的界面(没有 STL 容器)工作正常,但已被弃用。如何在 Matlab 和 OpenCV 之间使用 STL 容器?
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?
发布评论
评论(4)
在过去的两天里,我一直在与这个问题作斗争。问题是这样的:
libmex.dll(和整个 Matlab)使用 Microsoft.VC80.CRT(版本=8.0.50727.4053)
但是你的OpenCV使用Microsoft.VC90.CRT(版本= 9.0.21022.8)
所以你可以使用以前版本的VC(据我所知VS 2005 with SP1),或者作为解决方法,你可以使用gcc(MINGW)(在这种情况下,他们使用完全不同的 stl,所以他们不能干扰)。
我做了后者并且它有效,并且它将与 Matlab 的下一个版本一起工作(希望如此)。
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).
很久以前,我在使用 Matlab 时遇到了问题<-> VS 互操作。这可能是一些 Microsoft Visual C++ 运行时库的差异。检查 matlab 需要什么运行时库以及您的 Visual Studio 具有什么版本。我记得使用 Depends 来获取我的程序的 dll 依赖项。崩溃后检查你的调用堆栈(通过附加你的 msdev 调试器)它可能会给你一些提示。
那是很久以前的事了,所以我只是提供一些我记得的暗示。
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.
过去几天我遇到了类似的问题,并且在 MathWorks 友好人员的帮助下解决了该问题。
来自原始帖子 http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll -分段错误:
mex 选项文件中删除了 SECURE_SCL=0 选项,
按照他的建议,我从
C:\Users\ThePit\AppData\Roaming\MathWorks\MATLAB\R2009b\mexopts.bat
的然后重新编译了 mex 文件,现在一切都像魅力一样工作 - 该函数返回正确的数据并且不再出现分段错误。
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 :
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.
向量中的数据仍应存储为单个连续块
应该为您提供指向数据的“c”样式指针,尝试将它们传递给 matlab
另请参阅:C++ STL 矢量模板如何在 Visual Studio 编译器实现中存储其对象?< /a>
The data in a vector should still be stored as a single contiguous block
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?