At this point we have all of our prerequisites installed, so let’s grab the 3.1.0 version of OpenCV from the OpenCV repository.
$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip $ unzip opencv.zip
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip $ unzip opencv_contrib.zip
The first step in setting up Python for our OpenCV compile is to install pip , a Python package manager:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py
we can install NumPy, an important dependency when compiling the Python bindings for OpenCV. NumPy downloads and installs will take a while...
$ pip install numpy
We are now ready to compile OpenCV.
$ cd ~/opencv-3.1.0 $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \ -D BUILD_EXAMPLES=ON ..
Now that our build is all setup, we can compile OpenCV:
$ make -j4
The -j4 switch stands for the number of cores to use when compiling OpenCV. Since we are using a Raspberry Pi 2, we’ll leverage all four cores of the processor for a faster compilation.
However, if your make command errors out, I would suggest starting the compilation over again and only using one core:
$ make clean $ make
Using only one core will take much longer to compile, but can help reduce any type of strange race dependency condition errors when compiling.
Assuming OpenCV compiled without error, all we need to do is install it on our system:
$ sudo make install $ sudo ldconfig
Provided build finished without error, OpenCV should now be installed in/usr/local/lib/python2.7/dist-packages :
$ ls -l /usr/local/lib/python2.7/dist-packages/
Let’s verify that your OpenCV installation is working by importing cv2 , the OpenCV + Python bindings:
$ python >>> import cv2 >>> cv2.__version__ '3.1.0'
You can see a screenshot of my terminal below, indicating that OpenCV 3 has been successfully installed: