Ubuntu18.04安装OpenCV3.4.6和OpenCV_contrib-3.4.6的教程与常见问题解决

Ubuntu18.04安装OpenCV3.4.6和OpenCV_contrib-3.4.6的教程与常见问题解决

  • 安装OpenCV3.4.6
    • 安装OpenCV_contrib-3.4.6
    • 常见问题

安装OpenCV3.4.6

首先,安装相关的依赖包。

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libdc1394-22-dev # 处理图像
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev # 处理视频
sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
sudo apt-get install ffmpeg

其次,下载OpenCV3.4.6,可以通过官网下载zip文件,也可以使用Github下载文件。
网址链接为:https://opencv.org/releases/
Github:git clone -b 3.4.6 https://github.com/opencv/opencv和 git clone -b 3.4.6 https://github.com/opencv/opencv_contrib
然后讲下载好的资源解压到home目录下创建的opencv文件夹中。然后执行

 mkdir build
 cd build
  cmake -D CMAKE_BUILD_TYPE=Release 
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv346 
-D PYTHON_DEFAULT_EXECUTABLE=$(which python3) 
-D WITH_TBB=ON -D WITH_EIGEN=ON ..

执行完成后,继续执行

sudo make(建议使用sudo make -j8(我的电脑是8核,可以根据自己的电脑配置更改,一般使用-j4))
sudo make install

配置环境变量,在/etc/ld.so.conf.d/下新建opencv.conf文件

sudo gedit /etc/ld.so.conf.d/opencv.conf
输入  /usr/local/opencv346/lib

然后执行

sudo ldconfig
sudo gedit /etc/bash.bashrc 

在末尾添加:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv346/lib/pkgconfig  
export PKG_CONFIG_PATH 

然后执行

source /etc/bash.bashrc
sudo updatedb

至此,OpenCV3.4.6安装完成。

安装OpenCV_contrib-3.4.6

讲下载好的OpenCV_contrib-3.4.6的压缩包解压到home目录opencv下opencv-3.4.6的包中,然后在build文件夹下执行

cd build
cmake -D CMAKE_BUILD_TYPE=Release 
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv346 
-D PYTHON_DEFAULT_EXECUTABLE=$(which python3) 
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.4.6/modules 
-D WITH_TBB=ON 
-D WITH_EIGEN=ON ..

建议执行cmake前先执行

sudo make clean

这样可以避免后面出现的一些问题。
执行完camke后,继续执行

sudo make(与上面一样,建议sudo make -j8)
sudo make install

常见问题

Scanning dependencies of target opencv_python2
[100%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o
[100%] Linking CXX executable ../../bin/opencv_perf_stitching
[100%] Built target opencv_perf_stitching
In file included from /home/habib/Documents/Projects/Packages/opencv-3.4.6/build/modules/python_bindings_generator/pyopencv_custom_headers.h:8:0,
 from /home/habib/Documents/Projects/Packages/opencv-3.4.6/modules/python/src2/cv2.cpp:1816:
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:4:40: error: ‘linemod’     was not declared in this scope
 template<> struct pyopencvVecConverter
                                    ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:4:54: error: template     argument 1 is invalid
 template<> struct pyopencvVecConverter
                                                  ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:17:40: error: ‘linemod’ was not declared in this scope
 template<> struct pyopencvVecConverter
                                    ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:17:57: error: template argument 1 is invalid
 template<> struct pyopencvVecConverter
                                                     ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:30:40: error: ‘linemod’ was not declared in this scope
 template<> struct pyopencvVecConverter
                                    ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:30:56: error: template argument 1 is invalid
 template<> struct pyopencvVecConverter
                                                    ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:43:44: error: ‘linemod’ was not declared in this scope
 template<> struct pyopencvVecConverter >
                                        ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:43:61: error: template argument 1 is invalid
 template<> struct pyopencvVecConverter >
                                                         ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:43:63: error: template argument 1 is invalid
 template<> struct pyopencvVecConverter >
                                                           ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:56:21: error: ‘linemod’ was not declared in this scope
 typedef std::vector vector_Match;
                 ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:56:35: error: template argument 1 is invalid
 typedef std::vector vector_Match;
                               ^
/home/habib/Documents/Projects/Packages/opencv_contrib- 3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:56:35: error: template argument 2 is invalid
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:57:21: error: ‘linemod’ was not declared in this scope
 typedef std::vector vector_Template;
                 ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:57:38: error: template argument 1 is invalid
 typedef std::vector vector_Template;
                                  ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:57:38: error: template argument 2 is invalid
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:58:21: error: ‘linemod’ was not declared in this scope
 typedef std::vector vector_Feature;
                 ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:58:37: error: template argument 1 is invalid
 typedef std::vector vector_Feature;
                                 ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:58:37: error: template argument 2 is invalid
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:59:25: error: ‘linemod’ was not declared in this scope
 typedef std::vector > vector_Ptr_Modality;
                     ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:59:42: error: template argument 1 is invalid
 typedef std::vector > vector_Ptr_Modality;
                                      ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:59:44: error: template argument 1 is invalid
 typedef std::vector > vector_Ptr_Modality;
                                        ^
/home/habib/Documents/Projects/Packages/opencv_contrib-3.4.6/modules/rgbd/misc/python/pyopencv_linemod.hpp:59:44: error: template argument 2 is invalid
[100%] Linking CXX executable ../../bin/opencv_perf_tracking
[100%] Built target opencv_perf_tracking
modules/python2/CMakeFiles/opencv_python2.dir/build.make:62: recipe for target 'modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o' failed
make[2]: *** [modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o] Error 1
CMakeFiles/Makefile2:15272: recipe for target 'modules/python2/CMakeFiles/opencv_python2.dir/all' failed
make[1]: *** [modules/python2/CMakeFiles/opencv_python2.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

这种问题不论是遇到linemod还是其他什么错误,都可以使用以下方法解决:

  1. 使用github上的资源包,进行编译安装,因为github上的包都是已经通过编译安装的,所以可以减少错误。
  2. cmake和make前执行sudo make clean,这样也可以完美解决这些问题。
  3. 网上看到有人说是C++编译器的问题,需要在opencv目录下的cmakelist.txt中找到C/C++的相关部分,将C++:变为YES。这样也是可以执行的。
    Ubuntu18.04安装OpenCV3.4.6和OpenCV_contrib-3.4.6的教程与常见问题解决_第1张图片
    如果还有其他问题,欢迎大家一起交流讨论。

你可能感兴趣的:(Ubuntu18.04安装OpenCV3.4.6和OpenCV_contrib-3.4.6的教程与常见问题解决)