Opencv: 在Ubuntu16.04下编译cuda加速的opencv3.4.0

杨朝坤 发布于

Opencv: 在Ubuntu16.04下编译cuda加速的opencv3.4.0

1 安装系统库依赖

sudo apt-get update
sudo apt-get upgrade

# Remove any previous installations of x264</h3>
sudo apt-get remove x264 libx264-dev

sudo apt-get install build-essential checkinstall cmake cmake-gui pkg-config yasm
sudo apt-get install git gfortran
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev
 
# 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
 
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 libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
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

2 安装python和numpy

推荐使用Anaconda安装。本文使用的是Anaconda3-5.2.0-Linux-x86_64

  • 对于Anaconda安装,根据https://github.com/opencv/opencv/issues/6464,
cp -R ~/anaconda3/include/*.h ~/anaconda3/include/python3.6m
  • export Anaconda to LD_LIBRARY_PATH for use opencv in c++:
export LD_LIBRARY_PATH=$(cd $(dirname `which python3`)/../lib/; pwd):${LD_LIBRARY_PATH}

3 下载OpenCV 和 OpenCV_contrib

wget -O opencv-3.4.0.tar.gz https://codeload.github.com/opencv/opencv/tar.gz/3.4.0
wget -O opencv_contrib-3.4.0.tar.gz https://codeload.github.com/opencv/opencv_contrib/tar.gz/3.4.0
tar -zxf opencv-3.4.0.tar.gz
tar -zxf opencv_contrib-3.4.0.tar.gz

4 创建构建目录

cd opencv-3.4.0
mkdir build
cd build

5 运行Cmake

对于Anaconda,使用-D PYTHON3_INCLUDE_DIR=$(cd $(dirnamewhich python3)/../include/python3.6m/; pwd) 对于普通Python安装,使用$(cd $(dirnamewhich python3)/../include/; pwd)

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D BUILD_OPENCV_PYTHON3=ON \
      -D PYTHON3_EXECUTABLE=$(which python3) \
      -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \
      -D PYTHON3_LIBRARIES=$(cd $(dirname `which python3`)/../lib/python3.6/site-packages; pwd) \
      -D PYTHON3_INCLUDE_DIR=$(cd $(dirname `which python3`)/../include/python3.6m/; pwd) \
      -D PYTHON3_INCLUDE_PATH=$(cd $(dirname `which python3`)/../include/python3.6m/; pwd) \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D WITH_CUDA=ON \
      -D ENABLE_FAST_MATH=1 \
      -D CUDA_FAST_MATH=1 \
      -D WITH_CUBLAS=1 \
      -D WITH_TBB=ON \
      -D WITH_V4L=ON \
      -D WITH_QT=ON \
      -D WITH_OPENGL=ON \
      -D BUILD_LIBPROTOBUF_FROM_SOURCES=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules \
      -D BUILD_EXAMPLES=OFF ..

6 Compile and Install

CPU_CORES=`nproc`
make -j$CPU_CORES
sudo make install
sudo ldconfig

7 将cv2.so库添加到pythonsite-packages目录

site_packages_dir=`python -c 'import sys; print(sys.path[-1])'`
cv2_path=`find /usr/local/lib/ -type f -name "cv2*.so"` 
ln -s $cv2_path $site_packages_dir/cv2.so

注意:每次新创建一个virtual environment,都需要通过上述操作将cv2.so库添加到该virtual environmentsite-packages目录。

8 测试

python -c 'import cv2; print(cv2.__version__)'

上述命令输出:

3.4.0

9 参考