Up & Running: OpenCV3, Python 3, & Anaconda

OpenCV3 is the latest major edition of the most comprehensive computer vision library, OpenCV. OpenCV has continued to expand its Python bindings and has finally started to support Python 3. Below I’ll show a fairly painless way of setting up this environment.
Anaconda makes creating virtual environments for Python and installing non-python packages like numpy and OpenCV incredibly easy, so we’re going to use that. Go to the following link and make sure to select I want Python 3.4.
continuum

If you’re on Windows this will download an executable that will install Anaconda. If you’re on Linux it will download a shell script that will install Anaconda. I assume OSX is similar to Linux.

Once we have Anaconda installed we’re going to want to create a new virtual environment in which to run all of our computer vision tasks. Virtual environments let us install packages without cluttering up our python ecosystem. In Windows make sure to open up the Anaconda command prompt. In Linux just a normal terminal. We’re going to install the following packages: numpy, scipy, scikit-learn, matplotlib. I don’t know if we’ll need all of them but they’re all great packages so what the heck. Only numpy is needed for sure. Run the following commands. (If you’re on Windows source activate becomes activate.)

conda create -n opencv numpy scipy scikit-learn matplotlib python=3
source activate opencv
conda install -c https://conda.binstar.org/menpo opencv3

Let’s explain what these commands mean. conda create makes a new virtual environment -n gives that environment a name. The names after that are the packages that get installed upon creating the environment and the keyword argument python=3 makes sure that we use python 3 (3.4 in my case). You might be wondering why we did not install OpenCV along with the other packages. That’s because OpenCV as of this writing is not included in Anaconda’s regular list of packages. We need to use binstar to get that.

Before that, we’ll activate our new virtual environment. This is similar to sourcing your bash profile in linux. In Windows source activate becomes activate. You should now see the name of your environment (opencv) prepended to the command prompt. Now we’re going to install OpenCV3. To do this we had to find where opencv3 is located. Go to binstar.org and search for opencv3. I found it under user menpo for linux_x64 and windows_x64. OSX has a separate package. We want to take that URL and modify it so that it works with the conda install. https://conda.binstar.org/user package_name The command as formatted above should install opencv3.

To check whether this works or not start up a python prompt. Run import cv2 (!!!IMPORTANT it’s still cv2 not cv3)To check the version print(cv2.__version__) If everything worked out you should have 3.0.0 🙂

Comments

  1. Elise

    Thank you very much, this post was of great help.

    I was having trouble because I had opencv 2.4 installed with anaconda and couldn’t upgrade it by any means.

    Had to run in terminal: conda remove opencv

    After that I used the comands specified by the author and it worked while I was at the enviroment (opencv), but after opening a new terminal window Python couldn’t find cv2. To fix this I runned the same ‘conda install -c https://conda.binstar.org/menpo opencv3′ in a regular terminal windows and it installed opencv3.

  2. Juan

    Thank you very much for your ‘to the point’ solution. I’ve just created a new environment for Python 2.7.11 + OpenCV 3.1.0 in OSX using your script as is (well, using python=2 instead)

  3. phyo arkar

    i am getting : ImportError: /usr/lib64/libpangoft2-1.0.so.0: undefined symbol: FcWeightToOpenType

    different system version of libpango . ..

  4. Pingback: Counting cancer cells with computer vision for time-lapse microscopy | Theory, Evolution, and Games Group

  5. Rishi Prasad

    Man, you’re a life saver! Thanks for saving me a lot of time here.

  6. saeed iqbal khattak

    Dear All,

    but still I am facing problem, When I use idle.

    Python 3.5.1+ (default, Mar 30 2016, 22:46:26)
    [GCC 5.3.1 20160330] on linux
    Type “copyright”, “credits” or “license()” for more information.
    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    import cv2
    ImportError: No module named ‘cv2’
    >>>
    Is there any way to resolve the above issues.

    1. Profile photo of Chris Sketch
      Chris Sketch Post author

      Have you activated the conda environment using activate or source activate? If you have you should be able to write conda list and see cv2. If it is listed and it still doesn’t work, then perhaps you need to start idle from the command prompt that has the environment activated or close Idle and open it back up after the environment has been activated.

    2. Jon

      When you run your file, make sure you don’t add “sudo” in front of it, because that was the issue for me.

  7. Adam

    Thanks – so much better than building from source. However, while this to worked well for Ubuntu 14.04, on OS X 10.11.5 it gives the following error during import:

    ImportError: dlopen(/opt/local/lib/Anaconda/lib/python2.7/site-packages/cv2.so, 2): Library not loaded: @rpath/libhdf5.10.dylib
    Referenced from: /opt/local/lib/Anaconda/lib/libopencv_hdf.3.1.0.dylib
    Reason: Incompatible library version: libopencv_hdf.3.1.dylib requires version 12.0.0 or later, but libhdf5.10.dylib provides version 11.0.0

    Also, any idea what backend this build uses for video I/O? On linux I could read “.avi” with the “MJPG” codec, but could not read “.mp4”. Is there a way to install / configure additional codecs?

    Thanks,

    1. Profile photo of Chris Sketch
      Chris Sketch Post author

      I don’t know how you would go about that. The binaries aren’t available on anaconda.org and pip is no help, so you would need to install from source as Adrian describes. I’m not sure how this would interact with conda’s virtual environment, but I’m guessing it’d be messy. Wish I could be more help :-/

  8. Profile photo of Zhi Zeng
    Zhi Zeng

    Thank you very much for your help! Here is my result following your instruction. Success!

    ****iMac:~ ****$ source activate OpenCV-py3-cv3
    prepending /Users/****/anaconda/envs/OpenCV-py3-cv3/bin to PATH
    (OpenCV-py3-cv3) ****iMac:~ ****$ conda install -c menlo opencv3=3.1.0
    Using Anaconda Cloud api site https://api.anaconda.org
    Fetching package metadata ………
    Solving package specifications: ……….
    Package plan for installation in environment /Users/****/anaconda/envs/OpenCV-py3-cv3:
    The following packages will be downloaded:
    package | build
    —————————|—————–
    tbb-4.3_20141023 | 0 1.6 MB menpo
    opencv3-3.1.0 | py35_0 37.3 MB menpo
    ————————————————————
    Total: 38.8 MB
    The following NEW packages will be INSTALLED:
    opencv3: 3.1.0-py35_0 menpo
    tbb: 4.3_20141023-0 menpo
    Proceed ([y]/n)? y
    Fetching packages …
    tbb-4.3_201410 100% |################################| Time: 0:00:26 61.73 kB/s
    opencv3-3.1.0- 100% |################################| Time: 0:04:19 150.82 kB/s
    Extracting packages …
    [ COMPLETE ]|###################################################| 100%
    Linking packages …
    [ COMPLETE ]|###################################################| 100%

  9. Aniruddha Saha

    I am working on Windows. I followed the steps. The installations worked perfectly. Now, when i open the Anaconda prompt and type python, then import cv2, and print cv2,__version__ it shows be opencv 3.1.0 and again when i activate opencv and type conda list it shows opencv 3.1.0 in the list. but when i open spyder from the prompt and try to import cv2 in the development environment in a .py file or in the IPython console it says ImportError: No module named ‘cv2’.

    Any solutions?

    1. Justin

      I have the exact same issue. Opencv3 3.1.0 is showing in anaconda Prompt, but in spyder if I run “import cv2″ I get the error ” ImportError: No module named ‘cv2’ “.

      I fixed this by running
      ” conda install -c https://conda.binstar.org/menpo opencv3 ”
      in the Anaconda Prompt window, but this time not from within the opencv environment. Hope that helps.

  10. Pingback: Tips for using Anaconda | Vic's World

  11. T

    OpenCV can be imported and run. However, reading video no matter it is .avi or .mp4 is not possible.
    I realise the problem is the ffmpeg and to solve this is to set a correct path. However, I have not so far sorted out the problem.

    OpenCV 3.1 + Python 3.5 + Mac

  12. Miko

    Hi Chris, it’s very nice!
    I am facing a silly problem as bellow.
    I successfully installed OpenCV3.1.0 by anaconda (python 3.5 + mac osx) and tested in python prompt
    >>> import cv2
    >>> cv2.__version__
    ‘3.1.0’

    But in eclipse (pydev), it’s showing
    ―――――――――――
    Unresolved import: cv2
    Please help me!

Your email address will not be published. Required fields are marked *