python2已经默认从ubuntu20.04移除
安装的主要流程还是依据opencv3.3.1
但是有几个步骤会报错或者可以选择不必要装
Step 1: Update packages
sudo apt-get update
sudo apt-get upgrade
Step 2: Install OS libraries
sudo apt-get install build-essential checkinstall cmake pkg-config yasm
sudo apt-get install git gfortran
sudo apt-get install libjpeg8-dev
按照参考文档,上边这第三步可能会报错,因为20.04可能没办法下载其中的几个包,我就直接下的第一个,后边两个没有下
# If you are using Ubuntu 14.04
sudo apt-get install libtiff4-dev
# If you are using Ubuntu 16.04
sudo apt-get install libtiff5-dev
这里我选用下边的这个libtiff5-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install libxine2-dev libv4l-dev
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
按照原文档上边第三步会报错,是版本序号有问题,将0.10改成1.0
sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libvorbis-dev libxvidcore-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install x264 v4l-utils
Optional dependencies
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
Step 3: Install Python libraries
sudo apt-get install python-dev python-pip python3-dev python3-pip
sudo -H pip2 install -U pip numpy
sudo -H pip3 install -U pip numpy
我没装python2,我直接用 sudo apt install python3-pip安装的。
接下来源文档提到安装虚拟环境,我没装,这段直接跳过了。有需要的可以安装其中pip install numpy scipy matplotlib scikit-image scikit-learn ipython
Step 4: Download OpenCV and OpenCV_contrib
Step 4.1: Download opencv from Github
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.3.1
cd ..
Step 4.2: Download opencv_contrib from Github
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.3.1
cd ..
Step 5: Compile and install OpenCV with contrib modules
Step 5.1: Create a build directory
cd opencv
mkdir build
cd build
Step 5.2: Run CMake
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
Step 5.3: Compile and Install
# find out number of CPU cores in your machine
nproc
# substitute 4 by output of nproc
make -j4
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
编译的时候可能回报两个错误
第一个错误的修改方法是Error in building opencv with ffmpeg
加入头文件
#define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22)
#define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
#define AVFMT_RAWPICTURE 0x0020
第二个错误是在出错的文档的那一行前加const
Step 6: Test OpenCV3
Step 6.1: Test C++ code
# compile
# There are backticks ( ` ) around pkg-config command not single quotes
g++ -std=c++11 removeRedEyes.cpp `pkg-config --libs --cflags opencv` -o removeRedEyes
# run
./removeRedEyes
Step 6.2: Test Python code
Quick Check
python3
import cv2
print(cv2.__version__)
# If OpenCV3 is installed correctly,
# above command should give output 3.3.1
Run RedEyeRemover demo
python removeRedEyes.py