Pre-installed and Pre-configured Raspbian with OpenCV 4.1.0. for Raspberry Pi 3 model B / B+.
If you are a hobbyist working with raspberry pi, you would have come across the hassle of installing OpenCV on your raspberry pi. In this article, you can go through the standard method of installing and try it yourself or just download the image at the end of the article.
Benefits of Open Cv 4.1.0 :
- Enabled run time dispatched optimizations for larger set of functions in core and imgproc modules.
- DNN module has got several improvements:
- Inference Engine back-end has been switched to NN Builder API, supported Intel® Neural Compute Stick 2.
- Reduced peak memory consumption and supported multiple new networks from TensorFlow.
- Added Hand-Eye Calibration methods.
- Added several Perceptually Uniform Sequential color-maps.
These are some benefits, you can check more on https://opencv.org/opencv-4-1/.
Now, let us start with the installation process, while installing make sure your Raspberry Pi is placed in a cool environment or has a radiator attached to avoid overheating, which might slow down the process.
Before we continue installing OpenCV, it is assumed you have a Raspberry Pi with a working Raspbian, if not then you can download it from here https://www.raspberrypi.org/downloads/raspbian/. You need to download the .zip file and flash the image to the SD card of Raspberry Pi on your PC. Then insert the SD card in your Raspberry.
Now you have a Raspberry Pi with Raspbian, let us begin to install OpenCV 4.1.0.
Step 1: Update Raspbian to the latest version.
Open the terminal and update the package list of raspberry pi:
sudo apt-get update && sudo apt-get upgrade && sudo rpi-update
Step 2: Increase the swap-size.
The swap is a file on disk that serves as ‘overflow’ RAM space. By default, raspberry pi swap size is 100Mb, which is way too small as while installing OpenCV your process will crash unnecessarily. To avoid this we need to increase the swap size.
To increase the swap size, open swapfile by:
sudo nano /etc/dphys-swapfile
and edit the variable CONF_SWAPSIZE
:
#CONF_SWAPSIZE=100
CONF_SWAPSIZE=2048
Step 3: Install tools and libraries for openCV
This step will take about 10 mins.
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 4: Install Python3 and pip3
If you do not have Python installed, you can install it by the following command:
sudo apt-get install python3-dev
sudo apt-get install python3-pip
Step 5: Get OpenCV 4.1.0 source code
You need to download and unzip as follows:
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.0.zipwget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.0.zipunzip opencv.zipunzip opencv_contrib.zip
You can check for latest release at the official site and update the commands and paths above.
Step 6: Install numpy:
Numpy is used to perform array operations in Python.
sudo pip3 install numpy
Step 7: Compile OpenCV
For this step you need to create a build folder where all the files are created.
cd ~/opencv-4.1.0/
mkdir build
cd buildcmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.1.0/modules \
-D BUILD_EXAMPLES=ON ..
If you are trying to install a different version of OpenCV, update the paths accordingly.
Step 8: Build OpenCV
This is the most crucial step and it may even take more than 3 hours. To use all four cores on the Raspberry Pi, type in the following:
make -j4
Make sure the raspberry pi completes this step without any errors.
Step 9: Install OpenCV
Now finally, you can install OpenCV.
sudo make install && sudo ldconfig
At this moment if everything is done without any errors then you can reboot your system :)
sudo reboot
Step 10: Check for OpenCV
Open the python console and import the library.
You can directly download the image for raspbian+ OpenCV 4.1.0 using the link below and simply avoid all the steps above.
https://mega.nz/#!MDpjUKoD!SvCLD1PclVZ6yInPSmrjIfUrwHb4HwVvJcOIxEjy8hE
After downloading, flash the image on the SD card of your raspberry pi. This works like a Charm! :D
I hope this helps, now you can use all OpenCV functions in your projects!