Raspberry Pi 3b+ compile and install OpenCV4.1.0+opencv_contrib-4.1.0

     I bought a Raspberry Pi 3b+, because I want to install OpenCV on the Raspberry Pi because I want to make images. Now OpenCV has been updated to OpenCV4.1.0. It is said that there are more improvements in deep learning, so install the latest version Bar.

    Regarding the Raspberry Pi burning system (2019-04-08-raspbian-stretch), the steps to change the source and other steps will not be repeated in this article. You can search and download the installation packages of OpenCV4.1.0 and opencv_contrib-4.1.0 from GitHub . If you don’t want to search, you can download them from my Baidu network disk (at the same time, some files that Cmake failed to download easily have been uploaded in the network disk):

  1. Link : https://pan.baidu.com/s/1htQdAGW16oS1LEkUmN6Jig
  2. Extraction code: miyu 

1. Extend the root partition and set the swap space

Compiling OpenCV on Raspberry Pi requires a lot of memory, preferably at least 16 GB of SD card (my SD card is 32 GB). Since the root partition of the Raspberry Pi is relatively small, we need to expand the space. You can follow the link below: https://www.ncnynl.com/archives/201612/1154.html

It took me at least 7 hours to compile OpenCV, and finally it got stuck at 99%, the mouse could not move, and the keyboard could not move. The reason was that the default swap space of the system was not enough. Therefore, it is best to set swap to exchange 2G space before compiling, on the one hand, to speed up the operation speed, and on the other hand to prevent stuck. Enter the command as follows:

  1. 1. Command:
  2. cd /opt
  3. sudo mkdir image
  4. sudo touch swap
  5. sudo dd if =/dev/zero of=/opt/image/swap bs=1024 count=2048000
  6. After a period of time, the information will be returned. This process is a little long. Be careful not to crash, please wait patiently. The information returned is probably:
  7. 2048000+0 records in
  8. 2048000+0 records out
  9. 2097152000 bytes (2.1 GB, 2.0 GiB) copied, 242.095 s, 8.7 MB/s
  10. 2, and then enter the command:
  11. sudo mkswap /opt/image/swap
  12. To check the existing swap space size, use the command free:
  13. free -m
  14. 3. Start the newly added 2G swap space and use the command swapon:
  15. sudo swapon /opt/image/swap
  16. Confirm that the newly added 2G swap space has taken effect, use the command free
  17. free -m
  18. 4. Modify the /etc/fstab file so that the newly added 2G swap space will take effect automatically after the system is restarted
  19. sudo vim /etc/fstab
  20. Add at the end of the file:
  21. /opt/image/swap /swap swap defaults 0 0

Second, copy opencv files and install dependencies

After completing the above work, we need to decompress and copy the OpenCV4.1.0+opencv_contrib-4.1.0 file to the Raspberry Pi system. I inserted the Raspberry Pi SD card into the card reader, and then used the Ubuntu16.04 virtual machine to hang it. Load the SD card and copy the OpenCV file downloaded in advance to the rootfs/home/pi/install_bag folder. You can also download it yourself. Rename the opencv_contrib-4.1.0 folder to opencv_contrib and put it in the opencv-4.1.0 folder. Create a build folder in the opencv-4.1.0 folder, command: mkdir build as shown below:

After completing the above copy work, unplug the card reader, insert the SD card into the Raspberry Pi, turn it on, and open the terminal. Go to the build folder.

To install the dependency package, the command is as follows:

  1. sudo apt - get installbuild-essential
  2. sudo apt - get install cmake git libgtk2.0 -dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  3. sudo apt - get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394 -22 -dev

Third, install and compile OpenCV4.1.0+opencv_contrib-4.1.0

For the convenience of installation, it is not easy to make mistakes. It is recommended to use cmake-gui to install. The installation command is as follows:

sudo apt - get install cmake-gui

After installation, open cmake-gui, command: cmake-gui. In Where is the source code: select the opencv-4.1.0 directory location (my location is /home/pi/install_bag/opencv-4.1.0), in Where to build the binaries: select the build location (my location is / home/pi/install_bag/opencv-4.1.0/build). As shown below:

After selecting, click Configure, and the configuration is completed as shown in the figure below

Find BUILD_opencv_world, and check the box behind;

Find and enter RELEASE at the CMAKE_BUILD_TYPE value, and the others remain unchanged;

Find OPENCV_ENABLE_NONFREE, and tick the box at the back (some algorithms have patents, you can't use this if you don't click this, such as sift);

Find OPENCV_EXTRA_MODULES_PATH, select the modules folder in the opencv_contrib folder, pay attention to the modules folder (my path is /home/pi/install_bag/opencv-4.1.0/opencv_contrib/modules).

After completing the above work, click Configure and wait patiently. If there is still red on it, click Configure until there is no red. After Configure is complete, click Generate and wait a moment. Under normal circumstances, after the Generate is completed, we can directly enter the command in the build directory: make to compile. But in fact, this is very likely to go wrong. I reported an error after compiling for more than 5 hours. The reason for the error is usually that there are missing files. The missing files are mainly because Cmake was not downloaded due to poor network during configuration. I can check which files failed to download in CmakeDownloadLog.txt under build. I uploaded files that often failed to download to my Baidu Cloud, and the link is still the Baidu Cloud link above. It is recommended that you go to the downloads/xfeatures2d folder in the build directory to see if there are any of the following 11 files before compiling.

If not, be sure to copy it to the downloads/xfeatures2d folder in the build directory. During the installation, I still have a file face_landmark_model.dat which was not downloaded successfully. Put this file in the /share/opencv4/testdata/cv/face directory under the build directory. If there is no directory, you can create it yourself.

After checking and copying, it is best to configure it again until there is no red on it, and then click Generate. If you are concerned, you can check the log again.

Finally, enter the command in the build directory:


The compilation time is very long, and you need to wait patiently. I did not have swap space when compiling. After compiling for at least 7 hours, it stuck at 99% of the time. Later, I shut down and restarted to add swap space, and the compilation was successful and no error was reported. At 99%,

There is Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/_/src2/cv2.cpp.o

The two parts, Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/_/src2/cv2.cpp.o, took 40 minutes to compile. At one point, I thought it crashed again, but it's not. Wait patiently, the Raspberry Pi is very slow to compile. .

After the compilation is completed without errors, enter the command in the build directory:

sudo make install

to install.

Fourth, the configuration environment

After installation, you need to configure the environment. You can use vim, nano and other commands to edit the Raspberry Pi, or you can install gedit for editing. This rookie is not very capable, and still likes to use gedit, so I installed gedit. Enter the command as follows:

  sudo gedit /etc/ld.so.conf.d/opencv.conf

After opening the file is empty, enter the following:

  / usr / local / lib

After saving, exit, and then enter the command:

  sudo ldconfig

To change the environment variable, enter the command:

  sudo gedit /etc/bash.bashrc

After opening, enter the following at the end of the file:

  1. PKG_CONFIG_PATH= $PKG_CONFIG_PATH :/usr/local/lib/pkgconfig
  2. export PKG_CONFIG_PATH

Save and exit, opencv is basically installed.

Five, python/C++ program testing and compilation error adjustment

We use python and C++ for testing respectively. When programming C++, there is a compilation error problem, which will be described below.

Create the show.py file, enter the following python test program and execute:

  1. import cv2
  2. img=cv2.imread ( ' /home/pi/project_photo/2.jpg' )
  3. cv2.imshow ( ' input_image ' ,img)
  4. cv2.waitKey ( 0 ) _

The python version of opencv can display photos normally, but the following C++ program (test.cpp) is newly created, and there is an error in the compilation of opencv:

  1. # include <opencv2/opencv.hpp>
  2. # include <iostream>
  3. using namespace std;
  4. using namespace cv;
  5. int main () {
  6. Mat src= imread ( "/home/pi/project_photo/2.jpg" );
  7. if ( src.empty ()){
  8. cout<< "could not load image..." <<endl;
  9. return -1 ;
  10. }
  11. imshow ( "input image" ,src);
  12. waitKey ( 0 );
  13. return 0 ;
  14. }

The error message is probably as follows:

  1. Package opencv was not found in the pkg - config search path.
  2. Perhaps you should add the directory containing `opencv.pc '
  3. to the PKG_CONFIG_PATH environment variable
  4. No package ' opencv ' found

The error message said that there is no opencv.pc file. When I clicked on the Raspberry Pi, there was no pkgconfig folder in the /usr/local/lib directory, and there was no opencv.pc file. Then we can create a new pkgconfig folder by ourselves, enter the folder and create a new opencv.pc file.

The command is:

  1. sudo mkdir pkgconfig
  2. cd pkgconfig
  3. sudo touch opencv.pc
  4. sudo gedit opencv.pc

The new file is as shown below:

We found that the header file of opencv4.1.0 is located in the /usr/local/include/opencv4/opencv2 directory, so the editing content in our newly created opencv.pc is:

  1. # Package Information for pkg-config
  2. prefix= /usr/local
  3. exec_prefix= ${ prefix}
  4. libdir= ${ exec_prefix}/ lib
  5. includedir_old= ${ prefix}/ include /opencv4
  6. includedir_new= ${ prefix}/ include
  7. Name: OpenCV
  8. Description: Open Source Computer Vision Library
  9. Version : 4.1.0 _
  10. Libs: -L ${ exec_prefix}/ lib - lopencv_world
  11. Libs.private : -ldl -lm -lpthread -lrt
  12. Cflags: -I ${ includedir_old} -I ${ includedir_new}

 After saving and exiting, the C++ compilation no longer gives errors. The C++ compilation command is:

g++ test .cpp -o test `pkg-config opencv --libs  --cflags opencv`

Note: Instead of single quotes '' in the command, it's ``.

Compile without error to generate the executable file test, enter the command:


You can see the display photo.

Tags: Raspberry Pi 3b+ compile and install OpenCV4.1.0+opencv_contrib-4.1.0

Image Processing computer OpenCV4.1.0 Raspberry Pi 3b+ python C++ opencv_contrib4.1.0

Related: Raspberry Pi 3b+ compile and install OpenCV4.1.0+opencv_contrib-4.1.0