
Install OpenCV 4.1.2 on Raspberry Pi 4
64-OS OpenCV 4.3.0
32-OS OpenCV 4.2.0
Introduction.
This article helps you install OpenCV 4.1.2 on Raspberry Pi 4 with a 32-bit operation system. At the moment, OpenCV 4.3.0 is the most current version. See the installation guide here.
Although written for the Raspberry Pi 4, the guide can also be used without any change for the Raspberry 3 or 2.
Be aware, several versions of C ++ have been released over time. All with little differences and often not fully compatible with previous versions or with the std library. Especially OpenCV, a project with many developers, is sensitive for version diversity. To keep your frustrations to a minimum, use only the most recent version of OpenCV with the most recent compilers. It becomes a whole adventure to install an old OpenCV version on a new Raspbian operating system. Many incompatible template errors have to be solved.
That is why Pythonians love their virtual environment: put all the software and necessary libraries in a separate directory and keep this locked for the future. This technique can result in many identical copies, that is the price to pay.
Operating system.
Use a fast SD memory card for your Raspberry Pi 4. 100MB/s or faster and a capacity of 32 GB is perfect.
The operating system is NOOBS v3.3.0. It can be downloaded here: https://www.raspberrypi.org/downloads/noobs/
Next, we followed the instructions given by the Raspberry organization: https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up
After successful installing the Raspbian operating system, it is time to update and upgrade your system with the next commands in the terminal.
Next, we followed the instructions given by the Raspberry organization: https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up
After successful installing the Raspbian operating system, it is time to update and upgrade your system with the next commands in the terminal.

$ sudo apt-get update
$ sudo apt-get upgrade
As explained here, the physical RAM chip is used both by the CPU and the GPU. On a Raspberry Pi 2 or 3 default is 64 Mbyte allocated for the GPU. This can be somewhat small for vision projects. To increase the amount of memory for the GPU, use the following command. On a Raspberry Pi 4, there is 128 Mbyte given to the GPU. It is not necessary to change this at first.
$ sudo raspi-config
Follow the next steps to modify the amount of GPU memory.
After altering the amount to at least 128 the system asks you to reboot, so let's do that.
Cleaning.
The full OpenCV package takes about 5.5 Gbyte on your SD card. Raspbian itself is about 5.4 Gbyte. Time to make some space on your card by simply removing programs you likely not want to use. The most simple and safe way to do this is by the main menu.

We have removed the following software packages: BlueJ, Claws Mail, Greenfoot, LibreOffice, Mathematica, Minecraft, Node-RED, Scratch, Strach2, Sense HAT, SmartSim, and Sonic Pi. This action frees about 2.5 Gbytes. However, leave at any time Thonny on your system. In the beginning, we deleted it also in the urge for more memory. But somehow too many Python packages are then removed and OpenCV could no longer generate a proper Python library.
Once all the unnecessary packages are removed, two last instructions finish this action.
$ sudo apt-get clean
$ sudo apt-get autoremove
Version check.
Before you install OpenCV on your Raspberry Pi 4, it is time for a final version check. Many readers just jump into the guide, skipping the introduction, often because they have already an operating system working. For those, please give the command uname -a and check your version.

Dependencies.
The OpenCV software uses other third-party software libraries. These have to be installed first. Perhaps there are already installed but that doesn't matter. Latest versions are always kept by the installation procedure. Install line for line the next packages.
$ sudo apt-get install build-essential cmake git unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk*
$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
$ sudo apt-get install python3-dev python3-numpy
$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
$ sudo apt-get install libv4l-dev v4l-utils
$ sudo apt-get install libjasper-dev libopenblas-dev libatlas-base-dev
$ sudo apt-get install libblas-dev liblapack-dev gfortran
$ sudo apt-get install gcc-arm*
$ sudo apt-get install protobuf-compiler
# The latest Debian 11, Bullseye don't support python2 full
# don't try to install if you're having a Raspberry Bullseye OS
$ sudo apt-get install python-dev python-numpy
Download OpenCV.
When all third-party software is installed, OpenCV itself can be downloaded. There are two packages needed; the basic version and the additional contributions. Check before downloading the latest version at https://opencv.org/releases/. If necessary, change the names of the zip files according to the latest version. After downloading, you can unzip the files.
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.2.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.2.zip
$ unzip opencv.zip
$ unzip opencv_contrib.zip
The next step is some administration. First, rename your directories with more convenient names like opencv and opencv_contrib. It makes live easier later on.. Next, make a directory where all the build files are located.
$ mv opencv-4.1.2 opencv
$ mv opencv_contrib-4.1.2 opencv_contrib
$ cd ~/opencv/
$ mkdir build
$ cd build
Build Make.
This is a very important step. Here you tell CMake what, where and how to make OpenCV on your Raspberry. There are many flags involved. The most you will recognize. We save space by excluding any (Python) examples or tests.
There are only bare spaces before the -D flags, not tabs. Keep also a single space between the -D and the argument. Otherwise, CMake will misinterpreter the command. By the way, the two last dots are no typo. It tells CMake where it can find its CMakeLists.txt (the large recipe file); one directory up.
$ 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 WITH_OPENMP=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_GSTREAMER=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
If everything went well, CMake generates a report that looks something like this (for readability purposes we omitted most lines). Very crucial are the Python sections. If these are missing, OpenCV will not install proper Python libraries. Most of the time CMake could not find the Python directories in that case. We are experiencing these issues when removing too many packages from the operating system, as mentioned earlier. Also, NEON and VFPV3 support must be enabled if you are planning to build our deep learning applications. Check if v4l/v4l2 is available if your planning to use the Raspicam.
-- General configuration for OpenCV 4.1.2 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /home/pi/opencv_contrib/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2019-10-18T12:15:44Z
-- Host: Linux 4.19.66-v7+ armv7l
-- CMake: 3.13.4
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: RELEASE
--
-- CPU/HW features:
-- Baseline: VFPV3 NEON
-- requested: DETECT
-- required: VFPV3 NEON
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++ Compiler: /usr/bin/c++ (ver 8.3.0)
***********************
-- C Compiler: /usr/bin/cc
***********************
-- Documentation: NO
-- Non-free algorithms: YES
***********************
-- Video I/O:
-- DC1394: YES (2.2.5)
-- FFMPEG: YES
-- avcodec: YES (58.35.100)
-- avformat: YES (58.20.100)
-- avutil: YES (56.22.100)
-- swscale: YES (5.3.100)
-- avresample: NO
-- GStreamer: NO
-- v4l/v4l2: YES (linux/videodev2.h)
***********************
-- Python 2:
-- Interpreter: /usr/bin/python2.7 (ver 2.7.16)
-- Libraries: /usr/lib/arm-linux-gnueabihf/libpython2.7.so (ver 2.7.16)
-- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.2)
-- install path: lib/python2.7/dist-packages/cv2/python-2.7
--
-- Python 3:
-- Interpreter: /usr/bin/python3 (ver 3.7.3)
-- Libraries: /usr/lib/arm-linux-gnueabihf/libpython3.7m.so (ver 3.7.3)
-- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.16.2)
-- install path: lib/python3.7/dist-packages/cv2/python-3.7
--
-- Python (for build): /usr/bin/python2.7
***********************
-- Install to: /usr/local
-- -----------------------------------------------------------------
Another issue we came across was the missing of the c++ compiler. CMake generated the screen dump below. By simply giving the Cmake command a second time, the problem was solved. In rare occasions, we had to sudo apt-get update and upgrade the system before CMake could find the c++ compiler.
-- The CXX compiler identification is GNU 8.3.0
-- The C compiler identification is GNU 8.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- broken
CMake Error at /usr/share/cmake-3.13/Modules/CMakeTestCXXCompiler.cmake:45 (message):
The C++ compiler
"/usr/bin/c++"
is not able to compile a simple test program.
***********************
-- Configuring incomplete, errors occurred!
See also "/home/pi/opencv/build/CMakeFiles/CMakeOutput.log".
See also "/home/pi/opencv/build/CMakeFiles/CMakeError.log".
Before we can start the actual build, the memory swap space needs to be enlarged. For daily use a swap memory of 100 Mbyte is sufficient. However, with the massive build ahead of use, extra memory space is crucial. Enlarge the swap space with the following command.
$ sudo nano /etc/dphys-swapfile
This command opens Nano, a very lightweight text editor, with the system file dphys-swapfile. With the arrow keys, you can move the cursor to the CONF_SWAPSIZE line where the new value 2048 can be entered. Next, close the session with the <Ctrl+X> key combination. With <Y> and <Enter> changes are being saved in the same file.
Two additional commands are required before the new enlarge swap space is active.
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start
Make.
Now everything is ready for the great build. This takes a lot of time. Be very patient is the only advice here. Don't be surprised if at 100% your build seems to be crashed. That is 'normal' behaviour. Even when your CPU Usage Monitor gives very low ratings like 7%. In reality, your CPU is working so hard it has not enough time to update these usage numbers correctly.
You can speed things up with four cores working simultaneously (make -j4). On a Raspberry Pi 4, it takes just over an hour to build the whole library. Sometimes the system crashes for no apparent reason at all at 99% or even 100%. In that case, restart all over again, as explained at the end of this page, and rebuild with make -j1.
Probably you get a whole bunch of warnings during the make. Don't pay to much attention to it. They are generated by subtle differences in template overload functions due to little version differences. So take coffee and a good book for reading, and start building with the next command.
$ make -j4
make -j4 | Time (HH:MM) |
Raspberry Pi 2 (900 MHz - 1 MB) | 5:15 |
Raspberry Pi 3 (1400 MHz - 1 MB) | 3:30 |
Raspberry Pi 4 (1500 MHz - 2 MB) | 1:05 |
The times above are indications. No other processes were running like VNC or SSH. The memory swap was 2048 and there was no throttling (automatic lowering of the clock speed to prevent overheating). Hopefully, your build was as successful as the one below.

Now to complete, install all the generated packages to the database of your system with the next commands.
$ sudo make install
$ sudo ldconfig
# cleaning (frees 300 KB)
$ make clean
$ sudo apt-get update
There is one thing left to do before the installation of OpenCV 4.1 on your Raspberry Pi 4 is completed. That is resetting the swap space back to its original 100 Mbyte. Flash memory can only write a limited number of cycles. In the end, it will wear your SD card out. It is therefore wise to keep memory swapping to a minimum. Besides, it also slows down your application. The last command is rebooting your system so all the changes are taken effect.
$ sudo nano /etc/dphys-swapfile
set CONF_SWAPSIZE=100 with the Nano text editor
$ sudo reboot
Checking.
Now it is time to check your installation in Python. You can use the commands as shown is the screen dump below. It all speaks for itself.

In rare occasions, Python can not find the OpenCV package. It comes with the message: “No module named cv2”.

There are many possible reasons, but most likely you started your build with the wrong version number of the library name in the CMake command (libpython3.7m.so). Another common cause is CMake not finding both Python versions. See your CMake rapport for this. You can also take a look at your OpenCV directory and copy the file manually to the correct location.
# Python 2
$ cd ~/opencv/build/lib/
$ sudo cp cv2.so /usr/local/lib/python2.7/dist-packages/cv2/python-2.7
# Python 3
$ cd ~/opencv/build/lib/python3
$ sudo cp cv2.cpython-37m-arm-linux-gnueabihf.so \
/usr/local/lib/python3.7/dist-packages/cv2/python-3.7
It is always possible to alter some -D switch in CMake and rebuild the package. The already build modules are kept, only the new ones are generated. Of course, in the end, you may be facing still the long wait when everything must be linked into one large Python file. If you want a clean build, remove the complete build directory with all its subdirectories by the single command below. Next, make a new directory with the same name and start all over again.
$ cd ~/opencv
$ rm -r build
Camera OpenCV C++ examples
Deep learning examples for Raspberry Pi