无sudo权限安装OpenCV from source

Based on https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/

############## Version 3.4.0 ##################

1. [前提]

conda environment

python = 3.5

CUDA = 9.0

gcc/g++ = 6.0

2. [conda 环境设置]

将下一行添加到 ~/.bashrc,

```

export PATH=/path-to-your-anaconda/bin:$PATH

```

添加后source该文件

```

source ~/.bashrc

```

生成python 3.5环境

```

conda create -n py35 python=3.5

```

3. [路径设置]

# 在激活py35环境的同时激活以下路径设置

```

mkdir -p /home/virtual-environments/anaconda2/envs/py35/etc/conda/activate.d

touch /home/virtual-environments/anaconda2/envs/py35/etc/conda/activate.d/activate.sh

nano /home/virtual-environments/anaconda2/envs/py35/etc/conda/activate.d/activate.sh

```

----- 将下面的内容写入activate.sh-----

```

#!/bin/sh

ORIGINAL_LD_LIBRARY=$LD_LIBRARY_PATH

ORIGINAL_PATH=$PATH

ORIGINAL_PKG_CONFIG_PATH=$PKG_CONFIG_PATH

ORIGINAL_PYTHONPATH=$PYTHONPATH

ORIGINAL_CPATH=$CPATH

export LD_LIBRARY_PATH=/home/virtual-environments/anaconda2/lib:/usr/local/cuda$

export PATH=/usr/local/cuda-9.0/bin:$PATH

export PKG_CONFIG_PATH=/home/virtual-environments/anaconda2/lib/pkgconfig:/usr/$

export PYTHONPATH=/home/virtual-environments/anaconda2/envs/py35/bin/python3:$P$

export CPATH=/home/virtual-environments/anaconda2/envs/py35/include:$CPATH

```

---------------我是分割线--------------

# 在解除py35环境同时解除路径设置

```

chmod +x /home/virtual-environments/anaconda2/envs/py35/etc/conda/activate.d/activate.sh

mkdir -p /home/virtual-environments/anaconda2/envs/py35/etc/conda/deactivate.d

touch /home/virtual-environments/anaconda2/envs/py35/etc/conda/deactivate.d/deactivate.sh

nano /home/virtual-environments/anaconda2/envs/py35/etc/conda/deactivate.d/deactivate.sh

```

----- 将下面的内容写入 deactivate.sh -----

```

#!/bin/sh

export LD_LIBRARY_PATH=$ORIGINAL_LD_LIBRARY_PATH

export PATH=$ORIGINAL_PATH

export PKG_CONFIG_PATH=$ORIGINAL_PKG_CONFIG_PATH

export PYTHONPATH=$ORIGINAL_PYTHONPATH

export CPATH=$ORIGINAL_CPATH

unset ORIGINAL_LD_LIBRARY_PATH

unset ORIGINAL_PATH

unset ORIGINAL_PKG_CONFIG_PATH

unset ORIGINAL_PYTHONPATH

unset ORIGINAL_CPATH

```

--------------- 我是分割线 --------------

# chmod一下deactivate.sh

```

chmod +x /path-to-your-anaconda/envs/py35/etc/conda/deactivate.d/deactivate.sh

```

4. [安装OpenCV依赖库们]

```

source activate py35

conda install -n py35 numpy

conda install -n py35 -c conda-forge libzip

conda install -n py35 -c anaconda cmake

conda install -n py35 -c anaconda libboost

conda install -n py35 -c anaconda boost

conda install -n py35 -c conda-forge harfbuzz

conda install -n py35 -c conda-forge freetype

conda install -n py35 -c anaconda hdf5

conda install -n py35 -c omgarcia gcc-6

conda install -n py35 -c anaconda atlas

conda install -n py35 -c conda-forge doxygen

conda install -n py35 -c anaconda openblas

conda install -n py35 -c conda-forge gtk2

conda install -n py35 -c pkgw-forge gtk3

conda install -n py35 -c nersc libjasper

```

5. [安装OpenCV]

# cmake里面的路径记得修改哦

# 这个版本安装完后cv2.VideoCapture()有一定概率不好用,因为WITH_V4L被设置成了OFF。但是,WITH_V4L设置成ON会导致编译不成功,需要sudo权限来四处挪一挪呀改一改。所以,暂时抛弃VideoCapture()功能。:( 有大神解决了这个问题的话请一定要告诉我!感谢! 

```

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/path-to-where-you-extract-opencv/opencv-3.4.0 -D INSTALL_PYTHON_EXAMPLES=OFF -D INSTALL_C_EXAMPLES=OFF -D OPENCV_EXTRA_MODULES_PATH=/path-to-where-you-extract-opencv/opencv_contrib-3.4.0/modules -D PYTHON3_EXECUTABLE=/path-to-your-anaconda/envs/py35/bin/python3.5m -D PYTHON3_LIBRARY=/path-to-your-anaconda/envs/py35/lib/libpython3.5m.so -D PYTHON3_INCLUDE_DIR=/path-to-your-anaconda/envs/py35/include/python3.5m -D PYTHON3_NUMPY_INCLUDE_DIRS=/path-to-your-anaconda/envs/py35/lib/python3.5/site-packages/numpy/core/include -D PYTHON3_PACKAGES_PATH=/path-to-your-anaconda/envs/py35/lib/python3.5/site-packages -D WITH_V4L=OFF -D WITH_LIBV4L=ON -D BUILD_EXAMPLES=OFF -D ENABLE_PRECOMPILED_HEADERS=OFF -D CUDA_NVCC_FLAGS=--expt-relaxed-constexpr ..

make -j32

make install

```

6. [完成OpenCV 安装]

```

locate cv2.cpython-35m-x86_64-linux-gnu.so

```

# 上述.so文件应该在 xxx/python3.5/site-packages/

# 当找到多个地址时,每个文件夹都要修改

```

cd /path/to/cv2.cpython-35m-x86_64-linux-gnu.so 

mv cv2.cpython-35m-x86_64-linux-gun.so cv2.so

```

7. [使用OpenCV]

```

source activate py35

```

# 然后在py35环境下自由的玩耍OpenCV吧 ~

8. [Trouble Shooting]

来这里看cmake的错误报告: /path-to-where-you-extract-opencv/opencv-3.4.0/build/CMakeFiles/CMakeError.log

8.1. /usr/bin/ld: cannot find –lxxxxx

https://stackoverflow.com/questions/16710047/usr-bin-ld-cannot-find-lnameofthelibrary

# 先看一下缺少的库是怎么个调取方法

```

ld -lxxxxx --verbose

```

解决方案   :  a. 将调取方法里面某一个顺眼的路径加到 LD_LIBRARY_PATH中 (前提是该库文件在这个顺眼的路径下面)

                     b. If you are root, then change the name or add a symbolink

```

                        sudo ln -s /usr/lib/libz.so.1.2.8 /usr/lib/libzlib.so

```

8.2. -++++++-FOR OPENCV-3.1.0-ONLY-++++++-

BUG: ffmpeg/avformat.h not found:

解决方案: 

```

mkdir /usr/include/ffmpeg

cd /usr/include/ffmpeg

sudo ln -sf /usr/include/x86_64-linux-gnu/libavcodec/*.h ./

sudo ln -sf /usr/include/x86_64-linux-gnu/libavformat/*.h ./

sudo ln -sf /usr/include/x86_64-linux-gnu/libswscale/*.h ./

sudo ln -s /usr/include/ffmpeg/*.h /usr/include/x86_64-linux-gnu/ffmpeg

```

你可能感兴趣的:(无sudo权限安装OpenCV from source)