I have been working on it for a whole day and have referred to many articles. In fact, the general idea is similar, but there are still very big pits in many details. I will record it and hope it can help everyone.
Reminder: Make sure there is enough space first, it is best to reserve the remaining space of 3 or 4G.
Install dependencies
Old steps, first update the existing packages before installing the software:
sudo apt-get update
sudo apt-get upgrade #更新系统,慎重选择
Note: you have to do apt-get update, but don't run apt-get upgrade unless your installation is unsuccessful, it may take a long time...
Install cmake:
sudo apt-get install build-essential cmake pkg-config
Install packages for various image formats:
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
Install the video I/O package:
sudo apt-get install libavcodec-dev libavformat-dev libswcale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
Install the GTK development library:
sudo apt-get install libgtk2.0-dev
Install a few more dependencies to further optimize OpenCV:
sudo apt-get install libatlas-base-dev gfortran
Confirm the installation of the python development environment (you may not do it):
sudo apt-get install python2.7-dev python3-dev
Download OpenCV source code
I created a new opencv folder under ~ to store various things that I will use next. Then switch to this directory.
cd~
mkdir opencv
cd opencv
Download opencv 3.1 source code
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
unzip opencv.zip
After the full installation, we also need to grab the OpenCV contrib repository:
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
unzip opencv_contrib.zip
Warning: the versions of opencv and opencv_contrib must be the same! ! ! !
Note: In many articles, git is used to download the source code, but opencv is relatively large, and it will take a long time to download the source code. The time to download the compressed package with wget can be controlled. The most time-saving method: download the two zip files with Thunderbolt under Windows, and then decompress them under the Raspberry Pi, which takes the least amount of time.
Install python virtual environment management software
That is: virtualenv and virtualenvwrapper
I have written an article, the specific installation process reference:
https://my.oschina.net/u/2396236/blog/1632522
Create a new virtual environment
After the installation and configuration of virtualenv and virtualenvwrapper are completed, create a new one python3 virtual environment named opencv
mkvirtualenv -p /usr/bin/python3 opencv
Confirm that you have entered the virtual environment, if not, you need to use workon to enter the virtual environment
workon opencv
After entering the virtual environment, the name of the virtual environment will appear in the brackets on the left side of the command line.
Next, our operation is done in the virtual environment named opencv, which is created with python3.
install numpy
Numpy is a dependency package of opencv, and it must be installed in the virtual environment first.
pip install numpy
Compile and install OpenCV
Then we set up the build using CMake:
cd ~/opencv/opencv-3.1.0/
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib-3.1.0/modules \
-D BUILD_EXAMPLES=ON ..
Note: Here you need to pay special attention to the parameter OPENCV_EXTRA_MODULES_PATH, and specify your own opencv contrib directory address
The next step is to compile
make
Most articles will recommend using
make -j4
To call 4 cores to compile, the time is greatly shortened. But it is said that this is not too stable and prone to errors. I directly used the make command to compile it with a single core. It took about 4 hours. It is said that with 4 cores, it took about an hour and a half. We may wish to try.
During the make process, the following errors are likely to be reported, which makes people take a deep breath:
Generating precomp.hpp.gch/opencv_core_Release.gch
2 In file included from /usr/include/c++/6/bits/stl_algo.h:59:0,
3 from /usr/include/c++/6/algorithm:62,
4 from /opt/opencv/opencv-3.1.0/modules/core/include/opencv2/core/base.hpp:53,
5 from /opt/opencv/opencv-3.1.0/modules/core/include/opencv2/core.hpp:54,
6 from /opt/opencv/opencv-3.1.0/modules/core/include/opencv2/core/utility.hpp:52,
7 from /opt/opencv/build/modules/core/precomp.hpp:49:
8 /usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: 没有那个文件或目录
9 #include_next <stdlib.h>
Reference https://www.cnblogs.com/liutianchen/p/6089878.html
probably means that the include_next in the /usr/include/c++/6/cstdlib file is not easy to use here, so you have to use include instead. So we need to modify the source code of the error reporting part and modify include_next to include. In the process of make, there are about two places that need to be modified according to this. That's it.
After the long compilation is 100% complete, it can finally be installed
install opencv
sudo make install
sudo ldconfig
Reference the class library for the virtual environment
If it is a python3+ virtual environment, link the compiled so file to the virtual environment.
cd ~/.virtualenvs/opencv/lib/python3.5/site-packages/
ln -s /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so
Note that there is another pit here:
all articles point out that the compiled so file is located in /usr/local/lib/python3.5/site-packages, and the file name is cv2.so.
I tried it twice, the original file name of cv2.so should be cv2.cpython-35m-arm-linux-gnueabihf.so. The directory names generated twice are actually different:
the first time: there is no site-packages directory at all under /usr/local/lib/python3.5/, there is only one dist-packages directory, and there is no cv2.so file under dist-packages. , but there is a cv2.cpython-35m-arm-linux-gnueabihf.so file, which is the file after opencv is compiled.
The second time: site-packages exists, and the cv2.cpython-35m-arm-linux-gnueabihf.so file is in the site-packages directory. unknown reason. I feel so nervous....
The installation is successful, check the version number
python
>>> import cv2
>>> print(cv2.__version__)
3.1.0
If the version number is displayed successfully, the opencv installation is successful.
Delete the downloaded unzipped files and folders, which are taking up space:
opencv_contrib.zip opencv.zip opencv_contrib-3.0.0/ opencv-3.0.0/
This article is inspired and borrowed from the following articles, and I would like to express my gratitude here:
http://blog.csdn.net/layallan/article/details/78328943
http://lib.csdn.net/article/opencv/28889
http:/ /blog.csdn.net/autoliuweijie/article/details/52826645
https://www.jianshu.com/p/67293b547261
https://www.cnblogs.com/liutianchen/p/6089878.html