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
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
I follow these steps but when I compile I obtained these errors:
/opt/local/include/opencv2/core/types_c.h
/opt/local/include/opencv2/core/types_c.h:742: ‘assert’ was not declared in this scope
/opt/local/include/opencv2/core/types_c.h:774: ‘assert’ was not declared in this scope
/opt/local/include/opencv2/core/types_c.h:791: ‘assert’ was not declared in this scope
/opt/local/include/opencv2/core/types_c.h:807: Implicit conversion shortens 64-bit value into a 32-bit value
/opt/local/include/opencv2/core/core_c.h
/opt/local/include/opencv2/core/core_c.h:1118: ‘assert’ was not declared in this scope
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:66: ‘::clock_t’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:68: ‘::tm’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:70: ‘::clock’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:71: ‘::difftime’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:72: ‘::mktime’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:73: ‘::time’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:74: ‘::asctime’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:75: ‘::ctime’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:76: ‘::gmtime’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:77: ‘::localtime’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/ctime:78: ‘::strftime’ has not been declared
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1839: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1846: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1854: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1861: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1873: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1880: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1883: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1895: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1900: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1908: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1912: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1932: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1968: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:1976: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:2210: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:2259: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/locale_facets.tcc:2288: Invalid use of incomplete type ‘struct tm’
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/wchar.h
/Volumes/Xcode/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/wchar.h:155: Forward declaration of ‘struct tm’
/opt/local/include/opencv2/core/operations.hpp
/opt/local/include/opencv2/core/operations.hpp:3446: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/core/operations.hpp:3446: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
/opt/local/include/opencv2/core/operations.hpp:223: ‘assert’ was not declared in this scope
/opt/local/include/opencv2/core/operations.hpp:216: ‘assert’ was not declared in this scope
/opt/local/include/opencv2/core/operations.hpp:230: ‘assert’ was not declared in this scope
/opt/local/include/opencv2/core/mat.hpp
/opt/local/include/opencv2/core/mat.hpp:772: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/core/mat.hpp:781: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/core/mat.hpp:792: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/legacy/legacy.hpp
/opt/local/include/opencv2/legacy/legacy.hpp:1464: ‘assert’ was not declared in this scope
/opt/local/include/opencv2/legacy/legacy.hpp:1478: ‘assert’ was not declared in this scope
/opt/local/include/opencv2/flann/nn_index.h
/opt/local/include/opencv2/flann/nn_index.h:75: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/nn_index.h:76: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/allocator.h
/opt/local/include/opencv2/flann/allocator.h:131: Implicit conversion shortens 64-bit value into a 32-bit value
/opt/local/include/opencv2/flann/allocator.h:142: Implicit conversion shortens 64-bit value into a 32-bit value
/opt/local/include/opencv2/flann/allocator.h:158: Implicit conversion shortens 64-bit value into a 32-bit value
/opt/local/include/opencv2/flann/kdtree_single_index.h
/opt/local/include/opencv2/flann/kdtree_single_index.h:217: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/kdtree_single_index.h:218: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/kmeans_index.h
/opt/local/include/opencv2/flann/kmeans_index.h:154: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/kmeans_index.h:209: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/hierarchical_clustering_index.h
/opt/local/include/opencv2/flann/hierarchical_clustering_index.h:155: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/hierarchical_clustering_index.h:210: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/lsh_table.h
/opt/local/include/opencv2/flann/lsh_table.h:159: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/lsh_table.h:240: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/lsh_index.h
/opt/local/include/opencv2/flann/lsh_index.h:195: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/lsh_index.h:196: There are no arguments to ‘assert’ that depend on a template parameter, so a declaration of ‘assert’ must be available
/opt/local/include/opencv2/flann/timer.h
/opt/local/include/opencv2/flann/timer.h:47: ‘clock_t’ does not name a type
/opt/local/include/opencv2/flann/timer.h:69: ‘startTime’ was not declared in this scope
/opt/local/include/opencv2/flann/timer.h:69: ‘clock’ was not declared in this scope
/opt/local/include/opencv2/flann/timer.h:77: ‘clock_t’ was not declared in this scope
/opt/local/include/opencv2/flann/timer.h:77: Expected `;’ before ‘stopTime’
/opt/local/include/opencv2/flann/timer.h:78: ‘stopTime’ was not declared in this scope
/opt/local/include/opencv2/flann/timer.h:78: ‘startTime’ was not declared in this scope
/opt/local/include/opencv2/flann/timer.h:78: ‘CLOCKS_PER_SEC’ was not declared in this scope
/opt/local/include/opencv2/core/internal.hpp
/opt/local/include/opencv2/core/internal.hpp:437: ‘assert’ was not declared in this scope
/opt/local/include/opencv2/core/internal.hpp:443: ‘assert’ was not declared in this scope
(null): -Wuninitialized is not supported without -O
please help me. I lost a lot of time
I see that you have OpenCV installed in “/opt/local”, have you supplied the correct paths in the configuration values? In the post I say to use “/usr/local/include” for “Header Search Paths” but if you have your include files in “/opt/local/include” you have to configure with that path. The same for “Library Search Paths”.
It looks like you installed OpenCV with MacPorts, am I righ?
yes , I installed opencv with macports and I changed all configurations.
as :
Header Search Paths: /opt/local/include
Library Search Paths: /opt/local/lib
and I added many libraries to my file using : File-> Add Files.
an this is my code main.cpp
//
// main.cpp
// MOG
//
// Created by Rafik Gouiaa on 2013-02-07.
// Copyright (c) 2013 Rafik Gouiaa. All rights reserved.
//
#include
#include
#include
#include
#include
#include “opencv2/features2d/features2d.hpp”
#include”opencv2/highgui/highgui.hpp”
#include”opencv2/core/core.hpp”
#include”opencv2/legacy/legacy.hpp” (BruteForceMatcher is defined here!)
int main(int argc, char** argv)
{
IplImage * pInpImg = 0;
// Load an image from file – change this based on your image name
pInpImg = cvLoadImage(“my_image.jpg”, CV_LOAD_IMAGE_UNCHANGED);
if(!pInpImg)
{
fprintf(stderr, “failed to load input image\n”);
return -1;
}
// Write the image to a file with a different name,
// using a different image format — .png instead of .jpg
if( !cvSaveImage(“my_image_copy.png”, pInpImg) )
{
fprintf(stderr, “failed to write image file\n”);
}
// Remember to free image memory after using it!
cvReleaseImage(&pInpImg);
return 0;
}