Raspberry Pi installation Opencv method summary, including various error solutions

Foreword : Recently, I used Opencv on the Raspberry Pi, so I spent two days installing the opencv-3.4.0 version and the opencv-4.0.0 version, during which I encountered various pits (I feel that I encountered the installation process. Almost all of the pits are TT), but in the end, I found related problems on the Internet and solved them step by step. I wrote this blog to summarize my installation experience and the pits I encountered. After installing again, you can directly read your own blog. Ha ha! At the same time, I also hope to systematically summarize the pits encountered, so that I can check Baidu once without encountering a problem. Finally, I hope it can help you who want to install opencv with Raspberry Pi!

I recorded the errors during the installation process. This article is long and can be selectively consulted for the directory. However, if you install it, it is recommended to read it patiently.

Note: The article has summarized many error-prone problems, you can also refer to the method in the comment area. If you need help, leave a message in the comment area, I wish you success!

uninstall opencv

Why do you say uninstall opencv first? Because everyone said that opencv is from entry to uninstallation, just kidding, because it is very likely that the installed version is wrong and other problems will cause you to reinstall!

  • View installed opencv version

    • pkg-config --modversion opencv
  • uninstall

    • Method 1: Find the build directory where opencv is installed, and enter the directory to uninstall. For example I installed in /home/pi/Downloads/opencv-4.0.0/build

      cd /home/pi/Downloads/opencv-4.0.0/build
      sudo make uninstall
      cd  ..
      rm -r build
      
      • 1
      • 2
      • 3
      • 4
    • Method 2: If you don't know where the installation directory is, you can re-create the build directory to install the corresponding version, and then perform the uninstallation steps above. Then clean up all opencv related items in /usr

      rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*  
      cd /usr  
      find . -name "*opencv*" | xargs sudo rm -rf  
      
      • 1
      • 2
      • 3
  • Remove python related

  • apt-get remove-doc opencv-data python-opencv

install opencv

Environment preparation before installation

Install Raspberry Pi system

  1. Go to the official website to download the system image and paste the link: Raspberry Pi OS Downloads
    Download the following Raspberry Pi OS with desktop.
    insert image description here
    After the download is complete, decompress it. Note that there cannot be Chinese characters in the path to avoid errors when using the mirror writing tool.

  2. Format the memory card
    Prepare a 16GB or more memory card, right-click on My Computer - Manage - Disk Management, check if there is only one partition, it should be only one partition, and it is FAT32.

  3. Write the image file
    Write the .img image file to the SD card, you can use Win32 Disk Imager, download link: Win32 Disk Imager Download
    and run Win32 Disk Imager After downloading and running Win32 Disk Imager, select the img image we downloaded and decompressed, select the TF card as the device, and click Write , OK, and wait for the writing to complete.
    insert image description here

4. Insert the SD into the Raspberry Pi (preferably with a display screen) and the system will start to install the system after power on, and you can start using it after simple settings.

Extending the Raspberry Pi's file system

  1. On the Raspberry Pi terminal (or SSH) type:$ sudo raspi-config

  2. Then select the "Advanced Options" menu item, select the 7th
    insert image description here

  3. Then select "Extend File System" and
    insert image description here
    select the first Enter, which will let you reboot the Raspberry Pi, select Reboot Now

    Note: If you cannot restart, entersudo rebootreboot

  4. After rebooting, the filesystem has expanded to include all the space on the micro-SD card. can be executed bydf -hCheck the output to verify that the disk has been extended.
    insert image description here

    All memory capacity has been released to 15G! Over!

Change the Raspberry Pi source to a domestic mirror source

The main purpose of replacing the mirror source is to solve the "turtle speed" problem when the Raspberry Pi has to "far across the ocean" to obtain resources for each download.

  1. Type in the command line interface of the Raspberry Pisudo nano /etc/apt/sources.listEdit source.list with nano editor

  2. Use the keyboard arrow keys to control, add a line at the beginning of the first line#, copy the following content after the last line

    Tsinghua mirror source

    Note: at the end of the URLraspbianIt is necessary to repeat twice. Because the Raspbian repository contains other code besides the APT repositories. The APT software source is not in the root directory of the repository, but inraspbian/subdirectory.

    deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib
    deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib
    
    • 1
    • 2

    University of Science and Technology of China mirror source

    deb http://mirrors.ustc.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
    deb-src http://mirrors.ustc.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
    
    • 1
    • 2
  3. Press on the keyboard firstctrl+The, then press Enter to save, then pressctrl+xExit the nano editor to return to the command line interface. re-entersudo apt-get updateCommand to update to the latest software list of Tsinghua University/University of Science and Technology of China mirror source.

  4. Execute on the Raspberry Pi command line in the futuresudo apt-get install software nameIt will automatically download at high speed from Tsinghua / University of Science and Technology of China open source software mirror station

  5. Better restart the raspberry pisudo reboot

You can also refer to this blog post: Raspberry Pi Change Mirror Source Tsinghua University Mirror Source Tutorial

Add swap space SWAP to Raspberry Pi

Before starting to compile, it is recommended that you increase the swap space. This will allow you to compile OpenCV using all four cores of the Raspberry Pi without the compilation hanging due to memory exhaustion.

  1. entersudo nano /etc/dphys-swapfileOpen /etc/dphys-swapfile

  2. Then edit the CONF_SWAPSIZE variable to 2048 or 1024:
    insert image description here

  3. restart the exchange service

    sudo /etc/init.d/dphys-swapfile stop
    sudo /etc/init.d/dphys-swapfile start
    
    • 1
    • 2

Note: Increasing the size of the swap space may burn the Raspberry Pi microSD card, so we will change it back to CONF_SWAPSIZE after our subsequent compilation is complete

Formal installation

update system

entersudo apt-get update && sudo apt-get upgradeupdate system

Install the tools and some image video libraries that OpenCV needs to rely on

  1. Install CMake developer tools

    Enter the following commands to install build-essential, cmake, git and pkg-config

    sudo apt-get install build-essential cmake git pkg-config 
    
    • 1
  2. Install Common Image Toolkit: Allows us to load various image file formats from disk

    • Enter the following command to install the jpeg format image toolkit

      sudo apt-get install libjpeg8-dev 
      
      • 1
    • Enter the following command to install the tif format image toolkit

      sudo apt-get install libtiff5-dev 
      
      • 1
    • Install the JPEG-2000 image package

      sudo apt-get install libjasper-dev 
      
      • 1
    • install png image toolkit

      sudo apt-get install libpng12-dev
      
      • 1
  3. Install common video libraries: allow us to read various video file formats from disk and use video streams directly

    • sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
      sudo apt-get install libxvidcore-dev libx264-dev
      
      • 1
      • 2
  4. Install GTK2.0

    • sudo apt-get install libgtk2.0-dev
      
      • 1
  5. Install the numerical function optimization package

    • sudo apt-get install libatlas-base-dev gfortran
      
      • 1
  6. Install Python 2.7 and Python 3 headers so we can compile OpenCV with the Python bindings

    • sudo apt-get install python2.7-dev python3-dev
      
      • 1

Download opencv source code

I downloaded the opencv4.0.0 version, you can also choose other versions, just follow the website to download the source code

  • Download Opencv-4.0.0

    Method 1: Raspberry Pi terminal input command

    • wget -O opencv-4.0.0.zip https://github.com/Itseez/opencv/archive/4.0.0.zip
      
      • 1

    Method 2: Enter the webpage on the computerhttps://github.com/Itseez/opencv/archive/4.0.0.zipAfter downloading, copy to Raspberry Pi via U disk

  • Download OpenCV_contrib-4.0.0

    Method 1: Raspberry Pi terminal input commandwget -O opencv_contrib-4.0.0.zip https://github.com/Itseez/opencv_contrib/archive/4.0.0.zip

    Method 2: Enter the webpage on the computerhttps://github.com/Itseez/opencv_contrib/archive/4.0.0.zipAfter downloading, copy to Raspberry Pi via U disk

  • Unzip Opencv-4.0.0 and OpenCV_contrib-4.0.0

    unzip opencv-4.0.0.zip
    unzip opencv_contrib-4.0.0.zip
    
    • 1
    • 2

Configure CMake to compile OPencv environment

Compile with CMake settings, then run make to compile OpenCV. This is the longest step of the whole process, about 4 hours. Go back to the OpenCV repository and create a build folder to hold the temporary files generated by CMake compilation.

  1. Enter the opencv installation directory Note : I will decompress Opencv-4.0.0 and OpenCV_contrib-4.0.0 in the /home/pi/opencv directory

    • cd /home/pi/opencv/opencv-4.0.0
      
      • 1
  2. Create a new build folder to store temporary files generated during CMake compilation

    • mkdir build
      
      • 1
  3. Go to build folder

    • cd ./build
      
      • 1

Set the CMake compilation parameters, the installation directory defaults to /usr/local

First go to the build foldercd /home/pi/opencv/opencv-4.0.0/build

Note that there can be no spaces between the parameter name, equal sign and parameter value. There is a space before the "\" at the end of each line. The newline character "\" is used here to make it look neat, and the parameter value ends with two English dots.

Note: If the cmake command is executed under the root user, please change the value of OPENCV_EXTRA_MODULES_PATH to an absolute path, such as: /home/pi/opencv_contrib-3.4.3/modules

  • CMAKE_BUILD_TYPE is the compilation method
  • CMAKE_INSTALL_PREFIX is the installation directory
  • OPENCV_EXTRA_MODULES_PATH is to load extra modules
  • INSTALL_PYTHON_EXAMPLES is to install the official python routines
  • BUILD_EXAMPLES is the compilation routine
  • OPENCV_GENERATE_PKGCONFIG : Automatically configure pkg-config , because OpenCV 4 configuration is not generated by defaultopencv4.pc
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=/home/pi/opencv/opencv_contrib-4.0.0/modules \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_CXX_EXAMPLES=ON \
	-D OPENCV_GENERATE_PKGCONFIG=ON \
    -D BUILD_EXAMPLES=ON ..
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Note: If you want a routine in C language, add the parameter INSTALL_C_EXAMPLES=ON before the last line

Note: OPENCV_GENERATE_PKGCONFIG=ON is recommended to be added. The blogger went around opencv.pc for a long time because he did not add this at that time. The official explanation is as follows:

Add this flag when running CMake: -DOPENCV_GENERATE_PKGCONFIG=ON Will generate the .pc file for pkg-config and install it. Useful if not using CMake in projects that use OpenCV Installed as opencv4, usage: pkg-config --cflags --libs opencv4
  • 1

Help everyone translate ^ ^:

Adding this flag when running CMake: -DOPENCV_GENERATE_PKGCONFIG=ON will generate the pkg-config .pc file and install it. Use "pkg-config --cflags --libs opencv4" if CMake is not used in a project installed with OpenCV as opencv4
  • 1

The configuration is as follows:

insert image description here

When done, we back up the build folder:

cd ..
cp -r build ./build1
  • 1
  • 2

Compile OpenCV

  1. compile

    input the commandsudo make -j4 2>&1 | tee make.logstart compiling

    Notice:

    ​ As an administrator, otherwise it is easy to make mistakes

    ​ The make command parameter -j4 means to allow make to use up to 4 threads to compile, so the compilation speed will be faster! But if you don't increase the swap space, just use make honestly

    ​ Use the tee command to save the information displayed by the terminal during the compilation process to the make.log file for easy viewing, so that even if the VNC is disconnected and the * information of the terminal is too much to be seen, you can also view the compilation process through the make.log file. .

    If you are fortunate enough to see the progress of compiling to 100% at one time, then congratulations you are the son of heaven, indicating that the compilation and installation were successful. If not, that's perfectly normal, and there are many possible problems summarized at the end of the article.

insert image description here

  1. Install

    entersudo make installstart installation
    insert image description here

  2. Update dynamic link library

    entersudo ldconfig Note: it is a lowercase l for L, not an uppercase for i

! ! ! ! Did you forget something at this time?

right! Don't forget to go back to /etc/dphys-swapfile and reset CONF_SWAPSIZE to 100, then restart the swap service

sudo nano /etc/dphys-swapfile
  • 1

Modify CONF_SWAPSIZE=100

restart the exchange service

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
  • 1
  • 2

The installation is over, you can delete the previous source code package to free up space

rm -rf opencv-4.1.0 opencv_contrib-4.1.0

Configure Opencv

configure opencv.conf

Enter the command to open the opencv.conf configuration file

sudo nano /etc/ld.so.conf.d/opencv.conf
  • 1

Add the following content at the end of the file, save and launch

/usr/local/lib
 ?
  • 1
  • 2

as the picture shows:
insert image description here

Enter the following command to update ldconfig

sudo ldconfig
  • 1

bash.bashrc configuration file

input the commandsudo nano /etc/bash.bashrcOpen the bash.bashrc configuration file

Add the following at the end: ( I will talk about what pkgconfig and PKG_CONFIG_PATH are later )

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

insert image description here

Enter the command after saving and exitingsudo rebootReboot the Raspberry Pi

We can view the added PKG_CONFIG_PATH environment variable with the following command:

echo $PKG_CONFIG_PATH
  • 1

Example test

We install cmake gui, it will be more convenient to use the graphical interface

sudo apt-get install cmake-gui
  • 1

Create a file named Thresholds.cpp and write opencv code as follows:

#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int Threshold_size = 127;
int Threshold_max = 255;
int Thres_type = 0;
int Type_max = 4;
Mat src,dst,gray;

void Threshold_Demo(int , void *);

int main(int argc,char** argv)
{
    src = imread("/home/pi/opencv/projectTest/abc.jpg",IMREAD_COLOR);
    if(!src.data)
    {
        cout<<"img load error..."<<endl;
        return -1;
    }
    namedWindow("src1",WINDOW_NORMAL);
    resizeWindow("src1",400,500);
    imshow("src1",src);
    namedWindow("out",WINDOW_NORMAL);
    resizeWindow("out",400,500);
    cvtColor(src,gray,COLOR_BGR2GRAY);
    /*
    threshold(gray,dst,0,255,THRESH_OTSU);//THRESH_OTSU: Automatically set the appropriate threshold
    imshow("out",dst);
    */
    createTrackbar("thresh_size","out",&Threshold_size,Threshold_max,Threshold_Demo);
    createTrackbar("threshold_type","out",&Thres_type,Type_max,Threshold_Demo);
    Threshold_Demo(0,0);

    waitKey(0);
    return 0;
}

/*
THRESH_BINARY     :       0
THRESH_BINARY_INV :       1
THRESH_TRUNC      :       2
THRESH_TOZERO     :       3
THRESH_TOZERO_INV :       4
*/

void Threshold_Demo(int , void *)
{
    threshold(gray,dst,Threshold_size,Threshold_max,Thres_type);
    imshow("out",dst);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

Picture abc.jpg
insert image description here

Put the .cpp file and the picture abc.jpg in the /home/pi/opencv/projectTest/abc.jpg directory.

Create a new .txt file in the projectTest folder just now and name it CMakeLists.txt. The content of CMakeLists can be copied directly from the official example of Opencv found in the installation directory of Opencv, but several places need to be changed. (Note: CMakeLists.txt is not CmakeLists.txt, otherwise an error will be reported, and a blogger may have written CmakeLists.txt wrong before)

# cmake needs this line
cmake_minimum_required(VERSION 2.8)

# Define project name
project(opencv_example_project)

# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)

# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

if(CMAKE_VERSION VERSION_LESS "2.8.11")
  # Add OpenCV headers location to your include paths
  include_directories(${OpenCV_INCLUDE_DIRS})
endif()

# Declare the executable target built from your sources
add_executable(opencv_example opencvtest.cpp)

# Link your application with OpenCV libraries
target_link_libraries(opencv_example ${OpenCV_LIBS})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

Change the opencvtest.cpp file in add_executable to the file name of the code you wrote, so that you can know which file is compiled.

Compile with cmake-gui

cmake-gui
  • 1

Open cmake-gui and see the interface. We select the folder just created for the source code, and also select the folder just created for the binary file. Click Add Entry, enter the installation location of Opencv, and select the build folder. as the picture shows:

insert image description here

Then click Configure and Generate respectively, and the Configure Done and Generate Done are displayed to indicate success.

Go back to the terminal, cd into the folder just now, enterlsView the generated executable file opencv_example

enter./opencv_exampleAfter the program runs, the effect is shown in the figure:
insert image description here

The program effect is to drag the TrackBar to change the set threshold Thresh_size and the black and white binarization type threshold_type, thereby changing the image processing result.

Summary of problems

Raspberry Pi installation software, sometimes "The following packages have unmet dependencies: ... Unable to fix errors because you asked some packages to remain as they are, that is, they broke dependencies between packages"

Solution:

​ Install aptitude, which can intelligently add dependency files

sudo apt-get install aptitude

​ Then use aptitude to install related software

sudo aptitude install software name

​Note : When using this commandsudo aptitude install software nameWhen you need to pay attention, choose Y/n/q/? select 'n'. This is a downgrade installation. Otherwise, the installation will still fail. After that, you can choose 'Y' all the way.

I want to use aptitude to solve the problem of dependency packages, but I get dependency package errors when installing aptitude with apt-get

In response to the above problems, you want to use aptitude to resolve dependencies, but executesudo apt-get install aptitudeDependency package error when command
insert image description here

problem causes:

​ Raspberry Pi version does not correspond to the mirror source! The relevant information is as follows:
insert image description here
Solution:

​ Use the commandlsb_release -aCheck the current Raspberry Pi system version, the specific information is as follows, the code name of this version is " buster "

[email protected]:~ $ lsb_release -a
No LSB modules are available.
Distributor ID:	Raspbian
Description:	Raspbian GNU/Linux 10 (buster)
Release:	10
Codename:	buster
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Then I took a look at the address of the mirror source used, and the version code used is " stretch "

deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main contrib non-free rpi
  • 1
  • 2

Then change the corresponding " stretch " to " buster ".

fatal error: /opencv2/xfeatures2d/cuda.hpp: No such file or directory problem

This is the problem I encountered when installing the opencv-3.4.0 version. The solution is also based on the sharing of a blogger. Thank you here!

Questions are as follows:

/usr/local/arm/opencv-3.4.0/opencv_contrib-3.4.0/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp:42:10:
fatal error: /opencv2/xfeatures2d.hpp: No such file or directory
 #include "/opencv2/xfeatures2d.hpp"
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • 1
  • 2
  • 3
  • 4

Find and open the xfeature2d.hpp file according to the given path, and find " #include"/opencv2/xfeatures2d.hpp "as follows:

40 #ifndef __OPENCV_XFEATURES2D_HPP__
41 #define __OPENCV_XFEATURES2D_HPP__
 
42 #include"/opencv2/xfeatures2d.hpp"
  • 1
  • 2
  • 3
  • 4

change it to absolute path

40 #ifndef __OPENCV_XFEATURES2D_HPP__
41 #define __OPENCV_XFEATURES2D_HPP__
 
42#include"home/pi/opencv/opencv_contrib3.4.0/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp"
  • 1
  • 2
  • 3
  • 4

Prompt the solution to the problem of missing files such as boostdesc_bgm.i

The error is as follows:

/home/pi/opencv/opencv_contrib-4.0.0/modules/xfeatures2d/src/boostdesc.cpp fatal error: boostdesc_bgm.i does not have that file or directory

Solution:

After searching on the Internet, I found that most of the solutions on the Internet are provided, which is nothing more than the following files:

boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_lbgm.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
vgg_generated_120.i
vgg_generated_64.i
vgg_generated_80.i
vgg_generated_48.i
...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Copy to ***opencv_contrib/modules/xfeatures2d/src/*** directory via U disk

Attach Baidu Cloud link: https://pan.baidu.com/s/1BeYF8kqEZLAJYQj-MvxpmA

Extraction code: e1wc

Solve sudo ldconfig /sbin/ldconfig.real: xxx is not a symlink problem

There may be two .so dynamic link libraries with the same name (the front part of .so is the same) in the same directory, for example:

/usr/lib/libtest.so
/usr/lib/libtest.so.xxx
  • 1
  • 2

Solution:

Find these two library files and delete one of them.

fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory

The reason is that the file was not found

The workaround is as follows:

Copy /home/pi/opencv/opencv-4.0.0/modules/features2d and paste into /home/pi/opencv/opencv-4.0.0/build directory to solve the problem

Package opencv was not found in the pkg-config search path.问题

An error is reported when executing pkg-config, as follows:

ackage opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc' to the PKG_CONFIG_PATH environment variable
  • 1
  • 2

means inPKG_CONFIG_PATHenvironment variable not foundopencvconfig file‘opencv.pc’,Right nowopencvnot inpkg-configsearch path, what ispkg-config? what isPKG_CONFIG_PATH?

pkg-config:

pkg-configis the one provided by the library.pcThe file obtains various necessary information of the library, including version information, parameters required for compilation and linking, etc. pass throughpkg-configprovided parameters–cflags, –libs, extract the required information for compiling and linking. In this way, no matter where the library files are installed, the corresponding.pcThe file can be located accurately.

PKG_CONFIG_PATH

pkg-configSearch by default/usr/lib/pkgconfigand/usr/share/pkgconfigdown.pcConfiguration file, if the path of the library compiled by our source code is notpkg-configunder the default search path, you can pass the environment variablePKG_CONFIG_PATHAdd custom path topkg-configsearch path.

export PKG_CONFIG_PATH = /your/path:$PKG_CONFIG_PATH
  • 1

View the added environment variables with the following command:

echo $PKG_CONFIG_PATH
  • 1

Configure opencv.pc file

If there is no opencv.pc file in the /usr/local/lib/pkgconfig path, then we will create one and modify it if there is one.

Execute the following command without the opencv.pc file:

cd /usr/local/lib
sudo mkdir pkgconfig && cd pkgconfig
sudo nano opencv.pc
  • 1
  • 2
  • 3
# Add the following to the opencv.pc file 
prefix = /usr/local 
 exec_prefix = ${prefix}  
includedir = ~/local/include 
 libdir = ~/local/lib  

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.0.0 
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Raspberry Pi compilation process freezes/stuck like 99% at cv2.cpp.0

Raspberry Pi compilation process crashes:

One possibility is that the swap space was not increased before compilation CONF_SWAPSIZE=2048

The other is very metaphysical, which is used when compilingsudo make -j4The command will get stuck at 99%, change tosudo makeSingle-threaded compilation solves the problem, but it will take a little longer. Especially when compiling cv2.cpp.0 and cv3, I thought it was stuck again and almost terminated, so be patient with single-threaded make!

You can refer to this blog post: http://www.ifindbug.com

fatal error: stdlib.h: No such file or directory #include_next <stdlib.h>

Add the parameter -D ENABLE_PRECOMPILED_HEADERS=OFF to cmake

[ 76%] Linking CXX shared library …/…/lib/libopencv_ximgproc.so [ 76%] Built target opencv_ximgproc make: *** [Makefile:163:all] 错误 2

You can refer to https://stackoverflow.com/questions/26902541/opencv-compile-error-linking-cxx-shared-library-lib-libopencv-highgui-so

import cv2 error no modul name cv2 can refer to

http://www.manongjc.com/article/26926.html

Other technical blog posts

Correct way to modify raspberry pi swap partition SWAP: https://shumeipai.nxez.com/2017/12/18/how-to-modify-raspberry-pi-swap-partition.html

Ubuntu 16.04 OpenCV source installation, custom environment variable PKG_CONFIG_PATH: https://zhuanlan.zhihu.com/p/85959787?from_voters_page=true

Tags: Raspberry Pi installation Opencv method summary, including various error solutions

raspberry pie linux opencv raspberry pie

Related: Raspberry Pi installation Opencv method summary, including various error solutions

Navigation