Charlie Gerard bio photo

Charlie Gerard

Software Developer & Creative technologist, I am passionate about the mix of science, art and technology

Email Twitter Google+ Github

I’m planning on starting a project involving computer vision using the OpenCV framework. As I’ve had some troubles getting everything installed and running a basic project, I’m writing this tutorial to help anyone who’s going through the same process, hoping it will make it easier :)

##Installing OpenCV

To get started, visit the OpenCV website and download the correct version for your OS. Then, go to the MacPorts project, download it and install it.

Once this is done, open a new terminal window, write ‘port’ and press enter. If you see the following, you’re good.

Find where you downloaded OpenCV and drag it to where you want it to live. Make sure you’re not going to change the location often because it could break projects.

Once you moved OpenCV to a directory you’re happy with, cd into this directory and create a folder called ‘build’ with the mkdir command.

Cd into this new folder and run ‘cmake -G “Unix Makefiles” ..

This command will start installing what OpenCV needs to run.

Once it’s done, run “make -j8” in your Terminal and ‘sudo make install’.

Everything should now be installed. If you check inside your build > lib folder, you should see all the frameworks that have been installed.

##Now, let’s setup a basic project in Xcode.

Open Xcode and create a new project by clicking on File > New > Project.

Go to OSX > Application and select ‘Command Line Tool’.

In the next window, make sure the language selected is C++ and enter the project name you’d like. For example ‘OpenCV Tutorial’.

Your project window will then open, select the project file (the file at the top of your project), and follow these steps:

  1. At the top, select Build Settings and search for Search Paths
  2. Change the Always Search User Paths to true.
  3. Change the Header Search Paths to usr/local/include.
  4. Change Library Search Paths to usr/local/lib.
  5. In the search field, remove your previous search and scroll down until you find the Linking section.
  6. Double click on Other Linker Flags, click on the + button and add:

  
1
2
-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'
  

All of the previous steps will allow Xcode to know where to look for when you import a library in your code.

Finally, go to your Xcode preferences, select Locations Tab, click Advanced and change the location button from Unique to Legacy.

##Writing our first program.

Click on ‘File > Add Files to …’ and select the image processing library (libopencv_imgproc.3.0.0.dylib), the core library (libopencv_core.3.0.0.dylib) and the gui library (libopencv_highgui.3.0.0.dylib).

Now we need to import them in our main.cpp file and write the rest of the code to open a basic webcam window.


    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;

int main(){

    VideoCapture cap(0);

    while(true){
        Mat Webcam;
        cap.read(Webcam);
        imshow("Webcam", Webcam);
    }

}
  

You project should build successfully and a webcam window should open.

You’re done!