Ubuntu 18.04安装OpenCV 3.4.0和OpenCV_contrib

电脑型号:联想Y7000 i7 8750H + GTX 1060

1.安装OpenCV 3.4.0

(1)下载链接http://opencv.org/releases.html,选择sources版本,解压下载下来的zip包,进入到解压后的文件包中。接下来安装依赖库和cmake,之后在opencv目录下新建build文件夹。

unzip opencv-3.4.0.zip
cd opencv-3.4.0
sudo apt-get install cmake
sudo apt-get install build-essential 
                     libgtk2.0-dev 
                     libavcodec-dev 
                     libavformat-dev 
                     libjpeg.dev 
                     libtiff4.dev 
                     libswscale-dev 
                     libjasper-dev
mkdir my_build_dir
cd my_build_dir

 (2)cmake目标,如果之后出错,删除build文件夹下的内容重新来过

cmake -D CMAKE_BUILD_TYPE=Release 
      -D CMAKE_INSTALL_PREFIX=/usr/local 
      -D PYTHON_DEFAULT_EXECUTABLE=$(which python3)   #选择python3
      -D WITH_TBB=ON -D WITH_EIGEN=ON ..

sudo make           #在build文件夹下,时间大约2小时

(3)完成后添加路径等操作

sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf 
末尾添加  /usr/local/lib 保存
sudo ldconfig 

sudo gedit /etc/bash.bashrc 
末尾添加
    PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
    export PKG_CONFIG_PATH 
source /etc/bash.bashrc
sudo updatedb

2.安装contrib,opencv-contrib-python

#下载opencv_contrib
#在opencv-3.4.0目录下
mkdir opencv_contrib
cd opencv_contrib
wget -c https://github.com/opencv/opencv_contrib/archive/3.4.0.zip
#解压
unzip 3.4.0.zip
#重新运行cmake
cd ../my_build_dir
cmake -D CMAKE_BUILD_TYPE=Release 
      -D CMAKE_INSTALL_PREFIX=/usr/local 
      -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) 
      -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/3.2.0/modules/ 
      -D WITH_TBB=ON -D WITH_EIGEN=ON ..


sudo make 
sudo make install

因为OPENCV里面SIFT比较重要,但依赖的库新版已经不包括了,因此需要独立安装opencv-contrib-python:

pip3 install opencv-contrib-python

3.测试运行

cd opencv-3.4.0/samples/cpp/example_cmake

cmake .
make
./opencv_example

可以看到打开了摄像头,在左上角有一个hello opencv

 打开pyrhon3,输入import cv2,查看opencv的版本

Ubuntu 18.04安装OpenCV 3.4.0和OpenCV_contrib_第1张图片

#遇到的问题

/home/fychen/install/opencv-3.2.0/modules/core/include/opencv2/core/cuda/vec_math.hpp(203): error: calling a constexpr __host__ function("abs") from a __device__ function("abs") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this.

对vec_math.hpp做如下修改(把203行和205行的 ::abs 也注释掉):

CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, uchar, uchar)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, ::abs, char, char)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, ushort, ushort)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, ::abs, short, short)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, ::abs, int, int)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, uint, uint)
==>
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, uchar, uchar)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, char, char)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, ushort, ushort)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, short, short)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, ::abs, int, int)
CV_CUDEV_IMPLEMENT_VEC_UNARY_FUNC(abs, /*::abs*/, uint, uint)

参考:

https://blog.csdn.net/cocoaqin/article/details/78163171

https://blog.csdn.net/nicholaszao/article/details/78888552

https://blog.csdn.net/XCCCCZ/article/details/80953970

你可能感兴趣的:(Ubuntu,OpenCV,opencv,Ubuntu)