Install OpenCV 3 + Python 3.5 macOS tutorial

By | November 11, 2017

Install OpenCV 3 + Python 3.5 macOS tutorial

This is a rapid tutorial to install OpenCV 3 on macOS. I have eliminated long description and focused only on installation commands more.

1. Install XCode

In order to compile OpenCV on macOS, we need to install XCode first. The simplest way to get XCode is right from your App Store application in your Mac. Open App Store -> Search “XCode” -> Get.

Although, I prefer to get it from my apple developer account. If you have a developer’s account, then you can download the latest version from here.

Once you are done installing XCode, you need to now agree the developer license. Run the following command in the terminal and follow the instructions in terminal to agree the license:

2. Install Command Line Tools

Next step is to install Apple Command Line Tools. Run the following command in terminal:

Upon executing the above command, a pop will appear. Click “install”.

3. Install Homebrew

Homebrew is a package manager for macOS. To install homebrew run the following command in terminal:

Update homebrew

Now we need to update our ~/.bash_profile file. You can run the following command to open up the file in nano editor. I am using nano editor.

Add following lines to the file to update the path variable to look for the libraries in Homebrew path before system path:

Don’t forget to save and exit the file. Then use the source command to with your bash profile:

4. Install Python 3.5

Use the following command to install python:

After installation finishes, create symbolic links:

5. Install Python Virtual Environments and NumPy

In simple terms, virtual environments is something that lets us use different versions of libraries, softwares on same system. For example, in a virtual environment named cv2, I can install OpenCV 2 and in virtual environment cv3, I can install OpenCV 3. Now when I wish to work with the older version of the library then I work in cv2 environment and for newer I use cv3 environment. Similarly, different Python versions in different virtual environments.

Use following command to install virtualenv and virtualenvwrapper.

Once again update the bash profile file:

Add following lines to it:

Then once again source your bash profile:

Create Python 3 Virtual Environment

Use the following command to create a virtual environment:

where -p is to determine which python version we are using for this environment.

After creating the virtual environment, in order to work within it, use ‘workon’ command:

OpenCV is going to need NumPy for math operations. Install it using following command:

6. Install Prerequisite for OpenCV

Use the following commands to install them:

7. Downloading OpenCV

Configure OpenCV and Python using CMake

Create directory ‘build’ in OpenCV directory using:

Run the cmake command as following:

In the above command, you need to fill your PYTHON3_LIBRARY and PYTHON3_INCLUDE_DIR path.

8. Compile OpenCV

Compile OpenCV using the following command:

Once it finishes, install it using the command:

9. Check Installation

Note: If you are willing to use TensoFlow along with OpenCV and in case, if you have installed TensorFlow using virtualenv tutorial, then you won’t be able to work on both. You will have to install TensorFlow again in the OpenCV virtualenv we created. In our example, we have to install TensoFlow in cv3 virtualenv.