本文介绍了openCV 2.4.3 iOS 框架编译器无法识别一些 C++ 头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

openCV 2.4.3/Xcode 4.5.2/mac osx 10.8.2

我正在尝试让 openCV 与 iOS 一起工作.我正在尝试使用来自 openCV.org 的预构建 2.4.3 框架.但是,我收到以下 xcode 项目构建错误,表明编译器不知道它正在处理 C++,例如

#include !'list' 文件未找到命名空间 cv !unknown type name 'namespace'

这似乎只涉及以下头文件:
opencv2/nonfree/features2d.hpp"
opencv2/nonfree/nonfree.hpp"
opencv2/video/video.hpp"

如果我不在 opencv.hpp(或其他任何地方)中包含这三个文件,我似乎可以编译和使用 openCV.问题是,我在试验 SURF 时确实需要非自由文件 - 最近已将其移至非自由.

这真的是一个双重问题(抱歉;-)

  • 如何让编译器相信这些是 C++ 头文件?
  • 使用 SURF 需要哪些标题?

更新

我已经克隆了 openCV git 存储库并从中构建了一个新框架.这种方法以前不起作用,但今天我意识到我没有使用当前版本的 CMAKE.我一直在使用 CMAKE 2.8.2,这将无法为 ios 构建 opencv.当前版本的 CMAKE 2.8.10 构建它没有任何问题(这是遵守文档的对象课程,它确实说需要 CMAKE min. v2.8.8).

现在,当我在 Xcode 项目中添加 opencv 框架的当前版本时,我可以包含 features2dnonfree 并顺利构建.唯一的问题仍然是一个标题:video/background_segm.hpp,它仍然产生:

#include !'list' 文件未找到

如果我注释行,我会在下一行出现错误:

namespace cv !unknown type name 'namespace'

很明显编译器不会将其识别为 C++ 头文件,即使它带有 .hpp 后缀.

opencv2/video/video.hpp 中,如果我删除

#include "opencv2/video/background_segm.hpp"

我也可以用 video.hpp 构建(虽然我猜它在实践中无法使用).

不幸的是,我仍然无法让 SURF 工作.当我运行该项目时,它因以下错误而崩溃:

OpenCV 错误:函数/特性没有实现(OpenCV 是在没有 SURF 支持的情况下构建的)

这是在 legacy/features2d.cpp 中触发的:

 Ptrsurf = Algorithm::create("Feature2D.SURF");如果(冲浪.空())CV_Error(CV_StsNotImplemented, "OpenCV 是在没有 SURF 支持的情况下构建的");

问题仍然存在...

  • 我如何说服编译器 background_segm.hpp 是一个合法的 C++ 头文件?
  • 如何启用 SURF 支持?

解决方案

我现在一切正常.在对 openCV.org 提供的预建 iOS 库不满意之后,这就是我所做的......

  • 从 gitHub 存储库的克隆中为 iOS 编译 openCV.运行 build_framework.py(在分发的 ios 文件夹中),指向您选择的输出目录.一定要有最新的 CMake 副本,否则你会像我一样被绊倒.

  • 您的输出文件夹将包含两个子文件夹,buildopencv2.framework.将后者拖入您的 Xcode 项目

project-Prefix.pch 文件中添加以下行

#ifdef __cplusplus#import #万一

(应该高于 #ifdef __OBJC__ 行)

这足以让大部分 openCV 工作.然而,避免objective-C++"(在与objective-C 相同的文件中混合你的c++ 代码)是一个很好的主意.为了管理这个,你创建了一个薄的包装器"对象(它将是 obj-C++)来在你的 obj-C 类和 C++ 代码之间进行调解.包装器本质上只有两个角色:转换数据格式(例如 UIImage cv::Mat),以及在 obj-C 方法和 C++ 函数调用之间进行转换.有关详细信息,请参阅我对此问题的回答(和一个 github 托管的示例项目)

要使 SURF(和 SIFT)工作需要几个额外的步骤,因为 SURF 是 由于许可问题而有些被弃用(它已移至不会自动加载的nonfree).

这些包含需要添加到您使用 SURF 的文件中

#include #include 

我正在处理的代码使用了 SURF 的 C 接口(例如 cvExtractSURF),所以我们还需要在调用这些函数之前添加这一行:

 cv::initModule_nonfree();

我的问题的另一部分,如何强制 Xcode 编译为 C++,有点牵强附会(我正在使用的 openCV 版本肯定存在兼容性问题) - 并且不再需要这个解决方案.然而,答案是首先,将您的 .m 文件重命名为 .mm(对于objective-C++)或.cpp(对于纯C++)……但是如果这不起作用,您可以通过更改'file输入'.

更新

您还需要注意在任何使用 openCV 框架的项目中正确设置 C++ 标准库.旧版本的 openCV (to.2.4.2) 需要 libstdc++,新的 (2.4.3+) 需要 libc++.详情请见:

https://stackoverflow.com/a/14186883/1375695

更新 2

openCV 现在与 cocoaPods 一起安装.引用 SebastienThiebaud

OpenCV 在 Cocoapods 上可用.在 Podfile 中添加一行:pod 'OpenCV'.很简单.

相当简单"......考虑到我们之前的所有麻烦,可能是对[去年]年度的轻描淡写......

openCV 2.4.3 / Xcode 4.5.2 / mac osx 10.8.2

I am trying to get openCV working with iOS. I am attempting to use the the prebuilt 2.4.3 framework from openCV.org. However I am getting the following xcode project build errors that suggest the compiler doesn't know it is dealing with c++, eg

#include <list>       !'list' file not found

namespace cv          !unknown type name 'namespace'

This only seems to concern the following header files:
"opencv2/nonfree/features2d.hpp"
"opencv2/nonfree/nonfree.hpp"
"opencv2/video/video.hpp"

if I don't include these three files in opencv.hpp (or anywhere else) I seem to be able to compile and use openCV ok. The trouble is, I do need the nonfree files as I am experimenting with SURF - which has been moved to nonfree recently.

This is really a twofold question (sorry ;-)

  • how do I convince the compiler that these are c++ headers?
  • which headers exactly do I need to use SURF?

update

I have cloned the openCV git repository and built a new framework from that. This approach had not worked previously, but today I realised that I was not using the current version of CMAKE. I had been using CMAKE 2.8.2 and this would fail to build opencv for ios. Current version CMAKE 2.8.10 builds it without any issues (that's an object lesson in obeying the docs, which do say CMAKE min. v2.8.8 is required).

Now when I add this current build of the opencv framework in an Xcode project I can include features2d and nonfree and build smoothly. The only problem remains with one header: video/background_segm.hpp, which still yields:

#include <list>       !'list' file not found

If I comment that line out I get an error on the next line:

namespace cv          !unknown type name 'namespace'

It seems clear that the compiler doesn't recognise this as a C++ header, even though it is suffixed .hpp.

In opencv2/video/video.hpp if I remove

#include "opencv2/video/background_segm.hpp"

I can build with video.hpp also (although I guess it would be unusable in practice).

Unfortunately I still can't get SURF to work. When I run the project it crashes with this error:

This is triggered in legacy/features2d.cpp:

   Ptr<Feature2D> surf = Algorithm::create<Feature2D>("Feature2D.SURF");
   if( surf.empty() )
        CV_Error(CV_StsNotImplemented, "OpenCV was built without SURF support");

The questions remain...

  • how do I convince the compiler that background_segm.hpp is a legit c++ header?
  • how do I enable SURF support?

解决方案

I have everything working now. After having no joy with the pre-built iOS library available from openCV.org this is what I did...

  • compile openCV for iOS from a clone of the gitHub repository. Run build_framework.py (in the ios folder of the distribution), pointing to an output directory of your choosing. Be sure to have an up-to-date copy of CMake or you will trip over like I did.

  • Your output folder will end up with two subfolders, build and opencv2.framework. Drag the latter into your Xcode project

Add the following line in the project-Prefix.pch file

#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif

(should go above the #ifdef __OBJC__ line)

That is sufficient to get most of openCV working. However it is a very good idea to avoid "objective-C++" (mixing your c++ code in the same files as your objective-C). To manage this you create a thin "wrapper" object (which will be obj-C++) to mediate between your obj-C classes and c++ code. The wrapper essentially has only two roles: to translate data formats (eg UIImage <-> cv::Mat), and to translate between obj-C methods and C++ function calls. See my answer to this question for details (and a github-hosted example project)

To get SURF (and SIFT) working requires a couple of additional steps, as SURF is somewhat deprecated due to licensing issues (it's been moved into nonfree which does not load automatically).

These includes need to be added in files where you are using SURF

#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/legacy/compat.hpp>

The code I am working with uses the C interfaces for SURF (eg cvExtractSURF), so we also need to add this line before calling these functions:

   cv::initModule_nonfree();

The other part of my question, how to force Xcode to compile as C++, was a bit of a red herring (there must have been something compatibility issue with the openCV build I was using) - and is no longer required for this solution. However the answer is first, to rename your .m files .mm (for objective-C++) or .cpp (for pure C++) … but if that doesn't work, you can force the issue in the file inspector by changing 'file type'.

update

You also need to take care that the C++ standard library is set correctly in any projects that use the openCV framework. Older versions of openCV (to.2.4.2) want libstdc++, newer (2.4.3+) expect libc++. Details here:

https://stackoverflow.com/a/14186883/1375695

update 2

openCV now installs with cocoaPods. To quote SebastienThiebaud

"Pretty easy" ... given all our previous hassles, could be the understatement of [last] year...

这篇关于openCV 2.4.3 iOS 框架编译器无法识别一些 C++ 头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 10:25