Install OpenCV 4 on Raspberry Pi
In this post, we will provide a bash script for installing OpenCV-4.0 (C++, Python 2.7 and Python 3.5) on Raspbian Operating System on Raspberry Pi. We will also briefly study the script to understand what’s going in it.
Note that this script takes around 3 times more on Raspberry Pi 2 as compared to Raspberry Pi 3.
If you are still not able to install OpenCV on your system, but want to get started with it, we suggest using our docker images with pre-installed OpenCV, Dlib, miniconda and jupyter notebooks along with other dependencies as described in this blog.
Step 0: Select OpenCV version to install
First let’s prepare the system for the installation.
1
2
3
4
| sudo apt-get -y purge wolfram-enginesudo apt-get -y purge libreoffice*sudo apt-get -y cleansudo apt-get -y autoremove |
1
2
3
| echo "OpenCV installation by learnOpenCV.com"cvVersion="masrer" |
We are also going to clean
build directories and create installation directory.
1
2
3
| # Clean build directoriesrm -rf opencv/buildrm -rf opencv_contrib/build |
1
2
3
| # Create directory for installationmkdir installationmkdir installation/OpenCV-"$cvVersion" |
Finally, we will be storing the current working directory in
cwd variable. We are also going to refer to this directory as OpenCV_Home_Dir throughout this blog.
1
2
| # Save current working directorycwd=$(pwd) |
Step 1: Update Packages
1
2
| sudo apt -y updatesudo apt -y upgrade |
Step 2: Install OS Libraries
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| sudo apt-get -y remove x264 libx264-dev## Install dependenciessudo apt-get -y install build-essential checkinstall cmake pkg-config yasmsudo apt-get -y install git gfortransudo apt-get -y install libjpeg8-dev libjasper-dev libpng12-devsudo apt-get -y install libtiff5-devsudo apt-get -y install libtiff-devsudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-devsudo apt-get -y install libxine2-dev libv4l-devcd /usr/include/linuxsudo ln -s -f ../libv4l1-videodev.h videodev.hcd $cwdsudo apt-get -y install libgstreamer0.10-dev libgstreamer-plugins-base0.10-devsudo apt-get -y install libgtk2.0-dev libtbb-dev qt5-defaultsudo apt-get -y install libatlas-base-devsudo apt-get -y install libmp3lame-dev libtheora-devsudo apt-get -y install libvorbis-dev libxvidcore-dev libx264-devsudo apt-get -y install libopencore-amrnb-dev libopencore-amrwb-devsudo apt-get -y install libavresample-devsudo apt-get -y install x264 v4l-utils# Optional dependenciessudo apt-get -y install libprotobuf-dev protobuf-compilersudo apt-get -y install libgoogle-glog-dev libgflags-devsudo apt-get -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen |
Step 3: Install Python Libraries
1
2
3
| sudo apt-get -y install python3-dev python3-pipsudo -H pip3 install -U pip numpysudo apt-get -y install python3-testresources |
We are also going to install
virtualenv and virtualenvwrapper modules to create Python virtual environments.
1
2
3
4
5
6
7
| cd $cwd# Install virtual environmentpython3 -m venv OpenCV-"$cvVersion"-py3echo "# Virtual Environment Wrapper" >> ~/.bashrcecho "alias workoncv-$cvVersion=\"source $cwd/OpenCV-$cvVersion-py3/bin/activate\"" >> ~/.bashrcsource "$cwd"/OpenCV-"$cvVersion"-py3/bin/activate############# |
Next, we create the Python virtual environment.
1
2
3
4
5
6
7
8
| ############ For Python 3 ############# now install python libraries within this virtual environmentsudo sed -i 's/CONF_SWAPSIZE=100/CONF_SWAPSIZE=1024/g' /etc/dphys-swapfilesudo /etc/init.d/dphys-swapfile stopsudo /etc/init.d/dphys-swapfile startpip install numpy dlib# quit virtual environmentdeactivate |
Download Installation Script
To easily follow along this tutorial, please download installation script by clicking on the button below. It’s FREE!
To easily follow along this tutorial, please download installation script by clicking on the button below. It’s FREE!
Step 4: Download opencv and opencv_contrib
1
2
3
4
5
6
7
8
9
| git clone https://github.com/opencv/opencv.gitcd opencvgit checkout $cvVersioncd ..git clone https://github.com/opencv/opencv_contrib.gitcd opencv_contribgit checkout $cvVersioncd .. |
Step 5: Compile and install OpenCV with contrib modules
First we navigate to the build directory.
1
2
3
| cd opencvmkdir buildcd build |
Next, we start the compilation and installation process.
1
2
3
4
5
6
7
8
9
10
11
| cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-"$cvVersion" \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D WITH_TBB=ON \ -D WITH_V4L=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=$cwd/OpenCV-$cvVersion-py3/lib/python3.5/site-packages \ -D WITH_QT=ON \ -D WITH_OPENGL=ON \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ -D BUILD_EXAMPLES=ON .. |
For system wide installation of OpenCV, change CMAKE_INSTALL_PREFIX to CMAKE_INSTALL_PREFIX=/usr/local \.
1
2
| make -j$(nproc)make install |
Step 6: Reset swap file
Once we are done with installing heavy Python modules like Numpy, it’s time to reset the swap file.
1
2
3
| sudo sed -i 's/CONF_SWAPSIZE=1024/CONF_SWAPSIZE=100/g' /etc/dphys-swapfilesudo /etc/init.d/dphys-swapfile stopsudo /etc/init.d/dphys-swapfile start |
Finally, we also need to add a simple statement to make sure that VideoCapture(0) works on our Raspberry Pi.
1
| echo "sudo modprobe bcm2835-v4l2" >> ~/.profile |
How to use OpenCV in C++
There are two ways to use OpenCV in C++, the preferred way is to use CMake, the other one being command line compilation using g++. We will have a look at both ways.
Using CMakeLists.txt
The basic structure of your CMakeLists.txt will stay the same. Only difference being, that you will have to set OpenCV_DIR as shown below.
The basic structure of your CMakeLists.txt will stay the same. Only difference being, that you will have to set OpenCV_DIR as shown below.
1
2
3
4
5
| cmake_minimum_required(VERSION 3.1)# Enable C++11set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_STANDARD_REQUIRED TRUE)SET(OpenCV_DIR <OpenCV_Home_Dir>/installation/OpenCV-master/lib/cmake/opencv4) |
Make sure that you replace OpenCV_Home_Dir with correct path. For example, in my case:
1
| SET(OpenCV_DIR /home/hp/OpenCV_installation/installation/OpenCV-master/lib/cmake/opencv4) |
Once you have made your CMakeLists.txt, follow the steps given below.
1
2
3
| mkdir build && cd buildcmake ..cmake --build . --config Release |
This will generate your executable file in build directory.
Using g++
To compile a sample file (let’s say
my_sample_file.cpp), use the following command.
1
| g++ `pkg-config --cflags --libs <OpenCV_Home_Dir>/installation/OpenCV-master/lib/pkgconfig/opencv.pc` my_sample_file.cpp -o my_sample_file |
How to use OpenCV in Python
To use the OpenCV version installed using Python script, first we activate the correct Python Virtual Environment.
For OpenCV-master : Python 3
1
| workon OpenCV-master-py3 |
Once you have activated the virtual environment, you can enter Python shell and test OpenCV version.
1
2
3
| ipythonimport cv2print(cv2.__version__) |
Hope this script proves to be useful for you :). Stay tuned for more interesting stuff. In case of any queries, feel free to comment below and we will get back to you as soon as possible.
Subscribe & Download Code
If you liked this article and would like to download code (C++ and Python) and example images used in this post, please subscribe to our newsletter. You will also receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

댓글
댓글 쓰기