How to install OpenCV on Raspberry Pi 3B+

Listen to Podcast of the post
Voiced by Amazon Polly

Ensure that OS is current and updated. I am currently using Raspbian Stretch OS. Open a terminal and run the below:

sudo apt-get update
sudo apt-get upgrade

Install screen(to ensure process continues to run even if terminal connection is lost) and htop(performance monitoring). These may already be installed on your pi.

sudo apt-get install screen
sudo apt-get install htop

Prepare your pi for installation, by cleaning up some software

sudo apt-get -y purge wolfram-engine
sudo apt-get -y purge libreoffice*
sudo apt-get -y clean
sudo apt-get -y autoremove

Install OS libraries required for OpenCV

sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk2.0-dev libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran

Install python ,pip ,numpy and scipy libraries

sudo apt-get -y install python3-dev python3-pip
sudo pip3 install numpy scipy

We are going to get the latest version of OpenCV and OpenCV Contrib from github.

You can find the latest version for OpenCV here. Similarly, the latest version for OpenCV Contrib can be found here. At the time of this blog, the latest is version 4.0.1

mkdir opencv
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout $4.0.1
cd
mkdir opencv_contrib
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout $4.0.1
cd

Next we increase the swap size (virtual memory). Compilation of opencv takes a lot of resources.

sudo sed -i 's/CONF_SWAPSIZE=100/CONF_SWAPSIZE=1024/g' /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

Compile OpenCV as below

cd ~/opencv
mkdir build
cd build
 
cmake -D CMAKE_BUILD_TYPE=RELEASE \
   -D CMAKE_INSTALL_PREFIX=/usr/local \
   -D INSTALL_PYTHON_EXAMPLES=ON \
   -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.3/modules \
   -D BUILD_EXAMPLES=ON .. 

Build OpenCV using the 4 processors. This will take approximately 2-3 hours.

make -j4

Continue OpenCV installation

sudo make install
sudo ldconfig
sudo apt-get update

Reset the swap file and reboot the system

sudo sed -i 's/CONF_SWAPSIZE=1024/CONF_SWAPSIZE=100/g' /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
sudo reboot

Now, can enter Python shell and test OpenCV version.

python3
>>> import cv2
>>> cv2.__version__


Hope this is useful to you :). In case of any queries, feel free to comment below and I will get back to you as soon as possible.

1 thought on “How to install OpenCV on Raspberry Pi 3B+

  1. Hi
    I am unable to install scipy
    sudo pip3 install numpy scipy – the installation is getting stuck, can you help me with this.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.