OpenCV、Matlab 和 STL 容器

发布于 2024-09-12 04:09:02 字数 471 浏览 4 评论 0 原文

最新版本的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失而复得 2024-09-19 04:09:02

在过去的两天里,我一直在与这个问题作斗争。问题是这样的:
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).

已下线请稍等 2024-09-19 04:09:02

很久以前,我在使用 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.

谁许谁一生繁华 2024-09-19 04:09:02

过去几天我遇到了类似的问题,并且在 MathWorks 友好人员的帮助下解决了该问题。

来自原始帖子 http://www.mathworks.com/matlabcentral/answers/9294-mex-dynamic-memory-management-issue-with-std-vector-in-linked-external-dll -分段错误

您可能会发现预编译 dll 使用的 stl 库和/或编译器选项与 MATLAB 和 MEX 命令使用的编译器选项不兼容。 MATLAB 2009b 是使用 MSVC 2005 构建的。

您可以通过更改 mex 使用的选项或直接使用 MSVC 构建 mex 文件来解决该问题。可能会产生影响的选项的一个示例是 SECURE_SCL=0。我首先使用 MATLAB 所使用的选项构建测试程序,以查找有问题的选项,然后尝试在构建 mex 文件时删除该选项。

由于这种不兼容性,在第三方编译库的 api 中使用 stl 对象通常是一个坏主意。

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 :

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.

我一直都在从未离去 2024-09-19 04:09:02

向量中的数据仍应存储为单个连续块

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

应该为您提供指向数据的“c”样式指针,尝试将它们传递给 matlab

另请参阅:C++ STL 矢量模板如何在 Visual Studio 编译器实现中存储其对象?< /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: How does the C++ STL vector template store its objects in the Visual Studio compiler implementation?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文