HOWTO: Install, Build and Use openCV (MacOSX 10.10)

Overview

Despite the wealth of information on the internet, installation guides for openCV are far and few between. Whilst we have used openCV packages in previous projects they have always been wrapped in an addon format, which obviously makes it easier to use but, for a forthcoming project we needed the ability to access the library directly. Briefly, openCV is a library of functions which mainly focuses on image analysis, processing and evaluation. In lamen’s terms, it allows computers to process and understand images and other forms of visual data.

In this post we will explain how we managed to work around the mysterious installation process, and provide a simple set of instructions that will enable you to install, build and use the openCV libraries and binaries on your system.

First you will need:

  • Mac OSX Yosemite 10.10
  • XCode
  • Command Line Tools (This is done from inside XCode)
  • CMake

Ready … lets begin!

Building OpenCV

Update:

Having rebuilt openCV using the XCode again the makefile for some reason is not generated, will try and find out why. For now use the Terminal method as outlined below.
Attention: Steps 2 a and b document methods of installing both the static and shared libraries.

Step 1:
Download openCV and unzip it somewhere on your computer. Create two new folders inside of the openCV directory, one called StaticLibs and the other SharedLibs.

Step 2a: Build the Static Libraries with Terminal.
To build the libraries in Terminal.
Screen Shot 2014-10-23 at 10.17.51

  • Open CMake.
  • Click Browse Source and navigate to your openCV folder.
  • Click Browse Build and navigate to your StaticLib Folder.
  • Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.

Screen Shot 2014-10-23 at 10.24.01
You will need to uncheck and add to the following options.

  • Uncheck BUILD_SHARED_LIBS
  • Uncheck BUILD_TESTS
  • Add an SDK path to CMAKE_OSX_SYSROOT, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.
  • Add x86_64 to CMAKE_OSX_ARCHITECTURES, this tells it to compile against the current system
  • Uncheck WITH_1394
  • Uncheck WITH_FFMPEG

Click Configure again, then Click Generate.

When the application has finished generating, Open Terminal and type the following commands.

- cd <path/to/your/opencv/staticlibs/folder/>
- make (This will take awhile)
- sudo make install

Enter your password.
This will install the static libraries on your computer.

Step 2c: Build the Shared Libraries with Terminal.

  • Open CMake.
  • Click Browse Source and navigate to your openCV folder.
  • Click Browse Build and navigate to your SharedLib Folder.
  • Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.

You will need to uncheck and add to the following options.

  • Check BUILD_SHARED_LIBS
  • Uncheck BUILD_TESTS
  • Add an SDK path to CMAKE_OSX_SYSROOT, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.
  • Add x86_64 to CMAKE_OSX_ARCHITECTURES, this tells it to compile against the current system
  • Uncheck WITH_1394
  • Uncheck WITH_FFMPEG

Click Configure again, then Click Generate.

When the application has finished generating, Open Terminal.

- cd <path/to/your/opencv/SharedLibs/folder/>
- make (This will take awhile)
- sudo make install

Enter your password.
This will install the shared libraries on your computer.

Make an Application

This is a very basic example, but the similar principles can be applied to other code.
For this post, lets make an application that shows two images, one normal and one that has been put through a blur filter.
Step 1:

  • Create a new folder somewhere on the computer.
  • Inside the folder, create a CMakeLists.txt file then create a BlurImage.cpp file.
  • Then add an image file.

For this example I’ll use this fruity number.
fruits

Step 2:

Open the BlurImage.cpp in your favourite text editor and add the following text.

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

Mat src; Mat dst;
char window_name1[] = "Unprocessed Image";
char window_name2[] = "Processed Image";

int main( int argc, char** argv )
{
    /// Load the source image
    src = imread( argv[1], 1 );

    namedWindow( window_name1, WINDOW_AUTOSIZE );
    imshow("Unprocessed Image",src);

    dst = src.clone();
    GaussianBlur( src, dst, Size( 15, 15 ), 0, 0 );

    namedWindow( window_name2, WINDOW_AUTOSIZE );
    imshow("Processed Image",dst);

    waitKey();
    return 0;
}

Save the file.

Step 3: 

Open the CMakeLists.txt file then add the following text.

cmake_minimum_required(VERSION 2.8)
project( BlurImage )
find_package( OpenCV )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( BlurImage BlurImage.cpp )
target_link_libraries( BlurImage ${OpenCV_LIBS} )

Save the File.

Step 4: 
Open Terminal and navigate to your applications directory.

- cd <path/to/application/folder>
- /Applications/CMake.app/Contents/bin/cmake .
- make

This will generate both the makefile and the executable file.

Then type ./BlurImage image.jpg

Screen Shot 2014-10-23 at 13.44.56

Screen Shot 2014-10-23 at 13.45.52

Yay … it works all you need to do is add your own .cpp file and alter the .txt file. Then follow the above commands.

For more detailed examples go to the openCV Tutorial Page, or check out the sample folder inside the opencv folder.

Huge thank you to Daniel Shiffman, whose guide put us on the right track : http://shiffman.net/2011/01/23/how-to-build-opencv-static-libraries-mac-os-x/

40 thoughts on “HOWTO: Install, Build and Use openCV (MacOSX 10.10)

  1. Hi ! First i would like to thank you for the descriptive steps to install openCV. but i have a problem with the very last step. In step 4 when I input “make” in the terminal i don’t get the “[100%] Built target BlurImage.”
    pls help me in sorting it out! Thank you

    • Hi Prabhakaran,

      No problems, do you get any error messages? Could you possibly copy the output?

      David

      • Hi David,

        Thanks for the reply. I tried building using step 2a… It Works!!!!
        I am new to mac and openCV. I had trouble installing but your blog made it easy. Thanks 🙂

  2. Hi, in step 2a when i input “sudo make install” i get:
    make: *** No rule to make target `install’. Stop.

    • Hi Graziano,
      Am trying to figure out this error. For now try using the Terminal methods.

      Cheers

  3. Thanks. Unfortunately, the terminal method gives the same result for “make”
    make: *** No targets specified and no makefile found. Stop.

  4. MacBook-Pro:~ graziano$ cd Documents/opencv-2.4.9/StaticLibs/
    MacBook-Pro:StaticLibs graziano$ make
    make: *** No targets specified and no makefile found. Stop.

    • Hi Graziano,

      Have you ran CMake?
      In your staticlib folder there should be a Makefile file.

      Screen Shot 2014-11-05 at 16.50.17

      Cheers

    • I recompiled and now it works. i think that the problem was in 10.10 SDK.
      For a new project I have to create every time CMakeLists.txt ??

  5. Hi…
    First of all thanks for the guide.
    Now, I have a problem … I followed the guide to install opencv 3.0, unfortunately, then, I realized that I need the version 2.4.* ’cause I have to use other libraries that are not supported in 3.0.
    How can I then uninstall opencv 3.0 then go to reinstall the 2.4.9?
    thanks
    Luca

    • Hi Luca,

      If you follow the guide using the openCV 2.4.* files it should replace the openCV 3.0.0 files.

      Best

  6. Hi,

    under: “Step 2c: Build the Shared Libraries with Terminal” you showed commands for the terminal:


    – cd
    – make (This will take awhile)
    – sudo make install

    Should “staticlibs” be “sharedlibs” instead?

    Thanks,
    Katharina

  7. Hello David,

    Thank you for creating a clear walk through guide. It worked flawlessly on Yosemite. Just wanted to make a comment: I used the path “ShareLibs” instead of “StaticLibs” as mentioned in the section “Step 2c: Build the Shared Libraries with Terminal.”

    Thank you again.

  8. Wow! I followed some compile instructions on the internet and they worked. That never happens! Thank you for this post.

  9. This worked perfectly for me under Mountain Lion, surprisingly! Why I’m still using Mountain Lion is another story.

    Thank-you!

  10. Extremely straightforward and the only opencv tutorial for mac that’s worked without crashing/exploding.

    Thanks!!!!

  11. Hello, is there any advantage of using this method instead of using the homebrew package manager?

    • Hi Andre,

      Its just personal preference. But, can’t see any disadvantages to using homebrew. This method for the most part tries to avoid filling your hard drive up with unnecessary data.

      Best
      David

    • Hi Ranvit, open terminal navigate to your folder then type touch BlurImage.cpp should generate the file.

      Best

  12. Thank you for creating this very good step by step tutorial!
    Using it right now…
    I got an error but after google it I found that must not exist white spaces anywhere on the path to the files.
    Maybe you can add this information to this great howto.
    Again, thanks for share your knowledge!

    • Thank you MOSO – your post helped a lot – was getting stuck at the “make” stage at 43% until I changed the paths to make sure there were no spaces in the folder names, and re-followed the tutorial above.

      Some clarifications though if you can:
      – This tutorial says (in the title) that it is for OSX 10.10, but step 2a and 2c get me to put something to do with MacOSX10.9.sdk into the CMAKE_OSX_SYSROOT field in CMAKE… can you check this isn’t for an old tutorial made for 10.9, or confirm we really are pointing CMAKE to the 10.9.sdk instead of the 10.10 one?

      – your next tutorial on setting up XCode 6.1 for it (i am using Xcode 6.1.1 though, close enough I hope) says that it’s looking for files in /usr/local/include and /usr/local/lib. But this tutorial asks me to extract the OpenCV zip file “anywhere” on my computer… is that extracted zip file needed after this installation is complete? I’m not sure if my future programs are still referencing that.

      I just want to be able to add OpenCV as a framework to my current projects. I already have other frameworks added but they are all “in-built” somehow.

      • Hi Aaron,
        Many thanks for your comments.
        I will change the white space errors.

        The sdk issue is a curious one. When I installed the libraries on yosemite, for some reason i didnt have the the 10.10.sdk! It should be fine with 10.10 though.

        With installing the libs, you download the source code to your desktop then through the cmake process you create the makefiles which then installs the libraries into your /usr/local/bin and /usr/local/include. So the libs are installed in a static location. Then in xcode you just point the compiler at the libs you need.

        Best

  13. Hi,

    When I ‘make’ in step 2a and c, I get up to 61% but then it stops due to errors. I would suggest unchecking BUILD_opencv_legacy as well. It seems to have some terrible code that my compiler does not like. When I unchecked legacy, everything worked smoothly.

    If legacy is an important component and if there is a way to make my compiler ignore those errors then I would love to hear it. In the meantime everything seems to be working fine. Thanks for this awesome tutorial.

  14. Hi,
    I have a problem after typing “make” in the step 2)a, it stops at 61pourcent and says:

    /Users/tomlorentbourdo/Desktop/opencv-2.4.10/modules/legacy/src/calibfilter.cpp:98:9: error:
    comparison of array ‘this->latestPoints’ not equal to a null pointer is
    always true [-Werror,-Wtautological-pointer-compare]
    if (latestPoints != NULL)
    ^~~~~~~~~~~~ ~~~~
    /Users/tomlorentbourdo/Desktop/opencv-2.4.10/modules/legacy/src/calibfilter.cpp:526:9: error:
    address of array ‘this->latestCounts’ will always evaluate to ‘true’
    [-Werror,-Wpointer-bool-conversion]
    if( latestCounts )
    ~~ ^~~~~~~~~~~~
    2 errors generated.
    make[2]: *** [modules/legacy/CMakeFiles/opencv_legacy.dir/src/calibfilter.cpp.o] Error 1
    make[1]: *** [modules/legacy/CMakeFiles/opencv_legacy.dir/all] Error 2
    make: *** [all] Error 2
    mbp-de-tom:StaticLibs tomlorentbourdo$

    • Hi Tom,

      Much like Dan in the comment above has done, I’d uncheck BUILD Legacy in the CMAKE application. Think that legacy has some issues.

      Best

      David

  15. Oh well, sorry for making you lose your time I just didn’t see, thank you for your fast answer and your great work and thanks to Dan too!

Comments are closed.