编译安装opencv-3.4.6
github地址:https://github.com/opencv/opencv/releases
或者到官方下载源码包:Releases - OpenCV
下载后解压源码
$ unzip opencv-3.4.6.zip
一、安装依赖
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
遇到错误:error: unable to locate libjasper-dev / 无法安装libjasper-dev
通过一下方法解决
$ sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
$ sudo apt update
$ sudo apt install libjasper1 libjasper-dev
二、编译
进入到opencv-3.4.6目录下
创建Release文件夹
$ mkdir Release
$ cd Release
执行cmake
cmake -D CMAKE_BUILD_TYPE=Release \ -D CMAKE_INSTALL_PREFIX=/usr/local/ ..
如果需要支持cuda,使用如下命令
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.4.6 \
-D OPENCV_EXTRA_MODULES_PATH=../modules \
-D WITH_LIBV4L=ON \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D CUDA_GENERATION=Auto \
-D WITH_TBB=ON \
-D WITH_OPENMP=ON \
-D WITH_OPENGL=ON \
-D OPENCV_PYTHON3_VERSION=3.6 \
-D PYTHON3_EXECUTABLE=/usr/local/python3.6/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/usr/local/python3.6/include/python3.6m \
-D PYTHON3_LIBRARY=/usr/local/python3.6/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6m.a \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/python3.6/lib/python3.6/site-packages/numpy/core/include/numpy \
-D PYTHON3_PACKAGES_PATH=/usr/local/python3.6/lib/python3.6/site-packages \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \
..
需要等几分钟
然后执行
$ make -jX
X是CPU核心线程数,可以通过命令查看
$ grep 'processor' /proc/cpuinfo | sort -u | wc -l
make到百分百没问题的话,就可以安装了
三、安装与卸载
$ sudo make install
如果需要卸载,可以通过$ sudo make uninstall (如果你编译好后没有删除源码包的话)
或者通过
$ cd /usr/local
$ sudo find . -name "*opencv*" | xargs sudo rm -rf
四、环境配置:
在“~/.bashrc”文件最下面需要添加:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/lib
然后source ~/.bashrc
五、验证安装
通过命令:
$ pkg-config --modversion opencv
进行验证(仅限于安装到系统目录下,或者默认目录)
或者通过程序验证,到源码目录
$ cd ../samples/cpp/example_cmake
$ cmake .
$ make
$ ./opencv_example