2

There's an article describing how to do this here, that seems to have worked for other people but it does not compile for me.

Here's a copy of the .h file that was used:

//
//  NSImage+OpenCV.h
//

#import <AppKit/AppKit.h>

@interface NSImage (NSImage_OpenCV) {

}

+(NSImage*)imageWithCVMat:(const cv::Mat&)cvMat;
-(id)initWithCVMat:(const cv::Mat&)cvMat;

@property(nonatomic, readonly) cv::Mat CVMat;
@property(nonatomic, readonly) cv::Mat CVGrayscaleMat;

@end

I'm on Xcode 4.4, using openCV 2.4.2. The compiler errors I'm getting for the header file are 4x of the following:

Semantic issue: Use of undeclared identifier 'cv'

This error seems rather obvious...the header file does not define what a cv::Mat is. So I took a guess from looking at the OpenCV 2.4 tutorial, that I needed to add

#include <opencv2/core/core.hpp>

This generated 20 other errors, where the compiler was complaining about the core.hpp file. The first of which says:

Semantic issue: Non-const static data member must be initialized out of line

So my question is what am I doing wrong? How do I get this code to work?

Community
  • 1
  • 1
wrjohns
  • 484
  • 4
  • 14

2 Answers2

9

Another stackoverflow Q&A (link: How to include OpenCV in Cocoa Application?) had the missing piece on the undefined symbols.

To summarize:

  1. The OpenCV headers have to be included before the Cocoa headers, due to a macro conflict.
  2. Xcode automatically includes the cocoa headers at the top of every objective-C file (*.m, *.mm), which prevents you from adding the OpenCV headers first, as stated in point 1.
  3. Xcode has a "-Prefix.pch" file that defines any code which is automatically prepended to any code.

Normally, it looks like:

#ifdef __OBJC__
    #import <Cocoa/Cocoa.h>
#endif

Instead, the file should be changed to look like:

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

#ifdef __OBJC__
    #import <Cocoa/Cocoa.h>
#endif

So in the case of any *.cpp file, the opencv header gets added automatically. In the case of any *.m file, the Cocoa headers are added. And in the special case of *.mm files, they BOTH get added, and in the correct order.

Kudos to Ian Charnas and user671435 for figuring this out.

Community
  • 1
  • 1
wrjohns
  • 484
  • 4
  • 14
  • Your answer helped me to resolve most of the errors I got in xcode, but after I did what you described above, I still get one error in lsh_table.h "Semantic Issue: Use of undeclared identifier 'use_speed_'". Do you know how do I get rid of it? Thanks! – 0pcl Sep 22 '12 at 03:01
0

That is C++ syntax. You have to give your source file the .mm suffix to make it Objective-C++.

SSteve
  • 10,550
  • 5
  • 46
  • 72
  • I've already done this. The compiler recognizes the syntax, but not the symbols "cv" and "Mat", which are undefined. – wrjohns Jul 28 '12 at 13:06