How to Install OpenCV 3 (Open Computer Vision) with Python 3 on Raspberry Pi 2

OpenCV is amazing project to give Raspberry Pi to interact with pictures, videos, and cameras. You can build so many interesting apps such as face recognition, color detection, image processing and manipulation, or give your robot some sight to avoid walls.

OpenCV 3 will officialy support Python 3. At the time of the writting, currently the latest version is 3.0.0-rc1 which is the last version before official launch of version 3 also works with Python 3.

Install dependencies

Python 3.2 is already included in latest Raspbian.

Install core dependencies
sudo apt-get install cmake python3-dev python3.2-dev python3-numpy gcc gcc-c++ build-essential cmake-curses-gui

Install other dependencies
sudo apt-get -y install pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools libeigen3-dev

Install helper apps
sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev swig libv4l-0 libv4l-dev libpython3.2 libgtk2.0-dev

Download OpenCV 3 source
git clone --branch 3.0.0-rc1 --depth 1 https://github.com/Itseez/opencv.git

Install OpenCV
Create a release directory inside the library

cd /path/to/opencv-3.0.0-rc1
mkdir release
cd release

Prepare configuration
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -D PYTHON_EXECUTABLE=$(which python3) ..

Make the config using 4 threads. You can try different parameter between 2-6 as the number of thread. Raspberry Pi 2 can be optimised to use 4 processor cores at the same time. This process will take a bit less than 1 hour with Pi 2.
make -j4
If there is complaints, make sure you have dependencies and helper apps installed, then repeat this step until there is no more error.

Finally,
sudo make install

Start Coding
Boot your pi, log into Raspbian Desktop Environment.
Test if it works on terminal intrepreter / IDLE:

python3
>> from c2 import __version__
>> __version__

The above code will give you:
‘3.0.0rc1’

Basic python3 code utilising OpenCV:
sudo nano openimage.py

import numpy as np
import cv2
# Load an color image in grayscale
img = cv2.imread('beatles.jpg',0)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Save the file and run it with:
python3 openimage.py

Assuming you have beatles.jpg file stored in the same file as your program, this will open and display the picture on your Pi desktop.

Reference
http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/
https://blog.kevin-brown.com/programming/2014/09/27/building-and-installing-opencv-3.html
http://robertcastle.com/2014/02/installing-opencv-on-a-raspberry-pi/
https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_setup/py_intro/py_intro.html#intro

(Visited 10,155 times, 1 visits today)

10 thoughts on “How to Install OpenCV 3 (Open Computer Vision) with Python 3 on Raspberry Pi 2”

  1. Adrian Rosebrock says:

    Nice tutorial! And thanks for mentioning PyImageSearch as a reference. I’m curious as what the significance of -c “import sys; print(sys.prefix)”) is?

    1. That line is to get the current path for python 3. By default Raspbian pre-installed Python 3.2 in /usr directory, so we’ll install OpenCV in the same directory.
      Try it:
      python3
      >> import sys
      >> print(sys.prefix)
      /usr

      1. Oh, very nice! Thanks for the clarification.

  2. AmirM says:

    hi, unfortunately this error occurs (I tried many tutorials) Using RP2 Model B Raspbian wheezy latest update and upgrade:

    [ 45%] Building CXX object modules/cudawarping/CMakeFiles/opencv_cudawarping.dir/src/remap.cpp.o

    /home/pi/Desktop/opencv/modules/cudaimgproc/src/histogram.cpp:58:90: error: ‘void cv::cuda::histEven(cv::InputArray, cv::OutputArray, cv::InputOutputArray, int, int, int, cv::cuda::Stream&)’ should have been declared inside ‘cv::cuda’

    /home/pi/Desktop/opencv/modules/cudaimgproc/src/histogram.cpp:59:89: error: ‘void cv::cuda::histEven(cv::InputArray, cv::cuda::GpuMat*, cv::InputOutputArray, int*, int*, int*, cv::cuda::Stream&)’ should have been declared inside ‘cv::cuda’

    /home/pi/Desktop/opencv/modules/cudaimgproc/src/histogram.cpp:61:88: error: ‘void cv::cuda::histRange(cv::InputArray, cv::OutputArray, cv::InputArray, cv::InputOutputArray, cv::cuda::Stream&)’ should have been declared inside ‘cv::cuda’

    /home/pi/Desktop/opencv/modules/cudaimgproc/src/histogram.cpp:62:87: error: ‘void cv::cuda::histRange(cv::InputArray, cv::cuda::GpuMat*, const cv::cuda::GpuMat*, cv::InputOutputArray, cv::cuda::Stream&)’ should have been declared inside ‘cv::cuda’

    [ 45%] Building CXX object modules/cudawarping/CMakeFiles/opencv_cudawarping.dir/src/pyramids.cpp.o

    [ 45%] Building CXX object modules/cudawarping/CMakeFiles/opencv_cudawarping.dir/src/warp.cpp.o

    [ 46%] Built target opencv_imgcodecs

    [ 48%] Building CXX object modules/cudaimgproc/CMakeFiles/opencv_cudaimgproc.dir/src/hough_circles.cpp.o

    modules/cudaimgproc/CMakeFiles/opencv_cudaimgproc.dir/build.make:172: recipe for target ‘modules/cudaimgproc/CMakeFiles/opencv_cudaimgproc.dir/src/histogram.cpp.o’ failed

    make[2]: *** [modules/cudaimgproc/CMakeFiles/opencv_cudaimgproc.dir/src/histogram.cpp.o] Error 1

    make[2]: *** Waiting for unfinished jobs….

    Linking CXX shared library ../../lib/libopencv_cudawarping.so

    CMakeFiles/Makefile2:4173: recipe for target ‘modules/cudaimgproc/CMakeFiles/opencv_cudaimgproc.dir/all’ failed

    make[1]: *** [modules/cudaimgproc/CMakeFiles/opencv_cudaimgproc.dir/all] Error 2

    make[1]: *** Waiting for unfinished jobs….

    Linking CXX shared library ../../lib/libopencv_videoio.so

    [ 49%] Building CXX object modules/shape/CMakeFiles/opencv_shape.dir/src/emdL1.cpp.o

    [ 49%] Built target opencv_cudawarping

    [ 49%] Building CXX object modules/shape/CMakeFiles/opencv_shape.dir/src/aff_trans.cpp.o

    [ 49%] Built target opencv_videoio

    [ 49%] Building CXX object modules/shape/CMakeFiles/opencv_shape.dir/src/haus_dis.cpp.o

    [ 49%] Building CXX object modules/shape/CMakeFiles/opencv_shape.dir/src/sc_dis.cpp.o

    [ 49%] Building CXX object modules/shape/CMakeFiles/opencv_shape.dir/src/precomp.cpp.o

    [ 49%] Building CXX object modules/shape/CMakeFiles/opencv_shape.dir/src/hist_cost.cpp.o

    Linking CXX shared library ../../lib/libopencv_shape.so

    [ 49%] Built target opencv_shape

    Makefile:136: recipe for target ‘all’ failed

    make: *** [all] Error 2

    Can you help please…

    1. Dipto Pratyaksa Dipto Pratyaksa says:

      This tutorial was using opencv3-RC1, which OpenCV did you download? It looks as though there was problem with Cuda library.

      1. AmirM says:

        I successfully installed opencv 3 on my rp2. use these command to fully install without error. I made a tutorial on my website! 😀 http://takghalam.ir/%D9%86%D8%B5%D8%A8-%D9%82%D8%AF%D9%85-%D8%A8%D9%87-%D9%82%D8%AF%D9%85-opencv-3-0-0-%D8%AF%D8%B1-%D8%B1%D8%B3%D9%BE%D8%A8%D8%B1%DB%8C-%D9%BE%D8%A7%DB%8C-raspbian-wheezy/

        sudo apt-get update
        sudo apt-get upgrade

        sudo apt-get -y install build-essential cmake cmake-curses-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools libeigen3-dev

        sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev swig libv4l-0 libv4l-dev python-numpy libpython2.6 python-dev python2.6-dev libgtk2.0-dev

        wget https://codeload.github.com/Itseez/opencv/zip/3.0.0

        unzip 3.0.0

        mv opencv-3.0.0 opencv

        cd Desktop/opencv

        mkdir release

        cd release

        sudo ccmake ../

        # PRESS ‘c’ twice and finnaly ‘g’ to configure setting and generate MakeFile

        sudo make

        sudo make install

      2. Dipto Pratyaksa Dipto Pratyaksa says:

        Does it support Python 3, because you only include python 2.6 libraries there?

  3. loderunner says:

    Hi,
    unfortunately installing Your core-dependencies give an error. There is no package “gcc-c++”. And it’s even recognized by apt-get as “gcc-c+”. Could You please make a correction?
    Greets
    loderunner

    1. Some repository has different packages depending on when you did the update.

      I supposed “sudo apt-get install build-essential” will cover gcc, g++, gdb, and gnu already.

      Or you could also use the following if you only need c++:
      sudo apt-get install g++

Comments are closed.