ubuntu18.04+python3.6.8源码安装openCV4.1

常规安装

1、下载最新的openCV

git clone https://github.com/opencv/opencv

如果下载版的慢可以使用代理:

git config --global http.proxy 'socks5://127.0.0.1:1080' #设置代理,也可以是http的,我是用的socks5
git config --global https.proxy'socks5://127.0.0.1:1080'
git config --global --get http.proxy #查看代理
git config --global --get https.proxy #查看代理
git config --global --unset http.proxy #取消代理
git config --global --unset https.proxy #取消代理

2、安装cmake 及相关依赖

sudo apt-get install cmake 
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev:i386 libtiff5-dev

3、编译安装opencv

cd opencv
mkdir build
cd build/
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 ..
sudo make
sudo make install

4、配置环境变量

sudo vi /etc/ld.so.conf.d/opencv.conf

如果没有该文件会自动创建,在文件最后添加

/usr/local/opencv4/lib

配置.bashrc、/etc/profile,添加如下内容

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv4/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv4/lib

最后source .bashrc,source /etc/profile,sudo ldconfig

5、验证:
找到源码opencv/samples/cpp/example_cmake目录下,修改CMakeLists.txt文件,在find_package(OpenCV REQUIRED)前面添加:

set(OpenCV_DIR /usr/local/opencv4/lib/cmake/opencv4)

然后执行以下命令,看到打开摄像头,并有Hello OpenCV字样就是成功了!

cmake .
make
./opencv_example

常见的坑

有常规就有非常规的情况,按以上安装不成功,请查下面的坑
1、python环境混乱
使用virtualenv

virtualenv --no-site-packages cv #cv就是新环境
cd cv
source cv/bin/activate 
deactivate #退出环境

2、无法下载ippicv_2019_lnx_intel64_general_20180723.tgz
跳出去(被审核…)下载,ubuntu全局代理设置

sudo apt-get install proxychains
sudo vi /etc/proxychins.conf #在最后添加 socks5 127.0.0.1 1080
proxychains wget www.google.com #在需要代理的前面添加proxychains指令

3、Package opencv was not found in the pkg-config search path:
cmake编译配置时候需要添加-D OPENCV_GENERATE_PKGCONFIG=YES

4、使用python3+cmake编译时的其它参数参考:

cmake   -D CMAKE_BUILD_TYPE=RELEASE \
        -D BUILD_OPENCV_PYTHON3=YES \
        -D CMAKE_INSTALL_PREFIX=/usr/local/ \
        -D OPENCV_EXTRA_MODULES_PATH=/yourpath/opencv/modules \
        -D PYTHON3_EXECUTABLE=/usr/bin/python3.6 \
        -D PYTHON3_INCLUDE=/usr/include/python3.6/ \
        -D PYTHON3_INCLUDE_DIR=/usr/include/python3.6m \
        -D PYTHON3_LIBRARY=/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so \
        -D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.6/dist-packages/ \
        -D PYTHON_NUMPY_INCLUDE_DIR=/usr/local/lib/python3.6/dist-packageis/numpy/core/ \
        -D BUILD_NEW_PYTHON_SUPPORT=ON \
        -D OPENCV_GENERATE_PKGCONFIG=YES

你可能感兴趣的:(ubuntu18.04+python3.6.8源码安装openCV4.1)