使用Xcode 5和Matlab R2013b在OS X 10.9中编译mexopencv [英] Compiling mexopencv in OS X 10.9 with Xcode 5 and Matlab R2013b

查看:63
本文介绍了使用Xcode 5和Matlab R2013b在OS X 10.9中编译mexopencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣在系统上使用 Kota Yamaguchi的mexopencv库以下规格:

I'm interested in using Kota Yamaguchi's mexopencv library on my system with the following specifications:

  • OS X 10.9(小牛)
  • Xcode 5
  • Matlab 2013b
  • 通过macports安装到/opt/local/include/{opencv,opencv2}和/opt/local/lib的OpenCV

我git使用mexopencv网站上的命令克隆了最新的OpenCV版本.这是2013年7月20日星期六05:18:37的版本084838d62a25fcb3eec9f610abf91b167bc6c2f5.

I git cloned the latest OpenCV revision using the command on the mexopencv web site; this is version 084838d62a25fcb3eec9f610abf91b167bc6c2f5 from Sat Jul 20 05:18:37 2013 -0700.

我运行了Matlab的mex -setup命令,然后实施了此解决方法从Mathworks中使用Xcode 5作为我的mex编译器.

I ran Matlab's mex -setup command and then implemented this workaround from Mathworks to use Xcode 5 as my mex compiler.

我使用Matlab命令setenv('PATH', [getenv('PATH') ':/opt/local/bin']);

I added macports' pkg-config command to the path with the Matlab command setenv('PATH', [getenv('PATH') ':/opt/local/bin']);

现在,运行mxopencv.make会产生以下链接器错误消息:

Now, running mxopencv.make yields the following linker error message:

Undefined symbols for architecture x86_64:
  "cv::merge(std::vector<cv::Mat, std::allocator<cv::Mat> > const&, cv::_OutputArray const&)", referenced from:
      MxArray::toMat(int, bool) const in libMxArray.a(MxArray.o)
  "cv::split(cv::Mat const&, std::vector<cv::Mat, std::allocator<cv::Mat> >&)", referenced from:
      MxArray::MxArray(cv::Mat const&, mxClassID, bool) in libMxArray.a(MxArray.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

    mex: link of ' "+cv/CamShift.mexmaci64"' failed.

make: *** [+cv/CamShift.mexmaci64] Error 255

我现在不知道该怎么办.可以在我的系统上构建mexopencv吗?

I'm not sure what to do at this point. Is it possible to build mexopencv on my system?

推荐答案

我终于弄清楚了我的问题,解决该问题导致了其他问题,最终我得以解决.因此,这是一个完整的分步过程,介绍了我如何使mexopencv在系统上运行(某些步骤将从我的原始问题中重复进行).

I finally figured out my problem, the solution of which led to other problems, which I eventually was able to solve. So here's a complete step-by-step procedure of what I did to get mexopencv working on my system (some steps will be repeated from my original question).

  1. http://www.cs.sunysb.edu/~kyamagu获取mexopencv /mexopencv/
  2. 实施 Mathworks解决方法以获取Matlab使用Xcode 5的mex编译器
  3. 在Matlab中运行mex -setup
  4. 按如下所示修改新创建的〜/.matlab/R2013b/mexopts.sh文件:

  1. Get mexopencv from http://www.cs.sunysb.edu/~kyamagu/mexopencv/
  2. Implement the Mathworks workaround to get the Matlab mex compiler working with Xcode 5
  3. Run mex -setup from within Matlab
  4. Modify the newly-created ~/.matlab/R2013b/mexopts.sh file as follows:

  1. 将"-lstdc ++"的所有引用替换为"-lc ++"
  2. 将以下参数添加到CXXFLAGS变量:-std = gnu ++ 11 -stdlib = libc ++".这两个步骤可确保您使用的是C ++ 11而不是C ++ 98(由于
  3. 更改行
  1. Replace all references of "-lstdc++" with "-lc++"
  2. Add the following arguments to the CXXFLAGS variable: "-std=gnu++11 -stdlib=libc++". These two steps ensure that you're using C++11 instead of C++98 (thanks to this stack overflow post).
  3. You should have already replaced all instances of the text "10.7" with "10.8" from an earlier step
  4. Change the line

MLIBS="-L$TMW_ROOT/bin/$Arch -lmx -lmex -lmat"

MLIBS="$TMW_ROOT/bin/$Arch/libmx.dylib $TMW_ROOT/bin/$Arch/libmex.dylib $TMW_ROOT/bin/$Arch/libmat.dylib"

这可确保编译器不会在$TMW_ROOT/bin/$Arch中搜索OpenCV库,在我的系统上,该库的评估结果为/Applications/MATLAB_R2013b.app/bin/maci64.无论出于何种原因,来自Matlab的旧版OpenCV(2.4.2)中的库都位于该文件夹中(也许它们与计算机视觉系统工具箱一起提供). [注意:可能无需执行此步骤]

This ensures that the compiler does not search for OpenCV libraries in $TMW_ROOT/bin/$Arch which, on my system, evaluates to /Applications/MATLAB_R2013b.app/bin/maci64. For whatever reason, libraries from an older version of OpenCV (2.4.2) come with Matlab and live in that folder (maybe they come with the Computer Vision System Toolbox). [Note: this step may not be necessary]

我通过简单的单线测试尝试了OpenCV的安装:

I tried out my installation of OpenCV with a simple one-liner test:

imshow(cv.Canny(rgb2gray(imread('peppers.png')), [10 100]))

当我尝试运行它时,出现以下错误消息:

When I attempted to run it at this point, I got the following error message:

>> imshow(cv.Canny(rgb2gray(imread('peppers.png')), [10 100]));
Error using cv.Canny
Invalid MEX-file '/Users/dgolden/software/cpp/mexopencv/+cv/Canny.mexmaci64': dlopen(/Users/dgolden/software/cpp/mexopencv/+cv/Canny.mexmaci64, 6): Library not loaded:
/opt/local/lib/libtiff.5.dylib
  Referenced from: /opt/local/lib/libopencv_highgui.2.4.dylib
  Reason: Incompatible library version: libopencv_highgui.2.4.dylib requires version 8.0.0 or later, but libtiff.5.dylib provides version 6.0.0

问题是Matlab在/Applications/MATLAB_R2013b.app/bin/maci64中包含了某些Macports安装的库的自己版本,与/opt/local/lib中的库不同.默认情况下,Matlab尝试动态链接其自身的库版本,而不是OpenCV期望的版本,因此该程序无法运行.

The problem is that Matlab has its own version of some macports-installed libraries, contained in /Applications/MATLAB_R2013b.app/bin/maci64, that are different from the ones in /opt/local/lib. By default, Matlab tries to dynamically link its own versions of the libraries, which are not the versions that OpenCV expects, so the program doesn't run.

该解决方案在mexopencv随附的README.markdown文件中提出.您需要告诉Matlab不要使用自己的共享库版本,而要使用/opt/local/lib中的版本.

The solution is suggested in the README.markdown file included with mexopencv. You need to tell Matlab not to use its own version of the shared libraries and to instead use the versions from /opt/local/lib.

您可以通过以下两种方法之一来执行此操作.首先,尝试运行您的程序,并记下产生错误的库的名称.然后,要么:

You can do this one of two ways. First, try to run your program and note the name of the library that yields an error. Then, either:

  1. 在/Applications/MATLAB_R2013b.app/bin/maci64中找到库文件,然后重命名或移动它.例如,将/Applications/MATLAB_R2013b.app/bin/maci64/libtiff.5.dylib重命名为/Applications/MATLAB_R2013b.app/bin/maci64/libtiff.5.dylib.bak.如果其他Matlab功能依赖于该库,则可能会产生意想不到的后果.您无需执行其他任何操作即可让OpenCV在/opt/local/lib中找到正确的库.
  2. 关闭Matlab并通过首先设置DYLD_INSERT_LIBRARIES环境变量来从命令行启动它,例如:

  1. Find the library file in /Applications/MATLAB_R2013b.app/bin/maci64 and rename or move it. E.g., rename /Applications/MATLAB_R2013b.app/bin/maci64/libtiff.5.dylib to /Applications/MATLAB_R2013b.app/bin/maci64/libtiff.5.dylib.bak. This might have unintended consequences if other Matlab functionality depends on that library. You shouldn't need to do anything else in order for OpenCV to find the correct library in /opt/local/lib.
  2. Close Matlab and start it from the command line by first setting the DYLD_INSERT_LIBRARIES environment variable, like:

DYLD_INSERT_LIBRARIES=/opt/local/lib/libtiff.5.dylib /Applications/MATLAB_R2013b.app/bin/matlab &

就我而言,在我解决了libtiff.5.dylib问题后,我也遇到了libfreetype.6.dylib问题,因此我将其添加到了DYLD_INSERT_LIBRARIES变量中,例如:

In my case, after I solved the problem with libtiff.5.dylib, I also had a problem with libfreetype.6.dylib, so I added that to the DYLD_INSERT_LIBRARIES variable, like:

DYLD_INSERT_LIBRARIES=/opt/local/lib/libtiff.5.dylib:/opt/local/lib/libfreetype.6.dylib /Applications/MATLAB_R2013b.app/bin/matlab &

然后尝试再次运行程序.如果遇到另一个库版本错误,请继续进行迭代,并从/Applications/MATLAB_R2013b.app/bin/maci64重命名/移动库,或者将正确的库路径添加到DYLD_INSERT_LIBRARIES变量.最终,它应该可以工作!

Then try to run your program again. If you get another library version error, keep iterating and either renaming/moving libraries from /Applications/MATLAB_R2013b.app/bin/maci64 or adding the correct library paths to the DYLD_INSERT_LIBRARIES variable. Eventually, it should work!

完成所有这些步骤后,我就能够成功运行Matlab命令:

After I followed all these steps, I was able to run my Matlab command successfully:

imshow(cv.Canny(rgb2gray(imread('peppers.png')), [10 100]))

让我知道这些步骤是否对您有用,以及我是否跳过了任何步骤或使事情变得比必须的复杂.

Let me know if these steps did or didn't work for you, and whether I skipped any steps or made anything more complicated than it had to be.

我希望这对某人有帮助!我花了几天的时间梳理互联网并打扰Kota终于找到正确的解决方案.

I hope this helps someone! I spent several days combing the Internet and bothering Kota to finally arrive at the correct solution.

这篇关于使用Xcode 5和Matlab R2013b在OS X 10.9中编译mexopencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆