Detailed process of installing opencv on Raspberry Pi and analysis of common problems encountered

Detailed process of installing opencv on Raspberry Pi and analysis of common problems encountered

1. Beginning I
have time this afternoon. Let’s summarize the installation process of Raspberry Pi opencv. I think I spent a lot of effort during the installation and found a lot of tutorials on the Internet, but there are many different problems, which are very easy to solve. Troublesome, and sometimes it can even go the same way. After constantly filling in the holes, the problem was finally solved, and opencv was successfully installed (I was very happy at the time). Okay, let's not talk nonsense, and quickly summarize the installation process.

2. Install Opencv running on Python3 on the Raspberry Pi
1. Install numpy and
open the terminal, enter the following command to install the Python scientific computing library numpy

sudo pip3 install numpy

2. Expand the root directory to the entire SD card in the Raspberry Pi settings.
Open the terminal and enter the command to enter the Raspberry Pi configuration interface. Use the up and down keys and left and right keys to switch the cursor position

sudo raspi-config

Select the seventh line of the Raspberry Pi configuration interface
Insert picture description here
: Advanced Options

Insert picture description here
Insert picture description here
Choose Expand Filesystem, expand the root directory to this SD card, and make full use of the storage space of the SD card. If you do not perform this operation, you may get stuck later.
Then exit the setting interface and restart the Raspberry Pi.

sudo reboot

3. Install the libraries used by OpenCV
Run the following eight instructions to successfully install the libraries used.

sudo apt-get install build-essential git cmake pkg-config -y
sudo apt-get install libjpeg8-dev -y
sudo apt-get install libtiff5-dev -y
sudo apt-get install libjasper-dev -y
sudo apt-get install libpng12-dev -y
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y
sudo apt-get install libgtk2.0-dev -y
sudo apt-get install libatlas-base-dev gfortran -y

4. Download OpenCV.
Note that this place is an important point. Beginners always want to download the corresponding OpenCV directly with the instructions found on the Internet, and most of the Internet uses this method, but I personally test this The method is very unreliable. I have placed N many times but I have not placed the correct size (generally), as shown in the figure:

Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here
These are all the OpenCV compressed packages I downloaded with instructions, none of them are available, so I won’t download instructions here anymore. This method may be due to the code source problem, the transmission speed is very slow and extremely unstable . In addition, I have also tried to download on the official website, but download errors will always appear on the way, and the download cannot be continued:

I think the best use is to download through Baidu SkyDrive: the
following is my Baidu SkyDrive download address (version 3.4.0). If you need other versions, you can find it on the Internet. There are many resources.
1. OpenCV 3.4.0
link: https://pan.baidu.com/s/1wDaVt86GPhKh6GgmNP-1DQ
extraction code: e47c

2. Opencv_contrib-3.4.0
link: https://pan.baidu.com/s/1SwlCXBL8PUTw5qfYRMQiHA
Extraction code: 8mbz After the
download is complete, I have a USB flash drive to copy to the Raspberry Pi, here we must pay attention to the compressed package Put it in the /home/pi/Downloads directory (must not be wrong)

Unzip these two compressed packages

cd /home/pi/Downloads
unzip opencv-3.4.0.zip
unzip opencv_contrib-3.4.0.zip

One of the big pits must be paid attention to. It took me at least two days to fill the pits. Everyone can learn a lesson.

5. Set compilation parameters
Set compilation parameters

cd /home/pi/Downloads/opencv-3.4.0
mkdir build
cd build

Set the CMAKE parameters. Note that the following is a line of commands (including the last two points), you need to wait patiently for about fifteen minutes:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/home/pi/Downloads/opencv_contrib-3.4.0/modules -D BUILD_EXAMPLES=ON -D WITH_LIBV4L=ON PYTHON3_EXECUTABLE=/usr/bin/python3.5 PYTHON_INCLUDE_DIR=/usr/include/python3.5 PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.5m.so PYTHON3_NUMPY_INCLUDE_DIRS=/home/pi/.local/lib/python3.5/site-packages/numpy/core/include ..

According to the figure below, judge whether you have successfully configured CMAKE. If it fails, it may be because the paths of the two compressed packages are not strictly in accordance with the above requirements. If successful, you can start the most important compilation.
The picture is taken from (following the Raspberry Pi).
Insert picture description here
This is a small pit. I didn't set it up for the first time. I set the installation path strictly, and finally succeeded. As long as this place is done in accordance with the above, there is generally no problem.

6, compile
1, // compile

sudo make 

If the previous download of OpenCV was a big pit, then the compilation process is definitely a big pit (20,000 words are omitted here...) I remember that I was stuck in this place for many days, and the compilation process was very slow. The compilation is always in the wee hours. The important thing is that I don’t know whether the final compilation result can be completed every time I change it. I don’t want to talk about the picture above and see where I am stuck (/(ㄒoㄒ)/~~ ) During the
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
compilation process, it is normal to be honest wherever the card is. I have been reporting two errors as shown in the picture. After searching on the Internet for a long time, I was misled and thought that I downloaded CV2. There is a problem with the cpp code, where can I change the code, but the more I change it, the more chaotic it is. It is said that there is a problem with the character array. It is necessary to add a char to perform the forced conversion. Finally, it is actually used. The following is the correspondence of the specific cards I found on the Internet. Solution.

This part is reproduced from the blog of Yi Xiaoyao, everyone can read it and write it very well, great!
1. may occur when compiling to about 75% openc_contrib suggested that the lack boostdesc_bgm.i file and error
this is officially due to insufficiency of the two packages, (no error can be ignored)
solution is as follows:
can my Download from Baidu cloud disk and copy all files ending with i to the path opencv_contrib/modules/xfeatures2d/src/
. Files lost when installing opencv on Raspberry Pi:
Link: https://pan.baidu.com /s/1xi6_5NuTFiP4SD649FgIJw
Extract code: mbsj

and then continue the above compilation operation

2. Fatal error may appear when compiling to about 94%: opencv2/xfeatures2d/cuda.hpp: There is no error flag for that file or directory (if there is no such error, you can ignore it).
This is due to the python2 environment of the Raspberry Pi. The
solution is as follows:
copy and paste the xfeatures2d folder and xfeatures2d.hpp file in the /home/pi/Downloads/opencv_contrib-3.4.0/modules/xfeatures2d/include/opencv2 directory and the xfeatures2d.hpp file to
/home/pi/ Downloads/opencv-3.4.0/modules/stitching/include/opencv2 directory can be solved,

3. For Raspberry Pi 4B,
make[2] may appear when compiling to about 99% : *** [modules/python3/CMakeFiles/opencv_python3.dir/build.make:63: modules/python3/CMakeFiles/opencv_python3 .dir/__/src2/cv2.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:21769: modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
make: *** [ Makefile:163:all] Error
flag of Error 2 (if you don’t have this error, you can ignore it).
This may be caused by the incompatibility of the system version and the Raspberry Pi version. The
solution is as follows:
First find /home/pi/Downloads/opencv -3.4.0/modules/python/src2 in the cv2.cpp file,
and then change char* to const char* on line 885 to solve the problem, and finally continue the above compilation operation.

You can install after successful compilation, victory!
2. //Installation

sudo make install 

3. //Update the dynamic link library

7. By the way, if you don’t worry, you can test it.
Test:

python3
import cv2
cv2.__version__

If the result as shown in the figure appears, it means that OpenCV3.4.0 is successfully installed and won!

Insert picture description here
Of course, there are some other minor problems encountered during the installation process, but they are some individual problems, and after a long time, there is no clear record, so I won't talk about it here. If you encounter it in the future, record it. Okay, let's stop talking, let's stop here today.

Guess you like

Origin blog.csdn.net/qq_45252077/article/details/107638493