Setting up OpenCV and C++ development environment in Xcode for Computer Vision projects
Xcode is a free and excellent IDE not only for creating iOS apps but also for C++ development and working on computer vision projects using OpenCV. After interviewing with some companies in Silicon Valley, I got to know that a lot of commercial AR software such as Snap Lenses, Instagram’s Filters, Apple’s Animoji, etc. is written in C++ to maximize performance on resource constrained devices such as mobile phones and tablets. A lot of the new features in OpenCV are first available in C++ and then introduced to Python since OpenCV is natively written in C++ and provides bindings for Python, Java and MATLAB.
Unfortunately, I used Python with OpenCV for computer vision for ease of use since Python is much more readable, easier to code than C++ and setting the development environment is easier too. However, Python is not used for computer vision in industry and thus the motivation for writing this.
This post goes through setting up OpenCV and C++ environment on MacBooks which is actually the hardest part for a beginner starting in computer vision because of lack of trustworthy documentation since most tutorials online deal with setting up the environment for Python and a very less percentage of them deal with setting it up on Xcode.
- Install Xcode
Install Xcode from the App Store. Open the App Store and search for Xcode and then click the Get button(in my case it shows Open because I have Xcode installed). Since, Xcode(9.2) setup size is about 5.5 GB it may take about 30–60 minutes to install depending on the internet speed.
2. Install Homebrew
Aptly titled ‘The missing package manager for macOS’, enough said. Homebrew is the macOS equivalent of the Ubuntu/Debian-based apt-get. For the installation, just open a terminal and paste:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
3. Install OpenCV
To install OpenCV using brew, open a terminal and paste:
brew install opencv
This should install OpenCV 3.
4. Install pkg-config
pkg-config is a helper tool used when compiling applications and libraries. It helps you insert the correct compiler options on the command line rather than hard-coding values. This will be helpful for finding the correct linker flags for OpenCV. This will be more clear in the subsequent steps.
To install pkg-config using brew, open a terminal and paste:
brew install pkg-config
5. View OpenCV linker flags
To view the linker flags for OpenCV, run:
pkg-config --cflags --libs opencv
The output looks like:
-I/usr/local/Cellar/opencv/3.3.1_1/include/opencv -I/usr/local/Cellar/opencv/3.3.1_1/include -L/usr/local/Cellar/opencv/3.3.1_1/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_photo -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_face -lopencv_fuzzy -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core
The above shows you the includes and the libraries for OpenCV.
In case the command to get linker flags doesn’t work, you might need to specify the location of opencv.pc file:
pkg-config --cflags --libs path/to/opencv.pc
My opencv.pc file is located at
Yours should be on a similar path.
Running your code on the terminal
Paste the below line which compiles the OpenCV code with the appropriate linker flags.
g++ $(pkg-config --cflags --libs opencv) -std=c++11 yourFile.cpp -o yourFileProgram
Run the binary,
Running your code in Xcode
Before following the below steps to run OpenCV C++ code in Xcode, you first need to create a C++ project in Xcode.
- Click on File>New>Project
- Under Choose a template for your new project click on macOS
- Under Application click on Command Line Tool
- You should get the above screen. Fill in the details and set the Language to C++
Set Header Search Paths
To set Header Search Path in Xcode, first click on the Xcode project(computer vision in this case) and then go to Build Settings and then search for Header Search Paths.
Set the Header Search Path to the path of OpenCV include folder. In my case, this is:
Set Library Search Paths
In this case, follow steps similar to Set Header Search Paths above but search for library search paths in the search bar.
Set the Library Search Path to the path of OpenCV library folder. In my case this is:
Set Other Linker Flags
Search for other linker flags in the search bar.
Set the other linker flags with all the flag values obtained after running pkg-config command above.
You are all set to run your OpenCV project in Xcode. Press Cmd+R to run your Xcode project.
Passing arguments to your code
In case you want to pass arguments such as an image file to your Xcode project, you will need to edit scheme as shown above.
Using relative file paths
Xcode uses the absolute path of the image or resource. In order to set relative paths, you need to set the Working Directory. To do that, in Edit Scheme go to the Options tab in Run and check Use Custom Working Directory and add the project directory name.
Multiple files in same project
The project will fail to build in case you have multiple C++ files for a given project. In this case, you will need to create Targets within your project, where each Target corresponds to a C++ file.