Use OpenCV in Xcode 4 for an Object Oriented Mac OS X Application

First of all you have to install OpenCV in Mac OS X. Then in Xcode create a new Mac OS X Application project of type “Command Line Tool”.

Set a “Product Name” and choose “Foundation” for “Type” option.

In the “Build Settings” tab (clicking on the projects blue icon on the left) add following values for those options:

Inside “Search Paths”:
Header Search Paths: /usr/local/include
Library Search Paths: /usr/local/lib
Inside “Linking”:
Other Linker Flags: -lopencv_core -lopencv_highgui -lopencv_imgproc

NOTE: You can add more libraries in “Other Linker Flags” to add more OpenCV functionality, for example:

-lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_contrib -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_ml -lopencv_objdetect -lopencv_video

If you want to also use the C functions available in OpenCV change inside “Build Options” the next parameter:

Compiler for C/C++/Objetive-C: LLVM GCC 4.2

Change the extension of “main.m” file to “main.mm“. This makes the code to be interpreted as Objetive-C++ and not only as Objetive-C.

Now (VERY IMPORTANT) inside the .pch file of the “Supporting Files” folder add BEFORE any other import the following code:

#ifdef __cplusplus
    #import "opencv2/opencv.hpp"
#endif

This is because OpenCV has a MIN macro defined that also exists in Apple framework, and if you don’t have the one in OpenCV you may have errors like “LLVM GCC 4.2 Error: Statement-expressions are allowed only inside functions” or “opencv2/core/core.hpp:433: error: statement-expressions are allowed only inside functions“. The import in surrounded by that “ifdef” so the files that don’t use Objetive-C++ code can maintain the “.m” extension and not be forced to change all them to “.mm“.

Fill “main.mm” with this example code, that is in fact the Object Oriented version code of the previous example in Use OpenCV on Xcode 4 for a Mac OS X Application:

//
// main.mm
// OpenCVOOTest
//
// Created by __MyName__ on 16/09/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import 
#import "opencv2/opencv.hpp"

using namespace cv;

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    Mat img(cv::Size(640, 480), CV_8UC3);
    
    circle(img, cv::Point(320, 240), 100, Scalar(255, 0, 0, 255), 5, 8, 0);
    
    namedWindow("OpenCV Window", CV_WINDOW_NORMAL);

    imshow("OpenCV Window", img);
    
    waitKey(0);
    
    [pool drain];
    return 0;
}

If you have this error while linking:

"Undefined symbols for architecture x86_64:"

Followed by multiple function names, you have to change the programs architecture to i386. Inside “Build Settings” of the project change the option “Arquitectures” to “32-bit Intel”. This by itself may not solve the problem, as OpenCV must be also compiled for a i386 32 bits architecture.

Sources: http://stackoverflow.com/questions/3810800/how-to-include-opencv-in-cocoa-application
http://computer-vision-talks.com/2011/03/opencv-build-script-with-xcode-4-support-is-here

Leave a comment ?

1 Comments.

  1. Thanks a lot, you saved my day. There seeems to be a lot of ‘How To’s around, but none of them worked for my system (opencv 2.4.2 with macports, XCode 4.5 on 10.7.4) I did not need to edit the .pch file (didnt’ find it anyway).
    Cheers

Leave a Comment