Overview
This post demonstrates how to setup your XCode IDE to work with openCV libraries on Yosemite. Initially XCode can be an intimidating environment, but past all the windows and complex sections, it boasts a number of beneficial features that aid and simplify programming – which for newcomers to openCV are a must. Features such as autocomplete and integrated compiler are extremely useful in simplifying compilation, speeding up programming time and clarifying how pieces of code fit together.
WARNING: This guide assumes you have compiled and installed openCV
It not, see this guide.
How To
- Open XCode
- Create New Project
- Under OSX Application > Choose Command Line Tool
- Give your application a name then Save
Assigning the Search Paths
- Click the XCode project file in your inspector (which is the blue icon in the furthest left hand tab). You should now have three tabs in the centre window.
Build Settings | Build Options | Build Rules
- Click
Build Settings
- Scroll down until you find Search Paths
- Double Click the Header Search Paths option, then Click the plus icon
- Type in the following
/usr/local/include
- Next Double Click the Library Search Paths option, then Click the plus icon
- Type in the following
/usr/local/lib
Linking the Libraries
Build Settings
.
- Scroll until you find Linking
- Double Click Other Linker Flags and Click the plus icon
- Copy and Paste the following, then press enter
-lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab
Setting the Build Path
Build your Application
This is a sample application from the openCV package.
#include <opencv2/imgcodecs.hpp> #include <opencv2/videoio/videoio.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> #include <stdio.h> using namespace cv; using namespace std; //hide the local functions in an anon namespace namespace { void help(char** av) { cout << "The program captures frames from a video file, image sequence (01.jpg, 02.jpg ... 10.jpg) or camera connected to your computer." << endl << "Usage:\n" << av[0] << " <video file, image sequence or device number>" << endl << "q,Q,esc -- quit" << endl << "space -- save frame" << endl << endl << "\tTo capture from a camera pass the device number. To find the device number, try ls /dev/video*" << endl << "\texample: " << av[0] << " 0" << endl << "\tYou may also pass a video file instead of a device number" << endl << "\texample: " << av[0] << " video.avi" << endl << "\tYou can also pass the path to an image sequence and OpenCV will treat the sequence just like a video." << endl << "\texample: " << av[0] << " right%%02d.jpg" << endl; } int process(VideoCapture& capture) { int n = 0; char filename[200]; string window_name = "video | q or esc to quit"; cout << "press space to save a picture. q or esc to quit" << endl; namedWindow(window_name, WINDOW_KEEPRATIO); //resizable window; Mat frame; for (;;) { capture >> frame; if (frame.empty()) break; imshow(window_name, frame); char key = (char)waitKey(30); //delay N millis, usually long enough to display and capture input switch (key) { case 'q': case 'Q': case 27: //escape key return 0; case ' ': //Save an image sprintf(filename,"filename%.3d.jpg",n++); imwrite(filename,frame); cout << "Saved " << filename << endl; break; default: break; } } return 0; } } int main(int ac, char** av) { if (ac != 2) { help(av); return 1; } std::string arg = av[1]; VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file or image sequence if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param capture.open(atoi(arg.c_str())); if (!capture.isOpened()) { cerr << "Failed to open the video device, video file or image sequence!\n" << endl; help(av); return 1; } return process(capture); }
Then hold CMD + B
(This builds and compiles the application)
Go to your project folder. There should be a new folder called Debug, inside is your executable file.
Open Terminal then navigate to the folder, then simply type ./<yourprojectname> 0
This should open window and display the camera image.
And there you go!
Hi,
I’m getting a build failed error:
“main.cpp:9:10: ‘opencv2/imgcodecs.hpp’ file not found”
Seems related to this line of code
“#include ”
Do you know what’s the issue ? Or how I should redefined the path
-thanks
Hi Chris,
Could be one of two things.
How did you install opencv?
If the libs aren’t in the usr/include/ or usr/local folders then the compiler wont find them.
Also when you link the libraries, you need to point the compiler at the
-lopencv_imgcodecs
?Is there any more error data you could post?
D
Hi Chris,
Can’t seem to reproduce this error.
How did you make/install the libraries? Might be that it hasn’t built those files.
See this guide.
David
Hi, David
I have the libraries in /usr/local/lib and following your steps I did add -lopencv_imgcodecs
I do have opencv and opencv2 in /usr/local/include. (no lib if that makes a difference)
Im new to mac – Which more data could I post ?
So looking at the libraries I see that I have opencv2/imgcodecs.hpp and opencv2/video/video.hpp
I do have opencv2/highgui/highgui.hpp. Where can I get the missing files?
Thanks
I also discovered the missing opencv lib files. 2.4.10 and 2.4.9 are missing those modules in the zip files. I had to got download opencv_master on github and rerun CMake to be able to install those libraries.
Which then opened another issue. I’m running OSX 10.10, Xcode 6.1, and CPT 6.1. When I compile an example in Xcode, I get 201 architecture symbolic link errors. It’s acting like it doesn’t want to cooperate with the static libopencv_*.a files. I’ve tried quite a few combinations over the past two days but to no avail. Any thoughts?
Hi Chris,
Just so you know. You should not be following this example because you are running opencv2 and the example in the blog is for opencv3
here is the example code for opencv2:
https://github.com/Itseez/opencv/blob/2.4.9/samples/cpp/starter_video.cpp
You will also need to remove a few of the linker flags that are not associated with the library
I removed the following and things worked:
-lopencv_imgcodecs
-lopencv_shape
-lopencv_videoio
Cheers Oleg, I have updated the guides title. Once those flags are removed the principle should still be the same.
Hi, when I run the program I still have one error:
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I followed the advice of Oleg and it gives me 183 more errors… so I don’t know what to do, do you have any idea?
Thank you for your help!
Tom
Hi Tom,
Which version of openCV are you using?
Best
Hi again,
I’m using OpenCV 2.4.10 and Xcode 6.3, In my SharedLibs I can find a doc named opencv2 and I tried to do the way you did and I have tested with your example, then, I tried with the example of Oleg but it gives me one errors… When I have removed the files he adviced, I got 183 more errors…
Best
Hi,
It’s sayd:
ld: library not found for -lopencv_videoio
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I probably need to download -lopencv_videoio, do you have this file? You could maybe send me it by mail or something else? Because when i watched in my file “lib” I obviously didn’t find it..This is probably the problem.
Waiting for your answer,
Best Tom
Hi Tom,
Sorry just tested it against the 2.4.10 libs.
If you’ve installed that lib successfully. In Xcode change the -lopencv_videoio linker flag to be -lopencv_video. Its a different name in openCV3.0.0 land.
Best
Hi again David!
The thing is, I already have the linker flag -lopencv_video, AND, what is really funny, is that I also have -lopencv_videoio. But, when I delete the “videoio” in the linker flags, it gives me 183 errors in my programm, when I put it back, I only have one error. I have described the error in my past comment but when I watch in my libraries, I don’t find the -lopencv_videoio… It’s very strange.. can you use something to watch in my computer or something, I tried everything but no way…
Thank you for your time!
Tom