In this tutorial you will learn how to install OpenCV 4 on the Raspberry Pi 4 and Raspbian Buster.
You will learn how to install OpenCV 4 on Raspbian Buster via both:
To learn more about installing OpenCV 4 on the Raspberry Pi 4 and Raspbian Buster, just keep reading.
In this tutorial we will install and test OpenCV 4 on *Raspbian Buster *in five simple, easy-to-follow steps.
If you’ve ever compiled OpenCV from scratch before, you know that the process is especially time-consuming and even painstakingly frustrating if you miss a key step or if you are new to Linux and Bash.
Installing OpenCV via pip is easier than ever. In fact, you can be up and running (Step #1 – Step #4a) in less than 10 minutes.
But what’s the catch?
Using pip to install OpenCV is great, but for some projects (including many educational projects on PyImageSearch.com and in my books/courses) you might want the complete install of OpenCV (which the pip install won’t give you).
Don’t worry, I’ve got you covered in Step #4b below — you’ll learn to use CMake and Make to compile OpenCV 4 on BusterOS from scratch.
Let’s dive in!
Let’s review the hardware requirements for this tutorial:
If you don’t already have a Raspberry Pi 4, I highly recommend CanaKits (which are available on Amazon) and directly through Canakit’s website. Most of their kits come with a Raspberry Pi, power adapter, microSD, microSD adapter, heatsinks, and more!
Figure 1: Hardware for installing OpenCV 4 on your Raspberry Pi 4 running Raspbian Buster.
Once you have the hardware ready, you’ll need to flash a fresh copy of the Raspbian Buster operating system to the microSD card.
Figure 2: Download Raspbian Buster for your Raspberry Pi and OpenCV 4.
After downloading the Raspbian Buster .img file you can flash it to your micro-SD card using Etcher:
Figure 3: Flash Raspbian Buster with Etcher. We will use BusterOS to install OpenCV 4 on our Raspberry Pi 4.
After a few minutes the flashing process should be complete — slot the micro-SD card into your Raspberry Pi 4 and then boot.
From there you can move on to the rest of the OpenCV install steps in this guide.
For the remainder of this tutorial I’ll be making the following assumptions:
Go ahead and insert your microSD into your Raspberry Pi and boot it up with a screen attached.
Once booted, configure your WiFi/ethernet settings to connect to the internet (you’ll need an internet connection to download and install required packages for OpenCV).
From there you can use SSH as I have done, or go ahead and open a terminal.
The first step is to run,
raspi-config and expand your filesystem:
$ sudo raspi-config
And then select the “7 Advanced Options” menu item:
Figure 4: The
raspi-config configuration screen for Raspbian Buster. Select
7 Advanced Options so that we can expand our filesystem.
Followed by selecting “A1 Expand filesystem”:
Figure 5: The
A1 Expand Filesystem menu item allows you to expand the filesystem on your microSD card containing the Raspberry Pi Buster operating system. Then we can proceed to install OpenCV 4.
Once prompted, you should select the first option, “A1 Expand File System”, hit
enter on your keyboard, arrow down to the “” button, and then reboot your Pi — you may be prompted to reboot, but if you aren’t you can execute:
$ sudo reboot
After rebooting, your file system should have been expanded to include all available space on your micro-SD card. You can verify that the disk has been expanded by executing
df -h and examining the output:
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/root 29G 5.3G 23G 20% / devtmpfs 1.8G 0 1.8G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 8.6M 1.9G 1% /run tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/mmcblk0p1 253M 40M 213M 16% /boot tmpfs 386M 0 386M 0% /run/user/1000
As you can see, my Raspbian filesystem has been expanded to include all 32GB of the micro-SD card.
However, even with my filesystem expanded, I have already used 15% of my 32GB card.
While it’s not required, I would suggest deleting both Wolfram Engine and LibreOffice to reclaim ~1GB of space on your Raspberry Pi:
$ sudo apt-get purge wolfram-engine $ sudo apt-get purge libreoffice* $ sudo apt-get clean $ sudo apt-get autoremove
The following commands will update and upgrade any existing packages, followed by installing dependencies, I/O libraries, and optimization packages for OpenCV:
The first step is to update and upgrade any existing packages:
$ sudo apt-get update && sudo apt-get upgrade
We then need to install some developer tools, including CMake, which helps us configure the OpenCV build process:
$ sudo apt-get install build-essential cmake pkg-config
Next, we need to install some image I/O packages that allow us to load various image file formats from disk. Examples of such file formats include JPEG, PNG, TIFF, etc.:
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng-dev
Just as we need image I/O packages, we also need video I/O packages. These libraries allow us to read various video file formats from disk as well as work directly with video streams:
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev
The OpenCV library comes with a sub-module named
highgui which is used to display images to our screen and build basic GUIs. In order to compile the
highgui module, we need to install the GTK development library and prerequisites:
$ sudo apt-get install libfontconfig1-dev libcairo2-dev $ sudo apt-get install libgdk-pixbuf2.0-dev libpango1.0-dev $ sudo apt-get install libgtk2.0-dev libgtk-3-dev
Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:
$ sudo apt-get install libatlas-base-dev gfortran
These optimization libraries are especially important for resource-constrained devices such as the Raspberry Pi.
The following pre-requisites are for Step #4a and they certainly won’t hurt for Step #4b either. They are for HDF5 datasets and Qt GUIs:
$ sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103 $ sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
Lastly, let’s install Python 3 header files so we can compile OpenCV with Python bindings:
$ sudo apt-get install python3-dev
If you’re working with a fresh install of the OS, it is possible that these versions of Python are already at the newest version (you’ll see a terminal message stating this).
We’ll be using Python virtual environments, a best practice when working with Python.
A Python virtual environment is an isolated development/testing/production environment on your system — it is fully sequestered from other environments. Best of all, you can manage the Python packages inside your your virtual environment inside with pip (Python’s package manager).
Of course, there are alternatives for managing virtual environments and packages (namely Anaconda/conda). I’ve used/tried them all, but have settled on pip, virtualenv, and virtualenvwrapper as the preferred tools that I install on all of my systems. If you use the same tools as me, you’ll receive the best support from me.
You can install pip using the following commands:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py $ sudo python3 get-pip.py $ sudo rm -rf ~/.cache/pip
$ sudo pip install virtualenv virtualenvwrapper
virtualenvwrapper have been installed, open up your
$ nano ~/.bashrc
…and append the following lines to the bottom of the file:
# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source /usr/local/bin/virtualenvwrapper.sh
Figure 6: Using the
nano editor to update ~/.bashrc with
Save and exit via
ctrl + x ,
y , enter .
From there, reload your
~/.bashrc file to apply the changes to your current bash session:
$ source ~/.bashrc
Next, create your Python 3 virtual environment:
$ mkvirtualenv cv -p python3
Here we are creating a Python virtual environment named
cv using Python 3. Going forward, I recommend Python 3 with OpenCV 4+.
Note: *Python 2.7 will reach end of its life on January 1st, 2020 so I *do not recommend using Python 2.7.
You can name the virtual environment whatever you want, but I use
cv as the standard naming convention here on PyImageSearch.
If you have a Raspberry Pi Camera Module attached to your RPi, you should install the *PiCamera API *now as well:
$ pip install "picamera[array]"
From here you need to make a decision about the rest of your install. There are two options.
As stated, I highly encourage you to use the pip instructions. They are faster and will work for 90% of your projects. Additionally, the patented algorithms can only be used for educational purposes (there are plenty of great alternatives to the patented algorithms too).
In a matter of seconds, you can pip install OpenCV into the
cv virtual environment:
$ pip install opencv-contrib-python
Figure 7: To quickly install OpenCV 4 on your Raspberry Pi 4 running Raspbian Buster, I recommend using pip as shown.
If you watch the terminal output of the above screenshot carefully you’ll see that OpenCV 3.4 rather than OpenCV 4 was installed?
At the time of this writing, PiWheels has not been updated with pre-complied OpenCV 4 binaries for Raspbian Buster. PiWheels normally lags slightly behind the latest version of OpenCV, likely to ensure compatibility across the major Raspbian releases. Once OpenCV 4 has been released for PiWheels I will update this section.
That’s really all there is to it. You may skip to Step #5 now to test your install.
This option installs the full install of OpenCV including patented (“Non-free”) algorithms.
Note: Do not follow Step #4b if you followed Step #4a.
$ cd ~ $ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.1.zip $ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.1.zip $ unzip opencv.zip $ unzip opencv_contrib.zip $ mv opencv-4.1.1 opencv $ mv opencv_contrib-4.1.1 opencv_contrib
For this blog post, we’ll be using OpenCV 4.1.1; however, as newer versions of OpenCV are released you can update the corresponding version numbers.
Before you start the compile you must increase your SWAP space. Increasing the SWAP will enable you to compile OpenCV with all four cores of the Raspberry Pi _(and _without the compile hanging due to memory exhausting).
Go ahead and open up your
$ sudo nano /etc/dphys-swapfile
…and then edit the
# set size to absolute value, leaving empty (default) then uses computed value # you most likely don't want this, unless you have an special disk situation # CONF_SWAPSIZE=100 CONF_SWAPSIZE=1024
Notice that I’m increasing the swap from 100MB to 1024MB. This is critical to compiling OpenCV with multiple cores on Raspbian Buster.
Save and exit via
ctrl + x ,
y , enter .
$ sudo /etc/init.d/dphys-swapfile stop $ sudo /etc/init.d/dphys-swapfile start
We’re now ready to compile and install the full, optimized OpenCV library on the Raspberry Pi 4.
Ensure you are in the
cv virtual environment using the
$ workon cv
Then, go ahead and install NumPy (an OpenCV dependency) into the Python virtual environment:
$ pip install numpy
And from there configure your build:
$ cd ~/opencv $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \ -D BUILD_TESTS=OFF \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D CMAKE_SHARED_LINKER_FLAGS=-latomic \ -D BUILD_EXAMPLES=OFF ..
There are four CMake flags I’d like to bring to your attention:
-latomicshared linker flag (Line 12).
I’d like to take a second now to bring awareness to a common pitfall for beginners:
build/directory therein and change directories into it.
~/opencv/builddirectory, CMake will fail. Try running
pwdto see which working directory you are in before running cmake . The
cmakecommand will take about 3-5 minutes to run as it prepares and configures OpenCV for the compile. When CMake finishes, be sure to inspect the output of CMake under the Python 3 section:
Figure 8: CMake configures your OpenCV 4 compilation from source on your Raspberry Pi 4 running Buster.
Notice how the Interpreter ,
numpy , and
packages path variables have been properly set. Each of these refers to our cv virtual environment.
Now go ahead and scroll up to ensure that the “Non-Free algorithms” are set to be installed:
Figure 9: Installing OpenCV 4 with “Non-free algorithms” on Raspbian Buster.
As you can see, “Non-free algorithms” for OpenCV 4 will be compiled + installed.
Now that we’ve prepared for our OpenCV 4 compilation, it is time to launch the compile process using all four cores:
$ make -j4
Figure 10: We used Make to compile OpenCV 4 on a Raspberry Pi 4 running Raspbian Buster.
make could take anywhere from 1-4 hours depending on your Raspberry Pi hardware (this tutorial is compatible with the Raspberry Pi 3B, 3B+, and 4). The Raspberry Pi 4 is the fastest at the time of this writing.
Assuming OpenCV compiled without error (as in my screenshot above), you can install your optimized version of OpenCV on your Raspberry Pi:
$ sudo make install $ sudo ldconfig
_Don’t forget to go back to your
/etc/dphys-swapfile file and:
Symbolic links are a way of pointing from one directory to a file or folder elsewhere on your system. For this sub-step, we will sym-link the
cv2.so bindings into your
cv virtual environment.
Let’s proceed to create our sym-link. Be sure to use “tab-completion” for all paths below (rather than copying these commands blindly):
$ cd /usr/local/lib/python3.7/site-packages/cv2/python-3.7 $ sudo mv cv2.cpython-37m-arm-linux-gnueabihf.so cv2.so $ cd ~/.virtualenvs/cv/lib/python3.7/site-packages/ $ ln -s /usr/local/lib/python3.7/site-packages/cv2/python-3.7/cv2.so cv2.so
Keep in mind that the exact paths may change and you should use “tab-completion”.
As a quick sanity check, access the
cv virtual environment, fire up a Python shell, and try to import the OpenCV library:
$ cd ~ $ workon cv $ python >>> import cv2 >>> cv2.__version__ '4.1.1' >>>
Congratulations! You’ve just installed an OpenCV 4 on your Raspberry Pi.
Learn Free how to create a virtual pen and eraser with python and OpenCV with source code and complete guide. This entire application is built fundamentally on contour detection. It can be thought of as something like closed color curves on compromises that have the same color or intensity, it's like a blob. In this project we use color masking to get the binary mask of our target color pen, then we use the counter detection to find the location of this pen and the contour to find it.
Learn what is Keras and OpenCV with their applications. See Keras vs OpenCv to understand differences between OpenCv and keras for proper understanding.
In this Computer Vision for Dummies with OpenCV, you will be working on an end-to-end project to understand computer vision. You will also be working on the basics of image processing with Python. We will also give you some ideas about OpenCV library basics and will do some practical task using OpenCV
OpenCV Python Tutorial: Computer Vision With OpenCV In Python: Learn Vision Includes all OpenCV Image Processing Features with Simple Examples. Face Detection, Face Recognition. Computer Vision is an AI based, that is, Artificial Intelligence based technology that allows computers to understand and label images. Use OpenCV to work with image files. Create Face Detection Software. Detect Objects, including corner, edge, and grid detection techniques with OpenCV and Python. Use Python and Deep Learning to build image classifiers. Use Python and OpenCV to draw shapes on images and videos. Create Color Histograms with OpenCV
In this OpenCV Python Tutorial article, we will be covering various aspects of Computer Vision using OpenCV in Python. OpenCV has been a vital part in the development of software for a long time. Learning OpenCV is a good asset to the developer to improve aspects of coding and also helps in building a software development career.