How To Setup OpenCV on Mac?

15 Jan 2015 . tech . Comments #OpenCV

###The Plot I had an issue with upgrading to OS X Yosemite (10.10) and then had to flash the HDD. Hence, I had to re-install all the libraries and programs I required to get working again.

###A Background The setup of OpenCV is mysterious. Although there are many resources available on the www, I couldn’t find a near and minimalistic setup routine. So I started to search franticly and found few bits and pieces scattered. I shall put this together here and keep it sufficiently short.

I assume that the OS is at 10.10 or above. The additionally required programs are:

  1. Xcode
  2. Command line tools. See this for help.
  3. CMake. Developed by Kitware.

###The Gimmick

Step 1: Getting openCV

Download openCV. Version 3.0 B is available whilst writing this post. The download begins from the Git repo. Once the download is complete, unzip the package on your machine. Location: /usr/anyWhere/openCV.

Step 2: Create dir(s) for building libs

Create two new folders inside the openCV directory and name those folder as staticLib and sharedLib respectively.

$ cd ~/openCV/ //go to the openCV folder and open
$ mkdir staticLib //create a new dir called staticLibs
$ cd .. //move back to openCV folder
$ mkdir sharedLib //create a new dir called sharedLibs

Step 3: Build Static Libraries

  • Open CMake.
  • Click Browse Source... button and navigate to the openCV folder. You should be at ~/openCV.
  • Click Browse Build... button and navigate to your staticLib folder. You should be at ~/openCV/staticLib.
  • Hit the configure button. Once you click configure, you’ll have to select the mode of generation. Choose Unix-Makefile and confirm by pressing OK. At this point, CMake will preform tests. You must see the content holder filled with entries in red.

Now ensure you carefully UNCHECK the following:

  • BUILD_SHARED_LIBS
  • BUILD_TESTS
  • WITH_1394
  • WITH_FFMPEG

Add the following:

  • Search for CMAKE_OSX_SYSROOT then add this path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk.
  • Search for CMAKE_OSX_ARCHITECTURE and add x86_64 to the value attribute.

Click Configure. Then click Generate. Once CMake completes generating open the Terminal and type the following:

$ cd ~/openCV/staticLib/
$ make
$ sudo make install //may require system admin password

Now, static libraries are successfully installed.

Step 4: Build Shared Libraries

To build shareLib, we’ll follow almost the same steps as above.

  • Click Browse Source... button and navigate to the openCV folder. You should be at ~/openCV.
  • Click Browse Build... button and navigate to your sharedLib folder. You should be at ~/openCV/sharedLib.
  • Hit the configure button. Once you click configure, you’ll have to select the mode of generation. Choose Unix-Makefile and confirm by pressing OK. At this point, CMake will preform tests. You must see the content holder filled with entries in red.

Now ensure you carefully UNCHECK the following:

  • BUILD_SHARED_LIBS
  • BUILD_TESTS
  • WITH_1394
  • WITH_FFMPEG

Add the following:

  • Search for CMAKE_OSX_SYSROOT then add this path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk.
  • Search for CMAKE_OSX_ARCHITECTURE and add x86_64 to the value attribute.

Click Configure. Then click Generate. Once CMake completes generating open the Terminal and type the following:

$ cd ~/openCV/sharedLib/
$ make
$ sudo make install //may require system admin password

Now, shared libraries are successfully installed.

OpenCV is successfully installed. You are all set to build your first program!

####Further Reading [1] OpenCV Documentation.

[2] Daniel Shiffman’s blog on building libs.