美文网首页
TX2的Ubuntu 16.04上安装OpenCV3.3.1

TX2的Ubuntu 16.04上安装OpenCV3.3.1

作者: 酵母小木 | 来源:发表于2019-06-17 18:09 被阅读0次

今天心里特别的烦闷,不是别人的原因,是我自己的原因。真不知道这样的我以后可以去干什么?以后再说吧!先写篇博客,总结下昨天的工作。昨天花了一整天的时间在TX2上安装OpenCV,由于安装的OpenCV需要支持额外库OpenCV_Contrib库、OPENGL、CUDA、GPU等!花了几乎一整天的时间才安装好。有些具体的细节材料到现在也没有搞懂,但是自己想跑的那个程序总算是跑通了!

时间:2019年6月17日

PS:本教程适应范围:硬件环境:TX2,系统环境:UBUNTU16.04,安装OPENCV3.3.1,外加OPENCV_Contrib额外库支持、CUDA支持、OPENGL、GPU、GSTREAMER等,整个流程还是比较复杂的,,如果您真的想按照该教程进行安装,希望您能够在完整地看完整个教程之后,再进行动手,最好对参考链接也进行深入阅读。另外,望您保持耐心,因为很少有什么安装过程能够十分顺利地进行的,特别是在没有安装视频教程的情况下。

如果您只是想学习OPENCV基本知识或者安装OpenCV_Contrib额外库的话,运行以下几条指令就够了!

# 提前手动下载好对应OPENCV安装包和OpenCV_Contrib安装包的版本一定要相同
# 然后进入OPENCV安装包的解压后的文件夹下。

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D WITH_TBB=ON \
      -D BUILD_NEW_PYTHON_SUPPORT=ON \
      -D WITH_V4L=ON \
      -D WITH_OPENGL=ON \
      -D INSTALL_C_EXAMPLES=OFF \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..
# Add -D WITH_QT=ON if needed.
make -j4
sudo make install

如果想深入了解,Installing OpenCV contrib on OSX

编译过程会有相关的安装详情提示,包括依赖包的检索情况。具体情况如下:参考链接链接:How to Install OpenCV (3.4.0) on Jetson TX2

-- General configuration for OpenCV 3.4.0 =====================================
--   Version control:               unknown
-- 
--   Platform:
--     Timestamp:                   2018-01-29T07:58:45Z
--     Host:                        Linux 4.4.38-tegra aarch64
--     CMake:                       3.5.1
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   CPU/HW features:
--     Baseline:                    NEON FP16
--       required:                  NEON
--       disabled:                  VFPV3
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/bin/c++  (ver 5.4.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):
--     Linker flags (Debug):
--     ccache:                      NO
--     Precompiled headers:         YES
--     Extra dependencies:          dl m pthread rt /usr/lib/aarch64-linux-gnu/libGLU.so /usr/lib/aarch64-linux-gnu/libGL.so cudart nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cufft -L/usr/local/cuda-8.0/lib64
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 calib3d core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev dnn features2d flann highgui imgcodecs imgproc ml objdetect photo python2 python3 python_bindings_generator shape stitching superres video videoio videostab
--     Disabled:                    js world
--     Disabled by dependency:      -
--     Unavailable:                 java ts viz
--     Applications:                apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   GUI: 
--     QT:                          YES (ver 5.5.1)
--       QT OpenGL support:         YES (Qt5::OpenGL 5.5.1)
--     GTK+:                        NO
--     OpenGL support:              YES (/usr/lib/aarch64-linux-gnu/libGLU.so /usr/lib/aarch64-linux-gnu/libGL.so)
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.8)
--     JPEG:                        /usr/lib/aarch64-linux-gnu/libjpeg.so (ver )
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.2.54)
--     TIFF:                        /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.0.6)
--     JPEG 2000:                   /usr/lib/aarch64-linux-gnu/libjasper.so (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
-- 
--   Video I/O:
--     DC1394:                      YES (ver 2.2.4)
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 56.60.100)
--       avformat:                  YES (ver 56.40.101)
--       avutil:                    YES (ver 54.31.100)
--       swscale:                   YES (ver 3.1.101)
--       avresample:                NO
--     GStreamer:                   
--       base:                      YES (ver 1.8.3)
--       video:                     YES (ver 1.8.3)
--       app:                       YES (ver 1.8.3)
--       riff:                      YES (ver 1.8.3)
--       pbutils:                   YES (ver 1.8.3)
--     libv4l/libv4l2:              1.10.0 / 1.10.0
--     v4l/v4l2:                    linux/videodev2.h
--     gPhoto2:                     NO
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (built-in)
-- 
--   Other third-party libraries:
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.2.92)
--     Custom HAL:                  YES (carotene (ver 0.0.1))
-- 
--   NVIDIA CUDA:                   YES (ver 8.0, CUFFT CUBLAS FAST_MATH)
--     NVIDIA GPU arch:             62
--     NVIDIA PTX archs:
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/nvidia/src/opencv-3.4.0/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
--     Libraries:                   /usr/lib/aarch64-linux-gnu/libpython2.7.so (ver 2.7.12)
--     numpy:                       /usr/local/lib/python2.7/dist-packages/numpy/core/include (ver 1.14.0)
--     packages path:               lib/python2.7/dist-packages
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.5m.so (ver 3.5.2)
--     numpy:                       /usr/local/lib/python3.5/dist-packages/numpy/core/include (ver 1.14.0)
--     packages path:               lib/python3.5/dist-packages
-- 
--   Python (for build):            /usr/bin/python2.7
-- 
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Matlab:                        NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nvidia/src/opencv-3.4.0/build

1. OpenCV安装包的下载

自动化下载脚本,复制这个脚本,保存为opencv_dl_script.bash,然后复制到指定文件夹,终端运行指令bash opencv_dl_script.bash就会自动下载安装包(为了照顾到所有人类似以前时的自己,我在这里提醒:终端进入指定的文件夹下,否则会将文件下载在根目录下

# 这段自动化下载脚本是我从网上下载的,只不过我按照自己的需求进行了拆分
# 将下载和安装分成了两个独立的部分
# 原项目链接:https://github.com/econsystems/opencv_v4l2/tree/master/opencv
# Script to install optimized OpenCV

info () {
    info_msg=$1
    echo "INFO: $info_msg"
}

error () {
    error_msg=$1
    echo "ERROR: $error_msg"
    exit 1
}
# OPENCV版本号
OPENCV_VERSION='3.3.1'
# CUDA路径
CUDA_INCLUDE_DIR='/usr/local/cuda/include'
# 获取脚本所在路径
SCRIPT_DIR="$(pwd)"

#安装依赖库
if (
    info "Installing basic dependencies required for OpenCV" &&
    sudo apt-get --assume-yes install build-essential &&
    sudo apt-get --assume-yes install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
)
then
    info "Installing dependencies succeeded."
else
    error "Installing dependencies failed!"
fi

if (
    info "Installing additional dependencies required for this build" &&
    sudo apt-get --assume-yes install libtbb-dev libeigen3-dev libopenblas-dev liblapacke-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk2.0-dev libgtkglext1 libgtkglext1-dev python-dev python-numpy python3-dev python3-numpy
)
then
    info "Installing additional dependencies succeeded."
else
    error "Installing additional dependencies failed!"
fi
# 下载OPENCV
if (
    info "Downloading OpenCV version ${OPENCV_VERSION}" &&
    wget -O opencv-${OPENCV_VERSION}.zip https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip --progress=dot &&
    unzip -q opencv-${OPENCV_VERSION}.zip
)
then
    info "Downloading and extracting OpenCV succeeded."
else
    error "Downloading and extracting OpenCV failed."
fi
# 下载OPENCV_Contrib额外库
if (
    info "Downloading OpenCV Contrib version ${OPENCV_VERSION}" &&
    wget -O opencv_contrib-${OPENCV_VERSION}.zip https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip --progress=dot &&
    unzip -q opencv_contrib-${OPENCV_VERSION}.zip
)
then
    info "Downloading and extracting OpenCV Contrib succeeded."
else
    error "Downloading and extracting OpenCV Contrib failed."
fi

# 这段指令的出处比较复杂,而且结果是运行失败,如果不需要OPENGL支持的可以不考虑该问题
# 具体原因是OPENGL安装有问题,参考链接如下:
# https://devtalk.nvidia.com/default/topic/1007290/jetson-tx2/building-opencv-with-opengl-support-/post/5141945/

if (test "$(uname -m)" = "aarch64")
then
    if (
        info "Patching cuda_gl_interop.h to avoid build failure when OpenGL and CUDA are enabled. (Patching with '--backup')" &&
        cd "${CUDA_INCLUDE_DIR}" &&
        sudo patch -p1 -f --backup -i "${SCRIPT_DIR}/cuda_opengl_interop.patch"
    )
    then
        info "Patching the file to avoid build failure succeeded."
    else
        echo "WARNING: patching cuda_gl_interop.h FAILED. This could cause build failure."
        echo "WARNING: Reference: https://devtalk.nvidia.com/default/topic/1007290/jetson-tx2/building-opencv-with-opengl-support-/post/5141945/"
    fi
fi

一般情况下上面的脚本应该都会运行正常,出了OPENGL安装会有点问题。
安装OPENGL需要的补丁文件如下:复制,保存为cuda_opengl_interop.patch,并和脚本放在同一个文件夹下

diff --git a/cuda_gl_interop.h b/cuda_gl_interop.h
index 0f4aa17..e8c538c 100644
--- a/cuda_gl_interop.h
+++ b/cuda_gl_interop.h
@@ -59,13 +59,13 @@
 
 #else /* __APPLE__ */
 
-#if defined(__arm__) || defined(__aarch64__)
-#ifndef GL_VERSION
-#error Please include the appropriate gl headers before including cuda_gl_interop.h
-#endif
-#else
+//#if defined(__arm__) || defined(__aarch64__)
+//#ifndef GL_VERSION
+//#error Please include the appropriate gl headers before including cuda_gl_interop.h
+//#endif
+//#else
 #include <GL/gl.h>
-#endif
+//#endif
 
 #endif /* __APPLE__ */

2. OPENCV和OPENCV_Contrib编译与安装

这一步会产生下问题,但是不要惊慌,如果出现问题了,请看最后的问题解惑,如果没问题,那就皆大欢喜。good luck to you!!!

使用方法:复制该脚本,保存为opencv_install_script.bash,同上,在终端运行指令bash opencv_install_script.bash

# 编译和安装脚本

info () {
    info_msg=$1
    echo "INFO: $info_msg"
}

error () {
    error_msg=$1
    echo "ERROR: $error_msg"
    exit 1
}

OPENCV_VERSION='3.3.1'
CUDA_INCLUDE_DIR='/usr/local/cuda/include'
SCRIPT_DIR="$(pwd)"

if (
    info "Build and installing OpenCV ${OPENCV_VERSION}." &&
    cd opencv-${OPENCV_VERSION} &&
    mkdir -p build &&
    cd build &&
    cmake -C ${SCRIPT_DIR}/opencv_build_options.cmake .. &&
    make -j4 && # An higher value of -j may speed-up the process a little
    sudo make install
)
then
    info "Building and installation succeeded"
else
    error "Building and installing OpenCV ${OPENCV_VERSION} failed."
fi

在指令:cmake -C ${SCRIPT_DIR}/opencv_build_options.cmake有一个依赖项,需要安装指令设定文件夹。

使用方法:复制指令,保存为文件opencv_v4l2 - opencv_build_options.cmake,并将该文件和上面的bash脚本放在一起。如果您只是想安装部分依赖,请详细查阅自己真正需要编译的部分模块。

#
# Copyright (c) 2017-2018, e-con Systems India Pvt. Ltd.  All rights reserved.
#

set (BUILD_CUDA_STUBS          OFF CACHE BOOL "")
set (BUILD_DOCS                OFF CACHE BOOL "")
set (BUILD_EXAMPLES            OFF CACHE BOOL "")
set (BUILD_IPP_IW              OFF CACHE BOOL "")
set (BUILD_ITT                 OFF CACHE BOOL "")
set (BUILD_JASPER              ON  CACHE BOOL "")
set (BUILD_JPEG                ON  CACHE BOOL "")
set (BUILD_OPENEXR             ON  CACHE BOOL "")
set (BUILD_PACKAGE             OFF CACHE BOOL "")
set (BUILD_PERF_TESTS          OFF CACHE BOOL "")
set (BUILD_PNG                 ON  CACHE BOOL "")
set (BUILD_PROTOBUF            ON  CACHE BOOL "")
set (BUILD_SHARED_LIBS         ON  CACHE BOOL "")
set (BUILD_TBB                 ON  CACHE BOOL "")
set (BUILD_TIFF                ON  CACHE BOOL "")
set (BUILD_TESTS               OFF CACHE BOOL "")
set (BUILD_WEBP                ON  CACHE BOOL "")
set (BUILD_WITH_DEBUG_INFO     OFF CACHE BOOL "")
set (BUILD_WITH_DYNAMIC_IPP    OFF CACHE BOOL "")
set (BUILD_ZLIB                ON  CACHE BOOL "")
set (CMAKE_BUILD_TYPE          "Release" CACHE STRING "")
set (CUDA_BUILD_EMULATION      OFF CACHE BOOL "")
set (CUDA_FAST_MATH            ON  CACHE BOOL "")
# set (CUDA_HOST_COMPILER        "/usr/bin/g++" CACHE STRING "To overcome bad interaction between ccache and nvcc in older cmake versions")
set (CUDA_PROPAGATE_HOST_FLAGS ON  CACHE BOOL "")
set (CV_DISABLE_OPTIMIZATION   OFF CACHE BOOL "")
set (CV_ENABLE_INTRINSICS      ON  CACHE BOOL "")
set (CV_TRACE                  OFF CACHE BOOL "")
set (ENABLE_BUILD_HARDENING    ON  CACHE BOOL "")
# set (ENABLE_CCACHE             ON  CACHE BOOL "")
set (ENABLE_COVERAGE           OFF CACHE BOOL "")
set (ENABLE_CXX11              ON  CACHE BOOL "") # RE-CHECK
set (ENABLE_FAST_MATH          ON  CACHE BOOL "")
set (ENABLE_GNU_STL_DEBUG      OFF CACHE BOOL "")
set (ENABLE_IMPL_COLLECTION    OFF CACHE BOOL "")
set (ENABLE_INSTRUMENTATION    OFF CACHE BOOL "")
set (ENABLE_OMIT_FRAME_POINTER ON  CACHE BOOL "")
set (ENABLE_PROFILING          OFF CACHE BOOL "")
set (ENABLE_PYLINT             OFF CACHE BOOL "")
set (INSTALL_C_EXAMPLES        OFF CACHE BOOL "")
set (INSTALL_PYTHON_EXAMPLES   OFF CACHE BOOL "")
set (INSTALL_TESTS             OFF CACHE BOOL "")
set (WITH_CUBLAS               ON  CACHE BOOL "")
set (WITH_CUDA                 ON  CACHE BOOL "")
set (WITH_CUFFT                ON  CACHE BOOL "")
set (WITH_EIGEN                ON  CACHE BOOL "")
set (WITH_GSTREAMER            ON  CACHE BOOL "")
set (WITH_GTK                  ON  CACHE BOOL "")
set (WITH_GTK_2_X              ON  CACHE BOOL "")
set (WITH_IPP                  OFF CACHE BOOL "")
set (WITH_ITT                  OFF CACHE BOOL "")
set (WITH_JASPER               ON  CACHE BOOL "")
set (WITH_JPEG                 ON  CACHE BOOL "")
set (WITH_LAPACK               ON  CACHE BOOL "")
set (WITH_LIBV4L               OFF CACHE BOOL "")
set (WITH_NVCUVID              OFF CACHE BOOL "")
set (WITH_OPENEXR              ON  CACHE BOOL "")
set (WITH_OPENGL               ON  CACHE BOOL "")
set (WITH_OPENMP               ON  CACHE BOOL "")
set (WITH_PNG                  ON  CACHE BOOL "")
set (WITH_PTHREADS_PF          ON  CACHE BOOL "")
set (WITH_QT                   OFF CACHE BOOL "FIXME: Installing Qt dependency results in build failure due to linking error. Workaround to be explored.")
set (WITH_TBB                  ON  CACHE BOOL "")
set (WITH_TIFF                 ON  CACHE BOOL "")
set (WITH_V4L                  ON  CACHE BOOL "")
set (WITH_WEBP                 ON  CACHE BOOL "")
set (OPENCV_EXTRA_MODULES_PATH        "../../opencv_contrib-3.3.1/modules" CACHE STRING "Build opencv_contrib")

运行指令之后,整个编译和安装过程将会自动进行。

3. 安装问题释疑

除了刚才的OPENGL安装问题之外,还有额外库安装问题也可能出现问题。

解决方案:重新编译Cmake,更新,执行以下指令:

wget --no-check-certificate https://cmake.org/files/v3.9/cmake-3.9.0.tar.gz
tar -zxvf cmake-3.9.0.tar.gz
cd cmake-3.9.0
# need to build against system curl to get https support
# -> install devel packages
sudo apt-get install libcurl4-gnutls-dev
sudo apt-get install zlib1g-dev
./bootstrap --system-curl
make
sudo make install

本来应该将这个问题放在前面的,但是有可能有人不会遇到这个问题,就放在后面了,仅供参考,这个解决方案确实完美地解决了我所遇到的问题。

  • 编译出现:Lack of support for escape characters during log output。至于为何会出现这样的问题,我不是很懂。……

这里只提供解决方案,不解释原因:
改变opencv/cmake/OpenCVDownload.cmake文件下的lines 37-39:

  macro(ocv_download_log)
    file(APPEND "${OPENCV_DOWNLOAD_LOG}" "${ARGN}\n")
  endmacro()

  function(ocv_download_log)
    file(APPEND "${OPENCV_DOWNLOAD_LOG}" "${ARGN}\n")
  endfunction()

4. 相关参考汇总

  1. 安装脚本的由来解释
  2. 安装脚本所在仓库
  3. 编译额外库出现的问题
  4. 比较好的安装教程一
  5. 额外库安装教程
  6. 本教程的目标依赖安装教程,就是支持深度学习

5. 结束语

写博客真费劲,总感觉写一堆乱七八糟的玩意,也不知道有没有看懂,还是说给别人带来了更大的困扰,还是录制视屏来的比较直接,但是TX2上没有什么好的录屏工具,算了!我个学术垃圾,哪里管得了那么多,各位看官将就着看吧,希望对你有用。

相关文章

网友评论

      本文标题:TX2的Ubuntu 16.04上安装OpenCV3.3.1

      本文链接:https://www.haomeiwen.com/subject/qbhzfctx.html