Luis González

Love for developing and technology

Install OpenCV 3 With Python 3 (Mac OSX)

| Comments

It took me a while, but I did it. I want to migrate my final project to python 3, so I had to use Opencv 3.0. At the moment of writing, the opencv 3.0 is still alpha, but seems stable enough. So, we’ll be using python 3.4 + Opencv 3.0 alpha on OSX 10.9.4. Let’s start.

Installing python3.4

I have installed python3 using homebrew. I recommend using it. In this case, you should only install python3:

    brew install python3

If you already have python3, skip this step. You can also install openCV 2.4.9, but at the time of writing there is no option for openCV 3 with python3. You should also install numpy. For that you can use pip (already installed), a python packet manager:

    pip3 install numpy

Installing Cmake

You can also install Cmake with homebrew, but instead, I downloaded it directly from its homepage. This way you have cmake gui, a graphical user interface that makes it easier to manage. So, download and install as any program (dmg): cmake download (look for the Mac OSX 64/32-bit Universal dmg). If you have problems opening it, read this.

Installing opencv 3.0

First download it from the opencv homepage: opencv3.0 download Select the OpenCV 3.0 (at the time of writing, alpha) for Linux/Mac. We should now unzip it. It’s time to use cmake. Open it from your applications. It asks you to select where the source code is. Navigate to your opencv3.0 folder, previously unzipped. You should also select the build directory (usually you can make a build folder inside the opencv3.0 folder)

This image shows my configuration. You can filter the configuration parameters so it is easier to find. The important ones are those with “python3” prefix. You should set yours depending on your system installation, but if you followed this guide you should set them as they are shown.

So remember to:

  • Set the BUILD_opencv_python3. If it is not present, you can add it with the Add Entry button.
  • In my case, the parameters I had to set manually were PYTHON3_INCLUDE_DIR and PYTHON3_LIBRARY.
  • If the others parameters are not set automatically, it may help to run brew linkapps command before configuring. In any case, you can set them manually.
  • Your Current Generator is Unix Makefiles

Now, click configure. If everything goes fine, now you click generate. If any problem should happen, you can try to detect it by reading the output in the bottom of CMAKE. Look for problems related with Python. It would be easier if you copy the text into textedit so you can find words.

All right, time to open a terminal. In the terminal, navigate to the folder where you have your build folder, something like:

    cd /Users/[username]/Downloads/opencv-3.0.0-alpha/build

Now execute the make command (-jX where X is the number of cores of your CPU, to make compilation faster):

    make -j8

For the next command you need super user privileges:

    sudo make install

Checking it all works

If there were no errors, you should be able to import it. Execute this in the terminal:

    import cv2

If there is no Error, congratulations, you have opencv3 with python 3.

Now go out for a beer, you earned it.

What? It didn’t work? No time for beer yet then…

UPDATE: If you got problems like import error, I recommend that you look careful at the outputs in every step. Actually, you should always do that for everything. Looking for words like ERROR. If you had made it to last step, you can try to uninstall and try configuring and installing it all again. To uninstall your recently installed openCV you should go to the build directory from the terminal and run this command:

    sudo make uninstall

I usually do this for installing newer versions of OpenCV too (OpenCV 3.0 - beta is out now).

UPDATE2: I add some useful info and links from the comments (Thanks everyone for your collaboration):

  • Bìtao Qiu said: I faced many troubles in installing opencv3, and setting locale LANG=’en_US.UTF-8’ and using this post: link really help. The question on the link was answered by Marc, who commented that I had exactly the same problem that seems specific to Anaconda. I finally fixed it and posted my answer.
  • Artem Fedosov said: You should probably mention how to include opencv_contrib modules as some of core functionality migrated there. It would be really useful. (I used your tutorial + their readme from here: link.
  • kungfujam shared this link. Give a look if you keep having problems.

ANOTATION: I have just installed the last stable version of OpenCV 3. I uninstalled previous version, as explained in first UPDATE. I had to follow the exact same steps here. I also compiled it for Python 2. In this case it detected everything allright and only needed to check the BUILD_opencv_python2 parameter.

ANOTTATION2: As requested in the comments, I add the CMakeCache.txt. I have also activated Python 2 and it is with OpenCV 3 stable.

If you keep having problems, post them in the comments and I’ll try to help ;)