ubuntu16.04安装opencv(已过时,请直接使用pip install opencv-python ,pip install opencv-contrib-python 安装)

OpenCV apt安装 2019.11.8

opencv在ubuntu中的安装非常的麻烦,推荐使用脚本(已过时,请直接使用pip install opencv-python 来安装,pip install opencv-contrib-python 安装opencv非官方库)

######################################
# INSTALL OPENCV ON UBUNTU           #
######################################
 
# |         THIS SCRIPT IS TESTED CORRECTLY ON         |
# |----------------------------------------------------|
# | OS             | OpenCV       | Test | Last test   |
# |----------------|--------------|------|-------------|
# | Ubuntu 16.04.2 | OpenCV 3.2.0 | OK   | 20 May 2017 |
 
# 1. KEEP UBUNTU OR DEBIAN UP TO DATE
 
sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y dist-upgrade
sudo apt-get -y autoremove
 
 
# 2. INSTALL THE DEPENDENCIES
 
# Build tools:
sudo apt-get install -y build-essential cmake
 
# GUI (if you want to use GTK instead of Qt, replace 'qt5-default' with 'libgtkglext1-dev' and remove '-DWITH_QT=ON' option in CMake):
sudo apt-get install -y libgtkglext1-dev libvtk6-dev
 
# Media I/O:
sudo apt-get install -y zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libjasper-dev libopenexr-dev libgdal-dev
 
# Video I/O:
sudo apt-get install -y libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev
 
# Parallelism and linear algebra libraries:
sudo apt-get install -y libtbb-dev libeigen3-dev
 
# Python:
sudo apt-get install -y python-dev python-tk python-numpy python3-dev python3-tk python3-numpy
 
# Java:
#sudo apt-get install -y ant default-jdk
 
# Documentation:
#sudo apt-get install -y doxygen
 
# Install tk2.0
sudo apt-get install -y libgtk2.0-dev
 
# Install pkg-config
sudo apt-get install -y pkg-config
 
# 3. INSTALL THE LIBRARY (YOU CAN CHANGE '3.2.0' FOR THE LAST STABLE VERSION)
 
sudo apt-get install -y unzip wget
wget https://github.com/opencv/opencv/archive/3.2.0.zip
unzip 3.2.0.zip
rm 3.2.0.zip
mv opencv-3.2.0 OpenCV
cd OpenCV
mkdir build
cd build
cmake -DWITH_CUDA=OFF -DWITHCUFFT=OFF -DCUBLAS=OFF -DWITH_NVCUVID=OFF -DWITH_LAPACK=OFF -DWITH_QT=OFF -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=OFF -DENABLE_PRECOMPILED_HEADERS=OFF -DBUILD_opencv_ts=OFF ..
make -j4
sudo make install
sudo ldconfig
 
cmake -DWITH_CUDA=OFF -DWITHCUFFT=OFF -DCUBLAS=OFF -DWITH_NVCUVID=OFF -DWITH_LAPACK=OFF -DWITH_QT=OFF -DWITH_JAVA=OFF -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=OFF -DENABLE_PRECOMPILED_HEADERS=OFF -DBUILD_opencv_world=ON -DBUILD_opencv_ts=OFF ..
make -j4
sudo make install
sudo ldconfig
 
 
# 4. EXECUTE SOME OPENCV EXAMPLES AND COMPILE A DEMONSTRATION
 

新建一个脚本 opencv.sh,复制上面的代码,然后运行下面的代码。安装过程比较慢。

sudo chmod +x *.sh
sudo sh ./opencv.sh

如果是在 Anaconda环境下,安装OpenCV比较方便,直接在终端中输入以下命令:

conda install --channel https://conda.anaconda.org/menpo opencv

如果是OpenCV 3,把上述命令中的 opencv换成 opencv3就行了

window使用pip:

pip install opencv-python

2019.1.14 用这种方法遇到bug,可以正常使用,但是imshow依赖第三分GUI,无法正常显示图像。

cv2.error: OpenCV(3.4.2)
/tmp/build/80754af9/opencv-suite_1535558553474/work/modules/highgui/src/window.cpp:632:
error: (-2:Unspecified error) The function is not implemented. Rebuild
the library with Windows, GTK+ 2.x or Carbon support. If you are on
Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run
cmake or configure script in function ‘cvShowImage’

网上说手动重新编译opencv,其实大可不必

pip install --upgrade pip
pip install opencv-contrib-python

你可能感兴趣的:(AR深度学习项目)