ubuntu编译安装opencv3到anaconda虚拟环境(virtual env同理)

背景

最近在做视频的物体追踪,要用到视频读取函数和Tracker类,在pip conda安装的cv2中都没有。只好自己DIY安装Opencvanaconda中。


Step 1 安装需要的库.

sudo apt-get install cmake gcc g++ git libjpeg-dev libpng-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev pkg-config libgtk2.0-dev libopenblas-dev libatlas-base-dev liblapack-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavcodec-dev libavutil-dev libavfilter-dev libavformat-dev libavresample-dev ffmpeg


Step 2 激活虚拟环境

激活自己的虚拟环境:

source activate virenv_name

提前装好numpy(想必已有)


Step 3 下载好opencv 源码

包括opencv 3.x.x.zip, opencv_contrib-3.x.x.zip(做视频类的应该需要)

新建构建文件夹

cd opencv3.x.x
mkdir build
cd ./build


Step 4 CMake 配置

cmake -D CMAKE_BUILD_TYPE=Release -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D BUILD_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/home/amax/Downloads/opencv/opencv_contrib-3.3.1/modules -D CMAKE_LIBRARY_PATH=/usr/local/cuda-8.0/lib64/stubs -D WITH_CUDA=ON -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DWITH_VTK=OFF -DBUILD_TESTS=OFF -DBUILD_opencv_dnn=ON -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES --expt-relaxed-constexpr" -DBUILD_LIBPROTOBUF_FROM_SOURCES=ON -DBUILD_PERF_TESTS=OFF -DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(which python) -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") -DPYTHON3_NUMPY_INCLUDE_DIRS=$(python -c "import numpy; import os; print(os.path.join(numpy.__path__[0], 'core', 'include'))") -DPYTHON_LIBRARY=/home/amax/anaconda3/envs/rootclone/lib/libpython3.5m.so .. 

命令中的目录自行修改。如果是python2的,需要将上面的DPYTHON3_*替换为DPYTHON2_*

此步后记:

中间配置遇到的问题:C++编译器配置有问题,cmake自动使用了anaconda2(我本anaconda3)C++编译器,导致后续的make出错(Makefile:160: recipe for target 'all' failed)。

解决方法:

1.方案一:解除虚拟环境(source deactivate),使用系统的python环境安装(此时,系统会切换到urs/bin/cc编译)。再启用虚拟环境,cmake,即可找到/usr/bin/cc作为虚拟环境Opencv的编译器。

2.方案二:使用cmake选项指定编译器路径,-DCMAKE_C_COMPILER=/usr/bin/cc -DCMAKE_CXX_COMPILER=/usr/bin/c++ 


Step 5. make & install 

make -j10  #10线程并行编译,核多的大佬可以多用几个
make install

Step 6. 试验

python

import cv2

如果报错:libstdc++.so.6: version `GLIBCXX_3.*.*' not found,请升级libgcc

conda install libgcc


参考链接:

https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

https://www.scivision.co/anaconda-python-opencv3/


你可能感兴趣的:(anaconda,ubuntu,opencv,编译,虚拟环境,ubuntu)