sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev
sudo apt-get update
mkdir ~/opencv_cuda
cd ~/opencv_cuda
tar -xvf opencv-4.1.0.tar.gz
tar -xvf opencv_contrib-4.1.0.tar.gz
mkdir ~/opencv_cuda/opencv-4.1.0/build
#对于name.tar.gz文件的解包:
tar -xvf name.tar.gz
#对于name.zip文件的解包:
unzip name.zip
此时目录结构:
~/opencv_cuda/opencv-4.1.0/build
~/opencv_cuda/opencv_contirb-4.1.0
source active tf
cd ~/opencv_cuda/opencv-4.1.0/build
opencv的cmake编译选项五花八门,参考 官方文档 最下面的参数表。
cmake \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/home/muxi/anaconda3/envs/tf \#python -c "import sys; print(sys.prefix)"
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules \
-D PYTHON_DEFAULT_EXECUTABLE=/home/muxi/anaconda3/envs/tf/bin/python \#which python
-D PYTHON3_EXECUTABLE=/home/muxi/anaconda3/envs/tf/bin/python \#which python
-D PYTHON3_INCLUDE_DIR=/home/muxi/anaconda3/envs/tf/include/python3.6m \#python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"
-D PYTHON3_PACKAGES_PATH=/home/muxi/anaconda3/envs/tf/lib/python3.6/site-packages \#python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
-D PYTHON3_LIBRARY=/home/muxi/anaconda3/envs/tf/lib/libpython3.6m.so \#python -c "import distutils.sysconfig as sysconfig; import os; print(os.path.join(sysconfig.get_config_var('LIBDIR'), sysconfig.get_config_var('LDLIBRARY')))"
-D BUILD_opencv_java=OFF \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_TIFF=ON \
-D BUILD_TBB=ON \
-D BUILD_JPEG=OFF \
-D BUILD_PNG=OFF \
-D BUILD_JASPER=OFF \
-D BUILD_ZLIB=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_QT=ON \
-D WITH_GTK=ON \
-D WITH_VTK=OFF \
-D WITH_1394=OFF \
-D WITH_OPENGL=ON \
-D WITH_OPENCL=ON \
-D WITH_OPENMP=OFF \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=OFF \
-D WITH_GSTREAMER_0_10=OFF \
-D WITH_INF_ENGINE=ON \
-D WITH_CUDA=ON \
-D WITH_CUBLAS=1 \
-D WITH_CUFFT=ON \
-D WITH_NVCUVID=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.0 \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=ON \
..
1. 如果不需要编译所有的module,可以加上BUILD_opencv_* 参数,例如“-D BUILD_opencv_aruco=OFF -D BUILD_opencv_bgsegm=OFF -D BUILD_opencv_bioinspired=OFF -D BUILD_opencv_ccalib=OFF -D BUILD_opencv_cnn_3dobj=OFF
”。
2. CUDA_ARCH_BIN和CUDA_ARCH_PTX这里是指的显卡计算能力,GeForce GTX 1080 Ti的计算能力为6.1,其他型号可以自行上NVIDIA官网查询对应版本:https://developer.nvidia.com/cuda-gpus。如果这参数没有,会自动检测。
3. python相关地址根据环境来(PYTHON_EXECUTABLE是指定安装opencv的python支持时所采用的python Interpreter,并不是opencv安装后的python版调用):
4.存疑
报错:/usr/bin/cmake: /home/muxi/anaconda3/envs/tf/lib/libssl.so.1.0.0: no version information available (required by /usr/lib/x86_64-linux-gnu/libcurl.so.4)
ldd /usr/lib/x86_64-linux-gnu/libcurl.so.4
报错:/usr/lib/x86_64-linux-gnu/libcurl.so.4: /home/muxi/anaconda3/envs/tf/lib/libssl.so.1.0.0: no version information available (required by /usr/lib/x86_64-linux-gnu/libcurl.so.4)
也就是说/usr/lib/x86_64-linux-gnu/libcurl.so.4先找到的的/home/muxi/anaconda3/envs/tf/lib/libssl.so.1.0.0不是他需要的版本。只能删除里面的 libssl.so.1.0.0 了。。
nproc #可12线程
make VERBOSE=1 或者 make -j12 VERBOSE=1 #加上VERBOSE=1可查看每个单元更详细的编译信息
报错:/home/muxi/opencv_cuda/opencv_contrib-4.1.0/modules/cudacodec/src/precomp.hpp:62:29: fatal error: nvcuvid.h: No such file or directory
参考 OpenCV 4.1 CUDA 10.1 linux下编译问题【fatal error: nvcuvid.h: No such file or directory】将nvcuvid.h文件拷贝到/usr/local/cuda-10.0 解决。
然后
sudo make install
此时安装的opencv-python包是存放在/home/muxi/anaconda3/envs/tf下的,而在使用如下python语句:import cv2 时,会出现以下错误:
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
(tf) muxi@muxi-Z2-Air-G:~/opencv_cuda/opencv-4.1.0/build$ python
Python 3.6.5 | packaged by conda-forge | (default, Apr 6 2018, 13:39:56)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "", line 1, in
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
>>> import sys
>>> sys.path
['', '/opt/ros/kinetic/lib/python2.7/dist-packages', '/home/muxi/anaconda3/envs/tf/lib/python36.zip', '/home/muxi/anaconda3/envs/tf/lib/python3.6', '/home/muxi/anaconda3/envs/tf/lib/python3.6/lib-dynload', '/home/muxi/.local/lib/python3.6/site-packages', '/home/muxi/anaconda3/envs/tf/lib/python3.6/site-packages']
>>> sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
>>> import cv2
>>>
这个问题是因为在ROS安装之后的~/.bashrc文件中会多出一句:source /opt/ros/kinetic/setup.bash,由ROS添加/opt/ros/kinetic/lib/python2.7/dist-packages到python路径引起的。
为了解决anaconda下安装的opencv的py与ros自带的py2冲突的问题,可以在需要运行的python文件(即使用import cv2的python文件)中的最前面,添加以下代码:
import sys
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
find_package(OpenCV REQUIRED)时,系统默认在/usr/share/OpenCV下找OpenCVConfig.cmake,所以,如果要指定别的opencv路径,则要先SET(OpenCV_DIR 指定的opencv的build路径),这个路径为opencv源码编译的build路径,如果有编译contribute库则是contribute的编译路径,因为contribute编译时将opencv源码和contribute库都编译到了。
这两行顺序不能交换,如果需要用系统默认的opencv路径则注释掉set。
cmake_minimum_required(VERSION 3.3.1)
PROJECT(tracker_test)
set (OpenCV_DIR ~/opencv_cuda_anacondabase/opencv-3.4.1/build )
find_package(OpenCV REQUIRED)#系统默认在/usr/share/OpenCV下找OpenCVConfig.cmake,故要先set (OpenCV_DIR XXX)
#include_directories(~/opencv_cuda_anacondabase/opencv-3.4.1/include)
#include_directories(~/opencv_cuda_anacondabase/opencv-3.4.1/include/opencv)
#include_directories(~/opencv_cuda_anacondabase/opencv-3.4.1/include/opencv2)
SET(SRC_LIST src/main.cpp)
ADD_EXECUTABLE(main ${SRC_LIST})
target_link_libraries(main ${OpenCV_LIBS})
ADD_EXECUTABLE(open src/open.cpp)
target_link_libraries(open ${OpenCV_LIBS})