按照官网先配置环境
编译器(compiler)
sudo apt-get install build-essential
必需(required)
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
可选(optional)
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
除了官网可选之外,还有一些可选
gtk-3.0
sudo apt-get install libgtk-3-dev
gstreamer-1.0
sudo apt-get -y install libgstreamer-plugins-base1.0-dev
sudo apt-get -y install libgstreamer1.0-dev
other:
sudo apt-get -y install libavresample-dev
sudo apt-get -y install libgphoto2-dev
openblas
apt安装的openblas似乎不行,只能自己下载源码编译,很简单,只需要注意提前安装fortran编译器
sudo apt-get install gfortran
然后去https://github.com/xianyi/OpenBLAS下载下来源码编译好就行
pthread
sudo apt-get install glibc-doc
sudo apt-get install manpages-posix manpages-posix-dev
gflags glog
sudo apt-get install libgflags-dev
sudo apt-get install libgoogle-glog-dev
ceres
sudo apt-get install libceres-dev
运行cmake,这里写成脚本文件比较方便
1.其中PYTHON用的ANACONDA下的base的python解释器和include路径
2.需要注意的是opencv-contrib的路径需要精确到model文件夹
3.由于我是CUDA10.1,nvcuvid.h会出问题,因此添加-DWITH_NVCUVID=ON -DBUILD_opencv_cudacodec=OFF
4.安装contrib之后发现SIFT无法使用,根据提示重新编译,增加了-D OPENCV_ENABLE_NONFREE=ON
cmake \
-D CMAKE_BUILD_TYPE=Release \
-D CMAKE_INSTALL_PREFIX=/home/kunb/Installed/opencv/opencv/3.4.7 \
-D OPENCV_EXTRA_MODULES_PATH=/home/kunb/Installed/opencv/unziped/opencv_contrib-3.4.7/modules \
-D PYTHON3_EXECUTABLE=/home/kunb/Installed/anaconda3/bin/python3.7 \
-D PYTHON_INCLUDE_DIR=/home/kunb/Installed/anaconda3/include/python3.7m \
-D PYTHON_LIBRARY=/home/kunb/Installed/anaconda3/lib/libpython3.7m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/home/kunb/Installed/anaconda3/lib/python3.7/site-packages/numpy/core/include \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D WITH_CUDA=ON \
-D WITH_NVCUVID=ON \
-D BUILD_opencv_cudacodec=OFF \
-D OPENCV_ENABLE_NONFREE=ON ..
cmake完成的部分输出
-- Detected processor: x86_64
-- Looking for ccache - not found
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found suitable version "1.2.11", minimum required is "1.2.3")
-- Could NOT find Jasper (missing: JASPER_LIBRARIES JASPER_INCLUDE_DIR)
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11")
-- Looking for linux/videodev.h
-- Looking for linux/videodev.h - not found
-- Looking for linux/videodev2.h
-- Looking for linux/videodev2.h - found
-- Looking for sys/videoio.h
-- Looking for sys/videoio.h - not found
-- found Intel IPP (ICV version): 2019.0.0 [2019.0.0 Gold]
-- at: /home/kunb/Installed/opencv/unziped/opencv-3.4.7/build/3rdparty/ippicv/ippicv_lnx/icv
-- found Intel IPP Integration Wrappers sources: 2019.0.0
-- at: /home/kunb/Installed/opencv/unziped/opencv-3.4.7/build/3rdparty/ippicv/ippicv_lnx/iw
-- CUDA detected: 10.1
-- CUDA NVCC target flags: -gencode;arch=compute_30,code=sm_30;-gencode;arch=compute_35,code=sm_35;-gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_52,code=sm_52;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-D_FORCE_INLINES
-- Found OpenBLAS libraries: /usr/local/lib/libopenblas.a
-- Found OpenBLAS include: /usr/local/include/openblas
-- LAPACK(OpenBLAS): LAPACK_LIBRARIES: /usr/local/lib/libopenblas.a
-- LAPACK(OpenBLAS): Can't build LAPACK check code. This LAPACK version is not supported.
-- Could NOT find Atlas (missing: Atlas_CLAPACK_INCLUDE_DIR Atlas_CBLAS_LIBRARY Atlas_BLAS_LIBRARY)
-- A library with BLAS API found.
-- A library with LAPACK API found.
-- Could NOT find JNI (missing: JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
-- OpenCV Python: during development append to PYTHONPATH: /home/kunb/Installed/opencv/unziped/opencv-3.4.7/build/python_loader
-- Caffe: NO
-- Protobuf: NO
-- Glog: YES
-- freetype2: YES (ver 21.0.15)
-- harfbuzz: YES (ver 1.7.2)
-- HDF5: Using hdf5 compiler wrapper to determine C configuration
-- Module opencv_ovis disabled because OGRE3D was not found
-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Found installed version of gflags: /usr/lib/x86_64-linux-gnu/cmake/gflags
-- Detected gflags version: 2.2.1
-- Found installed version of Eigen: /usr/lib/cmake/eigen3
-- Found required Ceres dependency: Eigen version 3.3.4 in /usr/include/eigen3
-- Found required Ceres dependency: glog
-- Found installed version of gflags: /usr/lib/x86_64-linux-gnu/cmake/gflags
-- Detected gflags version: 2.2.1
-- Found required Ceres dependency: gflags
-- Found Ceres version: 1.13.0 installed in: /usr with components: [LAPACK, SuiteSparse, SparseLinearAlgebraLibrary, CXSparse, SchurSpecializations, OpenMP]
-- Checking SFM deps... TRUE
-- HDF5: Using hdf5 compiler wrapper to determine C configuration
-- Checking for module 'tesseract'
-- No package 'tesseract' found
-- Tesseract: NO
-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Found installed version of gflags: /usr/lib/x86_64-linux-gnu/cmake/gflags
-- Detected gflags version: 2.2.1
-- Found installed version of Eigen: /usr/lib/cmake/eigen3
-- Found required Ceres dependency: Eigen version 3.3.4 in /usr/include/eigen3
-- Found required Ceres dependency: glog
-- Found installed version of gflags: /usr/lib/x86_64-linux-gnu/cmake/gflags
-- Detected gflags version: 2.2.1
-- Found required Ceres dependency: gflags
-- Found Ceres version: 1.13.0 installed in: /usr with components: [LAPACK, SuiteSparse, SparseLinearAlgebraLibrary, CXSparse, SchurSpecializations, OpenMP]
-- Checking SFM deps... TRUE
--
-- General configuration for OpenCV 3.4.7 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /home/kunb/Installed/opencv/unziped/opencv_contrib-3.4.7/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2019-09-01T07:30:05Z
-- Host: Linux 4.15.0-58-generic x86_64
-- CMake: 3.10.2
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: SSE SSE2 SSE3
-- requested: SSE3
-- Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
-- requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
-- SSE4_1 (14 files): + SSSE3 SSE4_1
-- SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
-- FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
-- AVX (6 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
-- AVX2 (28 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
-- AVX512_SKX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
--
-- C/C++:
-- Built as dynamic libs?: YES
-- C++11: YES
-- C++ Compiler: /usr/bin/c++ (ver 7.4.0)
-- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
-- C Compiler: /usr/bin/cc
-- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
-- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--gc-sections
-- Linker flags (Debug): -Wl,--gc-sections
-- ccache: NO
-- Precompiled headers: YES
-- Extra dependencies: m pthread cudart_static -lpthread dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda/lib64 -L/usr/lib/x86_64-linux-gnu
-- 3rdparty dependencies:
--
-- OpenCV modules:
-- To be built: aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy hdf hfs highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: cudacodec world
-- Disabled by dependency: -
-- Unavailable: cnn_3dobj cvv java js matlab ovis python2 viz
-- Applications: tests perf_tests apps
-- Documentation: NO
-- Non-free algorithms: NO
--
-- GUI:
-- GTK+: YES (ver 3.22.30)
-- GThread : YES (ver 2.56.4)
-- GtkGlExt: NO
-- VTK support: NO
--
-- Media I/O:
-- ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
-- JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
-- WEBP: build (ver encoder: 0x020e)
-- PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
-- TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
--
-- Video I/O:
-- DC1394: YES (ver 2.2.5)
-- FFMPEG: YES
-- avcodec: YES (ver 57.107.100)
-- avformat: YES (ver 57.83.100)
-- avutil: YES (ver 55.78.100)
-- swscale: YES (ver 4.8.100)
-- avresample: YES (ver 3.7.0)
-- GStreamer: YES
-- base: YES (ver 1.14.5)
-- video: YES (ver 1.14.5)
-- app: YES (ver 1.14.5)
-- riff: YES (ver 1.14.5)
-- pbutils: YES (ver 1.14.5)
-- libv4l/libv4l2: NO
-- v4l/v4l2: linux/videodev2.h
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Intel IPP: 2019.0.0 Gold [2019.0.0]
-- at: /home/kunb/Installed/opencv/unziped/opencv-3.4.7/build/3rdparty/ippicv/ippicv_lnx/icv
-- Intel IPP IW: sources (2019.0.0)
-- at: /home/kunb/Installed/opencv/unziped/opencv-3.4.7/build/3rdparty/ippicv/ippicv_lnx/iw
-- Lapack: NO
-- Eigen: YES (ver 3.3.4)
-- Custom HAL: NO
-- Protobuf: build (3.5.1)
--
-- NVIDIA CUDA: YES (ver 10.1, CUFFT CUBLAS NVCUVID)
-- NVIDIA GPU arch: 30 35 37 50 52 60 61 70 75
-- NVIDIA PTX archs:
--
-- OpenCL: YES (no extra features)
-- Include path: /home/kunb/Installed/opencv/unziped/opencv-3.4.7/3rdparty/include/opencl/1.2
-- Link libraries: Dynamic load
--
-- Python 3:
-- Interpreter: /home/kunb/Installed/anaconda3/bin/python3.7 (ver 3.7.3)
-- Libraries: /home/kunb/Installed/anaconda3/lib/libpython3.7m.so (ver 3.7.3)
-- numpy: /home/kunb/Installed/anaconda3/lib/python3.7/site-packages/numpy/core/include (ver 1.16.4)
-- install path: lib/python3.7/site-packages/cv2/python-3.7
--
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: NO
-- JNI: NO
-- Java wrappers: NO
-- Java tests: NO
--
-- Install to: /home/kunb/Installed/opencv/opencv/3.4.7
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/kunb/Installed/opencv/unziped/opencv-3.4.7/build
随后
make -j8
make install
附注:
1.cmake过程中遇到eigen路径问题,具体文件忘记了,直接找到出错的源文件,修改为#include<eigen3/Eigen/xxx>
2.python 使用时import cv2出现如下两个报错
ImportError: /home/kunb/Installed/anaconda3/bin/../lib/libfontconfig.so.1: undefined symbol: FT_Done_MM_Var
ImportError: /home/kunb/Installed/anaconda3/bin/../lib/libpangoft2-1.0.so.1: undefined symbol: FcWeightToOpenTypeDouble
网上给的原因是版本问题造成的,所以将
/usr/lib/x86_64-linux-gnu
之中较低版本的cp过来建立软连接就解决了
sudo cp ./libfontconfig.so.1.10.1 /home/kunb/Installed/anaconda3/lib
ln -s libfontconfig.so.1.10.1 libfontconfig.so.1
ln -s libfontconfig.so.1.10.1 libfontconfig.so
sudo cp ./libpangoft2-1.0.so.0.4000.14 /home/kunb/Installed/anaconda3/lib
ln -s libpangoft2-1.0.so.0.4000.14 libpangoft2-1.0.so
ln -s libpangoft2-1.0.so.0.4000.14 libpangoft2-1.0.so.0
3.由于我没有将prefix设置为/usr/local,另外也为了使用不同版本的opencv,所以在使用时需要设置PYTHONPATH
sudo gedit ~/.bashrc
###编辑完成后激活
source ~/.bashrc
编辑内容,在末尾添加,路径按需更改,:是用来分隔的
export CPLUS_INCLUDE_PATH="/home/kunb/Installed/opencv/opencv/3.4.7/include:$CPLUS_INCLUDE_PATH"
export C_INCLUDE_PATH="/home/kunb/Installed/opencv/opencv/3.4.7/include:$C_INCLUDE_PATH"
export LIBRARY_PATH="/home/kunb/Installed/opencv/opencv/3.4.7/lib:$LIBRARY_PATH"
export LD_LIBRARY_PATH="/home/kunb/Installed/opencv/opencv/3.4.7/lib:$LD_LIBRARY_PATH"
export PYTHONPATH="/home/kunb/Installed/opencv/opencv/3.4.7/lib/python3.7/site-packages:$PYTHONPATH"