Friday 18 September 2015

Installing OpenCV for MAC!

After instructions guide for installing OpenCV on Ubuntu and installing OpenCV for Windows, here is the guide to install OpenCV on Mac. I have been using OpenCV on ubuntu 14.10 for three years now. However, installing OpenCV on Mac was real pain. No appropriate tutorials added to the pain.!! So, finally here is a tutorial which will try to help you out setting up OpenCV on Mac and ease out your help to some extent.


Prerequisites

  • Make sure you have git command line tool installed on your machine.
  • Make sure Macports is installed on your machine. If not, you can get it here.Install it before you proceed.
  • If you don’t have cmake, run the following command to install it (this command uses Macports): 
  •  $ sudo port install cmake

Downloading OpenCV

We are now ready to install OpenCV. Download the latest version of OpenCV from here. Make sure you download the version for your platform (Mac, Windows, etc). You will get a directory named opencv-2.x.x, depending on the latest version number.
If you don’t want to worry about downloading every time a new version is released, you can download the OpenCV git repo. This way, you can easily maintain and update your OpenCV version with a simple command. Open the terminal, go to a convenient location and download the OpenCV source code using the following command:

$ git clone git://code.opencv.org/opencv.git
It will create a directory called “opencv” and download all the files here. When you want to update to a new version, just go to this folder and type the following command:

$ git pull
This will update your repo to the latest version.


Installing OpenCV

Now that we have the source code for OpenCV, we need to build it. Run the following commands:

$ cd opencv
$ mkdir build
$ cd build
$ cmake -G "Unix Makefiles" ../
$ make -j8
We need to place the libraries in the correct locations so that they are accessible. Run the following command:

$ sudo make install
This will install everything in the /usr/local/ directory. That’s it! OpenCV is now installed on your machine.


OpenCV with Xcode

Now that we have installed OpenCV, we are ready to use it in our C/C++ code. Xcode is the most popular IDE on Mac OS X, so we would naturally want to see how to use OpenCV on Xcode. I have outlined the procedure below:
  1. Open Xcode
  2. Select Xcode -> New -> Project.
  3. A new window will pop up. On the left sidebar, under “OS X”, select “Application” and choose “Command Line Utility”. Click “Next”
  4. In the next window, give a name for the project and make sure the “Type” field at the bottom is set to C++. Click “Next”
  5. In the left sidebar, you should be able to see your project name next to a small Xcode icon. Double click on the project. This should open the Build Settings tab (located on the top of the new window).
  6. In the “Architectures” section, make sure it is set to “Native architecture of the build machine”.
  7. Scroll down to the “Search Paths” section and set “Header Search Paths” to: /usr/local/include. You can do this by double-clicking on the blank space right next to “Header Search Paths” and clicking on the “+” symbol at the bottom left. After you add it, make sure you select “recursive” located on the end of the same row.
  8. Close this window.
  9. In the left sidebar, you should be able to see your project name with a folder icon next to it. If you expand it, you will see main.cpp. Now right click on this project and select “New group”. Name it “OpenCV Libraries”.
  10. Right click on “OpeCV Libraries” and select Add Files to “Project” … (the word “Project” would have been replaced with your project name)
  11. Press the “/” key to get the “Go to” folder prompt. Enter the following path: /usr/local/lib
  12. Select all the files whose names are of the form libopencv_*.dylib.
    Note: Although you don’t have to worry about this, it’s better to know anyway. There will be aliases in that file list as well. So before selecting a file, look at the file icon. If there’s an arrow on the icon, it means that this file is an alias. You don’t have to add these files, since they are just aliases (and not real files).
  13. Click “Add” and you are done!
Go to main.cpp and write a simple piece of OpenCV code. Check if it runs. If you see an error, read on.


Running OpenCV Code From Command Line

This is the last sure shot way of running OpenCV code, but you will miss out on the niceness of an IDE. You can use cmake to build and run OpenCV code. Open the Finder and create a folder for your project. Place your .cpp file there. Create a file called “CMakeLists.txt” and place the following lines in that file:

project(PROJECT_NAME)
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

find_package(OpenCV REQUIRED)

# Project Executable
add_executable (filename filename.cpp)
target_link_libraries(filename ${OpenCV_LIBS})
In the above lines, replace “PROJECT_NAME” with your project name and replace filename by your source code file name. For example, if your project name is “Image Display” and your filename is “display.cpp”, then the contents of CMakeLists.txt should be:


project(IMAGE_DISPLAY)
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

find_package(OpenCV REQUIRED)

# Project Executable
add_executable (display display.cpp )
target_link_libraries(display ${OpenCV_LIBS})
Once you create this file, run the following commands in the same directory:
$ cmake .
$ make
You should now be able to see the executable with the same name as the your .cpp file. Now open the terminal and just type the following to run the code:

$ display
If you have any command line arguments that you are taking in your .cpp file, give them here:

$ tracker arg1 arg2
And there you go! You are all set to do some OpenCV coding!! Subscribe to regularly get updates in your mail box.

No comments:

Post a Comment