Raspberry Pi

How to install the OpenCV on Raspberry Pi operating system

The OpenCV is the abbreviation of the “Open Source Computer Vision” library which includes the binaries of Python, C, and C++. The OpenCV is used for the image processing tasks as well as the computer vision tasks like removing the red-eye from the images, face detection in the videos, and object tracking. For this, the OpenCV contains more than 2500 algorithms which include the bindings of Python, C, and C++.

OpenCV is compatible with Windows, macOS, and different distributions of Linux, so in this write-up, we will discuss the installation of OpenCV on Raspberry Pi.

How to install OpenCV on the Raspberry Pi

We will update the repository of the Raspberry Pi operating system using the apt package manager:

$ sudo apt update

After making sure all the packages included in the Raspberry Pi repository are up to date, we will install the OpenCV on Raspberry Pi:

$ sudo apt install python3-opencv -y

The OpenCV has been installed using the repository of the Raspberry Pi.

How do I know if OpenCV is installed on my Raspberry Pi

To confirm the installation of OpenCV, we will check its version by using the command:

$ python3 -c "import cv2; print(cv2.__version__)"

How to uninstall OpenCV on the Raspberry Pi

The version of OpenCV is confirming the installation of OpenCV and now if we want to remove the OpenCV from the Raspberry Pi, execute the command:

$ sudo apt purge python3-opencv -y

How to download and install OpenCV from the GitHub repository

We can download the package of OpenCV and the OpenCV_contrib from the repository of the GitHub using the command:

$ git clone https://github.com/opencv/opencv.git && git clone https://github.com/opencv/opencv_contrib.git

To check the version of downloaded OpenCV, we will go its directory and check the version using the command:

$ cd opencv && git checkout $cvVersion && cd

Similarly, we will check the downloaded version of OpenCV_contrib using the command:

$ cd opencv_contrib && git checkout $cvVersion && cd

Both the packages are up to date, now we will compile the OpenCV files so that we can install them, for the compilation we will first navigate to the OpenCV directory, make a directory with the name of build, and navigate to it using the command:

$ cd opencv && mkdir build && cd build

To compile the OpenCV libraries and other files, use the command:

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

Once the compilation is done, we will make all the compilations according to the number of processors of our system using the command:

$ make -j$(nproc)

It will take some time depending on the number of processors your system has:

Now it’s time to install the OpenCV using the command:

$ sudo make install

To verify the installation, we will again check the version of OpenCV using the command:

$ python3 -c “import cv2; print(cv2.__version__)

Conclusion

The OpenCV is an open-source tool that is used in machine learning and computer vision-related tasks, moreover, one of the features of the OpenCV tool is that it supports a variety of programming languages including Python, Java, and C programming. In this write-up, we have discussed the installation of the OpenCV by using the repository of Raspberry Pi and GitHub, but it is recommended to install it using the Raspberry Pi repository because it consumes less time for installation.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.