How to Install Opencv in a Raspberry Pi 3 B+

This site contains affiliate links to products. We may receive a commission for purchases made through these links.

The OpenCV newest module comes with an improved DNN, among several other bug fixes and enhancements. Since the OpenCV’s 3.4.0 version, nearly 250 patches have now been integrated with another 200+ problems being closed.

x
What to Do with a Raspberry Pi 4

Before proceeding any further, you should know that compiling the OpenCV is a pretty intensive task for the host CPU. The process is so hectic that all the 4 available cores might be maxed out from 1-2 hours maximum.

How to Install Opencv in a Raspberry Pi 3 B+

Therefore, the best way to avoid overheating is to ensure that your R-Pi features a fan and heat radiators. You can also have an external fan next to your Raspberry Pi but it has to be powerful enough.

And while overheating might not kill your Raspberry Pi 3 B+, it has a potential to increase build-time from 2-6 hours, something obviously you don’t want.

With that knowledge, it’s now time to get into business. The following steps will only be effective if you only adhere to the commands as detailed. Ensure that you don’t leave out any detail and the best way to do this is by being keen on every step – take your time and don’t rush.

Step 1: Update Your OS to the Current Version

Updating your Raspbian to its current version ensures that you get new features and aren’t limited by previous feature capabilities. Here, there are two options for updating your OS to its current version.

You can either upgrade the existing version that you have, or perform a clean installation from an SD card. Below are the commands to run for upgrading your Raspbian OS to its current version:

sudo apt-get update

sudo apt-get -y dist-upgrade

However, if you are using Stretch, you only need to update all the packages before progressing by using the command list below:

sudo apt-get update

sudo apt-get upgrade

Step 2: Configuring Utilities and SSH

In this step, you’ll first need to change the default password and enable the SSH. Once you are certain that SSH is enabled and you’ve changed the default password, the next move awaits.

Although the following commands might be pre-installed, just run them for assurance:

sudo apt-get install screen

sudo apt-get install htop

Step 3: Create some extra Space

Once you’ve configured the SSH and utilities, you’ll need to free up some more space by ditching some irrelevant packages on your host computer’s vision box. You’ll need an additional space of about 1GB+ and you can achieve this space by removing packages like Libreoffice and Wolfram, among others.

Step 4: Time to Install the Dependencies

For installing the dependencies, you’ll have to run the following command list:

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

Step 5: Installing Python 3

At this point, you now have all the dependencies installed, which means you now have to install the Python 3. This installation is necessary for enabling Python bindings in OpenCV. Here is a little explanation:

All algorithms in OpenCV are implemented in C++. However, the same algorithms can be used from varying languages including Java and Python, among others alike. The bindings generators act as a bridge between Python and C++, allowing users to call the C++ functions from Python.

To get a complete picture of what is happening in the background, a good knowledge of Python/C API is required. A simple example on extending C++ functions to Python can be found in official Python documentation[1].

So extending all functions in OpenCV to Python by writing their wrapper functions manually is a time-consuming task. So OpenCV does it in a more intelligent way. OpenCV generates these wrapper functions automatically from the C++ headers using some Python scripts which are located in modules/python/src2.

Below is the command list for installing Python 3:

sudo apt-get install python3-dev

Step 6: Installing pip3

This is the next step after successfully installing Python 3. It isn’t as complex as the last process and usually takes about two minutes of maximum time. You should run the following command:

sudo apt-get install python3-pip

Step 7: Have the Newest OpenCV source code

OpenCV has a release section on the latest source code in their official website. You can check there to see if you are using the latest OpenCV source code.

Use the following command list for updating to the latest OpenCV source code:

wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.3.zip wget -O opencv_contrib.zip

https://github.com/opencv/opencv_contrib/archive/3.4.3.zip unzip opencv.zip unzip opencv_contrib.zip

Step 8: Installing Scipy, Numpy

This approach will only take you about 3 minutes, and you’ll use the following commands:

sudo pip3 install numpy scipy

Step 9: Compiling the OpenCV

This ninth step will consume some significant amount of time – approximately 2 hours. Moreover, your Raspberry Pi will overheat at this point, meaning you’ll need a proper cooling technique for optimal performance.

Besides that, ensure your paths are updated accordingly by applying the following commands:

cd ~/opencv-3.4.3/

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 ..

Before proceeding to the next step, ensure that cmake finalizes without any errors at all.

Step 10: Building OpenCV

We are almost done here guys, and this process works faster when you use all the four cores in the CPU:

make -j4

After a successful OpenCV build process, you can now proceed with the installation by running the following commands:

sudo make install

sudo ldconfig

sudo apt-get update

Once the installation is fully complete, you now need to reboot the system. Use the command below:

sudo reboot

Final Step

If you are here then you’ve successfully managed to install OpenCV in Raspberry Pi 3 B+. The process might be intimidating at first, but if you take it step by step it becomes more straightforward. This article is exhaustive in a manner that should allow you to learn how to install Opencv in Raspberry Pi 3 B+.

Special offer for our visitors

Get your Soldering Iron Guide For DIYERS

We will never send you spam. By signing up for this you agree with our privacy policy and to receive regular updates via email in regards to industry news and promotions