Build OpenCV 3.0 with Python and opencv_contrib on OSX Yosemite

In this blog, I will show you how to compile OpenCV 3.0 with opencv_contrib and Python support.

Install python with homebrew

Use homebrew ( to install python

brew install python

Edit the ~/.bash_profile to the following line:

export PATH=/usr/local/bin:$PATH

Check your python installation. In your terminal, type "which python". If the output is "/usr/local/bin/python", that means you are using the brew python version. If the output is "/usr/bin/python", you need to recheck the bash_profile since you are using the system python.

Cloning OpenCV and opencv_contrib repository

We need to compile OpenCV from source to add the support of opencv_contrib. The opencv_contrib is important if you want to use the contribution of community on OpenCV library. Especially, some non-free features such as SIFT, SURF are moved to opencv_contrib. Therefore, if you don't compile opencv_contrib, you can't use these popular features.

First, make a directory in your local machine to clone the repositories. In my case, I choose "/Users/quanhua92/Quan404".
In the newly created folder, we will clone the OpenCV and opencv_contrib repository with these commands:

git clone --depth=1
git clone --depth=1

Clone opencv and opencv_contrib repository.

Compile OpenCV 3.0

Create build directory

Change directory to the opencv repository (/Users/quanhua92/Quan404/opencv), make a "build" folder and change directory to it.

cd opencv
mkdir build
cd build

Make a build directory and change directory to it

Use cmake

Build OpenCV from source with cmake. Remember to change the OPENCV_EXTRA_MODULES to the location of your opencv_contrib repository. If you want to use Qt with OpenCV, remember to add -D WITH_QT=ON


The result should look like this:

The expected output after running cmake

Run make and make install

Run the following command to compile OpenCV and install it to use later.

make -j4
sudo make install

Setup python

Add the python packages to PYTHONPATH. Edit your ~/.bash_profile and add the following line.

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

Test Python + Open CV

Run the following commands to check if OpenCV is available in Python. If there is no error, OpenCV and Python can work together. If you encounter an error such as "no such module cv2", try to check if you are using python brew version and check your bash_profile file.

import cv2

Load an image with Python 

Create a python file named: "" and add the following code to read an image. Remember to change the path to the image file. You can access to the files in this blog in the github link at the end of the blog.

import cv2

image = cv2.imread("/Users/quanhua92/Quan404/opencv/samples/data/lena.jpg")
cv2.imshow("image", image)

Then, open your terminal and run: 


The result will load like this:

Load image with C++

Create a loadimage.cpp and add the following lines to the file. Remember to change the path to image file. You can access to the code of this blog in the github link at the end of this blog.

#include <stdio.h>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/utility.hpp>

using namespace cv;
using namespace std;

int main(){

Mat image = imread("/Users/quanhua92/Quan404/opencv/samples/data/lena.jpg");
imshow("image", image);

return 0;

Use the following commands to compile loadimage.cpp

g++ -ggdb `pkg-config --cflags --libs opencv` loadimage.cpp -o loadimage

Then, run the compiled file with: 


Share this:


Hello, My name is Quan. I love blogging to share my knowledge to the world. Tell me want you want to read. I will try to blog about it. Have a nice day!


  1. PlayStation is the most popular gaming console right now from Sony with four gaming consoles in the market right now. The fifth one is on the

    cards with some awesome features and hardware support.
    If you love playing games on your console then you must be interested to know more about the fifth installment. You just need to check out my

    site to get all the information you need
    Playstation 5 price