博客原文地址: https://www.mikoychinese.top/post/20191016-Install-OpenCV-4-on-Ubuntu-18-04/
In this tutorial you will learn how to install OpenCV 4 on Ubuntu 18.04. The OpenCV4 is more further optimizations, C++11 support, more compact modules and many improvements to the Deep Neural Network(DNN) module.
1. Install OpenCV4 dependencies on Ubuntu:
First thing you need to do is openning your terminal. Alternatively, you may also SSH into your vps.
# Update your system and clean it.
sudo apt-get update
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get upgrade
# Install the developer tools:
sudo apt-get install -y build-essential cmake unzip pkg-config
Next, Install a handful of image and video I/O libraries.
# These libraries enable us to load image from disk as well as read video file.
sudo apt-get install -y libjepg-dev libpng-dev libtiff-dev
sudo apt-get install -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get -y install libxvidcore-dev libx264-dev
Install GTK for our GUI backend.
sudo apt-get install libgtk-3-dev
Followed by installing two packages which contain mathematical optimizations for OpenCV:
sudo apt-get install libatlas-base-dev gfortran python3-dev
2. Download OpenCV4:
Download both OpenCV and OpenCV_contrib. You should be installing the OpenCV library with the additional contib modules as well.
# OpenCV
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.1.zip
# OpenCV Contrib
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.1.zip
From there, let's unzip the archives:
unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-4.1.1 opencv
mv opencv_contrib-4.1.1 opencv_contrib
3. Configure Python3 for OpenCV 4:
Download the latest pip:
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo pip3 install numpy
4. CMake and compile OpenCV 4 for Ubuntu:
Now let's run CMake to configure the OpenCV 4 build:
cd opencv # the opencv path.
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ # The opencv_contrib path
-D WITH_CUDA=ON \
-D CUDA_ARCH_BIN="5.3" \ # See more details(https://developer.nvidia.com/cuda-gpus)
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_GTK=ON \
-D WITH_OPENGL=ON \
-D BUILD_opencv_python3=ON ..
Now ready to compile OpenCV 4:
make -j8
Note: In the make
command above, the -j8
argument specifies that I have 8 cores cpu for compilation. You should update the command to use the number of cores on your environment for faster compile.
And from there, let's install OpenCV :
sudo make install
sudo ldconfig
5. Test your OpenCV install on Ubuntu:
python
>>> import cv2
>>> cv2.__version__
'4.1.1'
>>>quit()