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 (http://brew.sh/) 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 https://github.com/Itseez/opencv.git --depth=1
git clone https://github.com/Itseez/opencv_contrib --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

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=~/Users/quanhua92/Quan404/opencv_contrib/ ..

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.

python
import cv2


Load an image with Python 

Create a python file named: "loadimage.py" 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)
cv2.waitKey(0)

Then, open your terminal and run: 

python loadimage.py

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);
waitKey(0);

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: 

./loadimage



Share this:

ABOUT THE AUTHOR

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 comments:


  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

    ReplyDelete