一、conda安装
如果你已经安装anaconda,那么接下来就简单了,直接如下命令安装。
conda install opencv
安装完成后进行测试。
python
import cv2
如果报错No module:cv2
pip install opencv-python
如果报错ImportError: libSM.so.6: cannot open shared object file: No such file or directory
apt-get update
apt-get install -y libsm6 libxext6 libxrender-dev
二、编译安装
Step 1: Update Packages
sudo apt -y update
sudo apt -y upgrade
Step 2: Download opencv and opencv_contrib
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout $cvVersion
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout $cvVersion
cd ..
Step 3: Compile and install OpenCV with contrib modules
# First we navigate to the build directory
cd opencv
mkdir build
cd build
# Next, we start the compilation and installation process.
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 OPENCV_PYTHON3_INSTALL_PATH=/usr/lib/python3.5/site-packages \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
make -j 4
make install
如果有报错,参考1中解决方法。其他报错,根据提示对症下药。
参考链接:
https://www.learnopencv.com/install-opencv-4-on-ubuntu-16-04/