11
votes

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:

OpenCV Error: The function/feature is not implemented (OpenCV was built without SURF support)

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?
2

2 Answers

11
votes

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

OpenCV is available on Cocoapods. Add one line in your Podfile: pod 'OpenCV'. Pretty easy.

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

1
votes

openCV implementation class just add .m to .mm as implementation file.

#import <UIKit/UIKit.h>
#import <opencv2/opencv.hpp>

@interface ViewController : UIViewController

@end

enter image description here .mm file run in C++ compiler in iOS, so it not show error.