11

Whenever I try to use the OpenCV Stitcher class in iOS and I include the stitcher-header ( #include ) I end up with a compile error "Expected '{'" in exposure_compensate.hpp. Apparently the line enum { NO, GAIN, GAIN_BLOCKS }; is causing some sort of error.

I am very new to openCV but using other functions like filter2d() work as expected. How can I resolve this?

1
  • how did u fix that Please Commented Feb 26, 2015 at 8:41

6 Answers 6

13

Try

#import <opencv2/opencv.hpp>

then

#import <UIKit/UIKit.h>

Update: this answer only highlights the bare minimum fix of the problem, and perhaps the root cause: order of dependencies. Please refer to other answers for better code / setup that you put in your project.

2
  • This saved me after hours of debugging. Commented Aug 25, 2017 at 6:31
  • For a more robust solution, you should definitely use a PCH file as explained in the other answers. Commented Oct 15, 2020 at 9:29
8

In your project, create a Prefix Header, MyProject.pch, and set it in your project's build settings.

Then within that pch file, do something like this:

#ifdef __cplusplus
#   include <opencv2/opencv.hpp>
#   include <opencv2/stitching/detail/blenders.hpp>
#   include <opencv2/stitching/detail/exposure_compensate.hpp>
#else
#   import <Foundation/Foundation.h>
#   import <UIKit/UIKit.h>
#   import <Availability.h>
#endif
0
3

I also ran into this problem. As G. Führ ensure you include the opencv headers first. The easiest way to do this is add:

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

near the top of the apps "Appname-Prefix.pch" header. This is a precompiled header and makes it easy to guarantee that your opencv header will be included before any of the apple headers.

//
//  Prefix header
//
//  The contents of this file are implicitly included at the beginning of every source file.
//

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

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

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif

This means you won't accidentally include an apple header before this anywhere else in the app.

1
  • this is the exact answer. <This means you won't accidentally include an apple header before this anywhere else in the app.>
    – tauheed
    Commented Sep 14, 2020 at 19:20
3

I had a similar issue and I solved editing directly the opencv-framework files involved (in your case compensate.hpp) and comment from them the definition of the NO enum case definition. In this case the file was blender.hpp, but compensate.hpp has the same structure

Hope this helps

1
  • This is kinda awful 😆 but for my scenario, where OpenCV was included via our library and none of the suggestions worked for the app using it, it saved a lot of time getting a demo up and running. As this is an internal spike I can live with it for now.
    – alex bird
    Commented Feb 14, 2022 at 15:21
2

I solved this issue by importing any Apple headers before those of OpenCV, as mentioned in the beginning of the header:

#if defined(NO)
#  warning Detected Apple 'NO' macro definition, it can cause build conflicts. Please, include this header before any Apple headers.
#endif

Hope that helps.

1
  • Did you mean importing Apple headers AFTER opencv headers? It's include this header before any Apple headers, and I tried #import <opencv2/opencv.hpp> then #import <UIKit/UIKit.h> and it worked. Commented Jun 23, 2017 at 18:43
0

In my case, I create a vertical image stitching app with openCV, error show as screenshot below. It is from exposure_compensate.hpp and blenders.hpp. From the error description, the top file is ../CVWrapper.mm, which is in my project instead of openCV pod project.

As above guys said, there is some conflict issue between C++ and Apple MACRO. And we should put C++ header above Apple header.

First, I try a workaround from internet, which said "replace NO with NO_EXPOSURE_COMPENSATOR = 0". This worked, but it modified openCV source code, I don't want to do that because I will not do version control on Pod files, then if other guys clone my repo/project, they will need to do the same modification on those source code.

Then, I follow the error message in Xcode, I did following changes in my CVWrapper.mm file. After that, those two error disappear.

// Before change
#import "CVWrapper.h"
#import "UIImage+OpenCV.h"
#import "stitching.h"
#import "UIImage+Rotate.h"

// After change
#import "stitching.h"
#import "CVWrapper.h"
#import "UIImage+OpenCV.h"
#import "UIImage+Rotate.h"

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.