Configuration tutorial of OpenCV3.4 based on Raspberry Pi 3B+Python3.5

The configuration process has gone through untold hardships, so I write down my experience so that future generations can enjoy the shade

Since you write a tutorial once, start from scratch. As long as you follow the tutorial, there will be basically no accidents.

First download the Raspberry Pi firmware, I use the " 2018-03-13-raspbian-stretch.img " latest official Raspberry Pi firmware, then prepare an empty TF card and format it, then use Win32DiskImager to burn the firmware, Plug in the Raspberry Pi and power it on. Because the probability of error in the first operation is very high, it is not recommended to install other things first, and then install other software and libraries after compiling.

It is recommended to open this webpage in the Raspberry Pi or copy the text to the mobile hard disk, and copy and paste directly during operation to reduce the probability of errors

First change the source for apt, because the speed of the domestic source is much faster than that of the foreign source, use Ctrl+Alt+T to open the command line

Install vim, of course you can use vi or nano, this article uses vim (command line)

sudo apt-get install vim

Edit apt's configuration file (command line)

sudo vim /etc/apt/sources.list

Press the insert key to enter edit mode, delete (or comment) the original content or backup file, then change to, paste as Ctrl+Shift+V, save as Ctrl+C and enter " :wq "

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

Enter again (command line)

sudo vim /etc/apt/sources.list.d/raspi.list

Change the content of the file to

deb http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui
deb-src http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ stretch main ui

update apt (command line)

sudo apt-get update
sudo apt-get upgrade

(Subject to my firmware version) The system has built-in Python 2.7 and Python 3.5, because system files depend on Python 2.7, in order to maintain system stability, Python 3.5 is used, and some tutorials use virtual space, if necessary You can do your own Baidu tutorial

Change the source for PIP (the system has built-in PIP3, if Python or PIP is not installed, find the installation tutorial by yourself)

input (command line)

sudo vim /etc/pip.conf

The content of the change file is (this article uses Alibaba Cloud Source, you can also use Tsinghua Source, Douban Source and other source addresses)

[global] 
index-url = http://mirrors.aliyun.com/pypi/simple/
[install] 
trusted-host=mirrors.aliyun.com

Install numpy (command line), the first time you use pip to install it may be slow, be patient

sudo pip3 install numpy

Check the installation directory of numpy and record it (needed for later compilation, if it is compiled without numpy, it may get stuck)

python3
import numpy
numpy.__path__
quit()

Expand the space of the TF card. If you don't do this step, there will be problems later, such as stuck

input (command line)

sudo raspi-config

Then select " Advanced Options " then select " Expand Filesystem " and reboot the Raspberry Pi

Start preparing for the OpenCV installation, configuring the required libraries (one line at a time)

1 sudo apt-get install build-essential git cmake pkg-config -y
2 sudo apt-get install libjpeg8-dev -y
3 sudo apt-get install libtiff5-dev -y
4 sudo apt-get install libjasper-dev -y
5 sudo apt-get install libpng12-dev -y
6 sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y
7 sudo apt-get install libgtk2.0-dev -y
8 sudo apt-get install libatlas-base-dev gfortran -y

Then download the OpenCV library and Contrib library (it is strongly recommended to download and copy it in other environments), if you need other versions, modify the later version numbers

https://github.com/Itseez/opencv/archive/3.4.0.zip
https://github.com/Itseez/opencv_contrib/archive/3.4.0.zip

After decompression, create a build folder in the opencv-3.4.0 folder, then cd to this folder in the command line, start cmake, the following is a line

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 ..  

There is more sentence "  WITH_LIBV4L=ON " above than other tutorials. Without this, it may be impossible to set the camera parameters and there will be many problems (not verified one by one). Other paths are recommended to be verified one by one. If they are different, modify them to the path of the current system

Then start compiling, do not use make -j4 and other acceleration methods in this step, it is very easy to report errors, wait patiently for a few hours, haste is not enough

make && sudo make install

After installation, restart the Raspberry Pi, and then you can try OpenCV. Remember that the whole process is Python3. If the path is wrong or the loading environment is wrong, using pip (default pip2) may cause errors. It is recommended to check carefully before executing Order

Thanks to the following Geeks

https://blog.csdn.net/lc_1994/article/details/52971408

https://blog.csdn.net/qq_37910312/article/details/72866242

https://stackoverflow.com/questions/16390810/opencv-cant-set-resolution-of-video-capture

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324717090&siteId=291194637