Download Opencv Mac

Posted on

OpenCV on Mac OSX: A step-by-step guide I’m using OpenCV for my 4th year design project and setting it up was a huge pain. I had to look through a whole bunch of different sites to figure out what to do. 1) Download OpenCV and Cmake. 2) Double click OpenCV file to expand it and rename the expanded folder 'opencv' (if it isn't already). 3) Open the Cmake folder you just downloaded, find the Cmake GUI application, and open it. 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. Click Browse Source and navigate to your openCV folder.

opencv_mac_w_python2
0) Get Anaconda Python
1) Download CMake for Mac and ensure you add the command line binaries to your PATH (sudo '/Applications/CMake.app/Contents/bin/cmake-gui' --install)
2) Download OpenCV
3) cd into the OpenCV director and run `mkdir build`
4) cd into the `build` director you just created and run the following:
Make sure you update the path to your own installation of Anaconda (i.e. replace 'sramanujam' in the following with your user name).
cmake ../ -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_EXAMPLES=ON -DBUILD_NEW_PYTHON_SUPPORT=ON -DINSTALL_PYTHON_EXAMPLES=ON -DPYTHON_EXECUTABLE=/Users/sramanujam/anaconda/bin/python -DPYTHON_INCLUDE_DIR=/Users/sramanujam/anaconda/include/python2.7/ -DPYTHON_LIBRARY=/Users/sramanujam/anaconda/lib/libpython2.7.dylib -DPYTHON_NUMPY_INCLUDE_DIR=/Users/sramanujam/anaconda/lib/python2.7/site-packages/numpy/core/include/ -DPYTHON_PACKAGES_PATH=/Users/sramanujam/anaconda/lib/python2.7/site-packages/ -DBUILD_PYTHON_SUPPORT=ON
You should see the following in the console log which confirms that Python module will be build:
-- To be built: hal core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres ts features2d calib3d stitching videostab python2
5) Run: make; sudo make install
6) Switch to the Python directory (OPENCV_HOME/build/modules/python2) and run:
make
sudo make install
This will install cv2.so in your Python's site-packages directory
7) Open up your Python REPL and import cv2, it should work
Here's a sample log (of the last couple of steps):
[email protected]:~/Downloads/opencv-3.0.0/build
⇒ cd modules
[email protected]:~/Downloads/opencv-3.0.0/build/modules
⇒ ls
CMakeFiles cudacodec cudastereo imgcodecs python2 videostab
Makefile cudafeatures2d cudawarping imgproc shape viz
calib3d cudafilters cudev java stitching world
cmake_install.cmake cudaimgproc features2d ml superres
core cudalegacy flann objdetect ts
cudaarithm cudaobjdetect hal photo video
cudabgsegm cudaoptflow highgui python videoio
[email protected]:~/Downloads/opencv-3.0.0/build/modules
⇒ cd python2
[email protected]:~/Downloads/opencv-3.0.0/build/modules/python2
⇒ ls
CMakeFiles headers.txt pyopencv_generated_ns_reg.h
Makefile pyopencv_generated_funcs.h pyopencv_generated_type_reg.h
cmake_install.cmake pyopencv_generated_include.h pyopencv_generated_types.h
[email protected]:~/Downloads/opencv-3.0.0/build/modules/python2
⇒ make
[ 2%] Built target opencv_hal
[ 7%] Built target zlib
[ 15%] Built target opencv_core
[ 23%] Built target opencv_imgproc
[ 25%] Built target opencv_video
[ 28%] Built target opencv_ml
[ 35%] Built target libtiff
[ 46%] Built target libjpeg
[ 53%] Built target libwebp
[ 58%] Built target libjasper
[ 61%] Built target libpng
[ 74%] Built target IlmImf
[ 76%] Built target opencv_imgcodecs
[ 79%] Built target opencv_videoio
[ 79%] Built target opencv_highgui
[ 79%] Built target opencv_objdetect
[ 79%] Built target opencv_flann
[ 84%] Built target opencv_features2d
[ 89%] Built target opencv_calib3d
[ 92%] Built target opencv_stitching
[ 92%] Built target opencv_superres
[ 92%] Built target opencv_shape
[ 97%] Built target opencv_photo
[100%] Built target opencv_videostab
[100%] Built target opencv_python2
[email protected]:~/Downloads/opencv-3.0.0/build/modules/python2
⇒ sudo make install
[ 2%] Built target opencv_hal
[ 7%] Built target zlib
[ 15%] Built target opencv_core
[ 23%] Built target opencv_imgproc
[ 25%] Built target opencv_video
[ 28%] Built target opencv_ml
[ 35%] Built target libtiff
[ 46%] Built target libjpeg
[ 53%] Built target libwebp
[ 58%] Built target libjasper
[ 61%] Built target libpng
[ 74%] Built target IlmImf
[ 76%] Built target opencv_imgcodecs
[ 79%] Built target opencv_videoio
[ 79%] Built target opencv_highgui
[ 79%] Built target opencv_objdetect
[ 79%] Built target opencv_flann
[ 84%] Built target opencv_features2d
[ 89%] Built target opencv_calib3d
[ 92%] Built target opencv_stitching
[ 92%] Built target opencv_superres
[ 92%] Built target opencv_shape
[ 97%] Built target opencv_photo
[100%] Built target opencv_videostab
[100%] Built target opencv_python2
Install the project...
-- Install configuration: 'RELEASE'
-- Installing: /usr/local/lib/python2.7/site-packages/cv2.so
[email protected]:~/Downloads/opencv-3.0.0/build/modules/python2
⇒ python
Python 2.7.11 Anaconda 2.2.0 (x86_64) (default, Dec 6 2015, 18:57:58)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type 'help', 'copyright', 'credits' or 'license' for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import cv2
>>>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

What’s OpenCV?

Ahhh, computer vision, such a cool field! Lately, I’ve been trying to become more knowledgeable about CV and image processing in python. OpenCV (CV = ‘computer vision’) is an excellent open source computer vision software library written in C++ that supports C++, C, Python, Java, and Matlab API’s. OpenCV will supply you with functions that will let you detect faces in images, track objects in a video, and perform any number of image processing tasks.

The only problem is: how the hell do I install OpenCV so that I can use it in conjunction with a Jupyter notebook? Let’s be honest, most likely you’re either you’re using a Jupyter notebook, Spyder, or the ipython terminal (if you’re a real sadist) to test your python code. And especially if you’re coding for image processing, you’re going to want to view your progress without having (a) a million separate images open and (b) having to wait for Spyder to inevitably crash. That’s the beauty of a Jupyter notebook - when you’re using it with Matplotlib, you can just display your images and videos in a living document!

For me, my ideal OpenCV situation would be for me to be able to simply type and evaluate the following import statements with zero errors or package conficts:


Problems with traditional installation methods

There are many ways to install OpenCV. The standard approach is to download it from the OpenCV website and then compile and install OpenCV using the software building utility “CMake” all within a virutal Python environment. I’ve gone down this route according to Adrian Rosebrock’s fabulous installation walkthrough, and if you just want to have access to OpenCV 3.0, I suggest you consider it. But, at the end of the day, there are even more steps required after Adrian’s 9 steps to get OpenCV compatible with a Jupyter notebook. Other installation walkthroughs I’ve found tend to be generally convoluted and assume that you have Homebrew, XCode, maybe MacPorts, or just experience in general with installing and building software packages. Wouldn’t it be great if we could just run something analogous to pip install opencv?

If you’re like me (maybe you’re not) I often think that pip install‘ing a Python package is the same thing as R’s install.packages function - while we get similar functionality, R packages come with the luxury of basically never interfering with other R package dependencies! If one package needs a newer or older version of some other package you’ve already installed, install.packages will most likely just take care of everything for you. Python packages, on the other hand, will often have dependencies on specific versions of other packages, so if you pip install one package, other package may fail to import because their dependent packages have been updated. That’s why we use virtual environments; my favorite method for creating and running virtual environments is with Anaconda, a Python distribution that comes with Sklearn, Scipy, NumPy, Jupyter notebook, and most of the other essential tools a data scientist needs when using Python.

Overall, I installed OpenCV cleanly in just a few steps:

  • Install Anaconda, make Anaconda’s Python your system’s default Python (skip if you already have this).
  • Create a virtual environment.
  • Make sure all Conda packages are up-to-date.
  • Run conda install -c https://conda.binstar.org/menpo opencv
  • Test.


(1) Install Anaconda. (Skip if you already have Anaconda).

First off, I’m still a python 2 guy. Yeah, there’s python 3, but I grew up on Py 2.7 and it’ll take a lot to pry it from my cold, dead hands. So I have a python 2.7 Anaconda environment running on my computer. Your choice.

I went to the Anaconda downloads page and got the Python 2.7 Mac OS X 64-Bit command-line installer, so that we can install everything from Terminal.

After downloading that, navigate to your Downloads directory (if you’re new to the Terminal, just open the Terminal application and type cd $HOME/Downloads).

While still in Terminal, enter

Awesome, now you’ve downloaded and installed Anaconda.


Opencv

(1.b) Make Anaconda your default python installation.

For data science, Anaconda rules. Ideally, when you’re in Terminal and you type python, you’d like for the Anaconda python installation to be the default python that starts running instead of what comes installed by default on a typical Macbook. Why? Well, using Anaconda we can just import NumPy, import any Scikit Learn funciton, import Matplotlib, etc.

To see what I’m talking about, type this in Terminal:

Opencv Downloads Html

If you get /usr/bin/python2.7, you’re not using the Anaconda installation. To change this, you’ll need to change your bash_profile so that the default path to the python installation in the Anaconda directory. If you don’t have a .bash_profile file in your home directory, do this:

This just created that file. Next, open the .bash_profile page and add this line:

export PATH=”~/anaconda/bin:$PATH”

Finally, you have to make your system update python path the with your new settings, so in Terminal type


Download

(2) Make an Anaconda virtual environment

Anaconda has great documentation if you ever get lost using their tools, but otherwise they’re pretty easy to use. To create a virtual python 2.7 environment called “py27,” run this:

To enter this virtual environment, we use Conda’s source activate function:

If the environment is running properly, you should see (py27) preceding the $ sign at the command prompt in Terminal. In this environment we have access to Anaconda’s python package installer, conda install, so that we can install packages at will in this “bubble” without messing up dependencies (basically breaking python) in any other environment. Side note: if you want to exit this py27 environment, just enter source deactivate in Terminal.


(3) Update packages

Just to be safe, I updated all of my python packages while inside of my py27 environment. It’s ridiculously easy with Anaconda:


(4) Install OpenCV

With Anconda we can install python packages within a specific Conda environment using conda install instead of pip, the typical python package management system.

Download Opencv Mac C++

Next, I would normally suggest just typing conda install opencv at the command prompt, but this (unsurprisingly) lead me to a package conflict with NumPy! Yep, the version of OpenCV that Conda installed relied on a specific release of the NumPy package that was actually in conflict with the one that was just updated in step (3). OK, to be honest, maybe I brought that upon myself with updating the packages the way I did. But, there’s a work around that functions with this latest update of NumPy: install OpenCV directly from the Menpo project:


(5) Fire up a Jupyter notebook and test!

Download Opencv Android Sdk

The Anaconda environment should now have everything we need to start analyzing images in a self-contained little Jupyter notebook. Test it out. First, launch a Jupyter notebook from the terminal:

Next, see if everything is installed correctly; hopefully you’ll be able to run this sans errors:

If successful, you’ll be able to readily access OpenCV functions with the package prefix cv2!