#Header1_HeaderTitle{ font-size:50px }

在Jetson TX2上安装OpenCV(3.4.0)

参考文章:How to Install OpenCV (3.4.0) on Jetson TX2

与参考文章大部分都是相似的,如果不习惯看英文,可以看看我下面的描述

 

在我们使用python3进行编程时,import cv2不起作用。由于预先安装的opencv是与python2.7绑定的,如果想在python3环境下使用,我们需要重新编译opencv

 

先决条件

已经在Jetson TX2上安装了JetPack-3.3(或JetPack-3.2.1或JetPack-3.1)

 

安装步骤

首先清理旧的opencv包,并构建opencv所需的依赖项

1. 删除所有由JetPack(或OpenCV4Tegra)安装的旧opencv内容

sudo apt-get purge libopencv*

2.我更喜欢使用新版本的numpy(与pip一起安装),所以我也要删除这个python-numpy apt包

sudo apt-get purge python-numpy

3. 删除其他未使用的apt包

sudo apt autoremove

4. 将所有已安装的apt软件包升级到最新版本(可选做)

sudo apt-get update
sudo apt-get dist-upgrade

5. 将gcc apt包更新到最新版本(强烈推荐)

sudo apt-get install --only-upgrade g++-5 cpp-5 gcc-5

6. 基于jetson安装opencv依赖项

sudo apt-get install build-essential make cmake cmake-curses-gui \
                       g++ libavformat-dev libavutil-dev \
                       libswscale-dev libv4l-dev libeigen3-dev \
                       libglew-dev libgtk2.0-dev

7. 为gstreamer安装依赖项

sudo apt-get install libdc1394-22-dev libxine2-dev \
                       libgstreamer1.0-dev \
                       libgstreamer-plugins-base1.0-dev

8. 安装其他依赖项

sudo apt-get install libjpeg8-dev libjpeg-turbo8-dev libtiff5-dev \
                       libjasper-dev libpng12-dev libavcodec-dev
sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev \
                       libatlas-base-dev gfortran
sudo apt-get install libopenblas-dev liblapack-dev liblapacke-dev

9. 安装QT5的依赖项

sudo apt-get install qt5-default

10. 安装python3的依赖项

sudo apt-get install python3-dev python3-pip python3-tk
sudo pip3 install numpy
sudo pip3 install matplotlib

修改matplotlibrc的41行,'backend      : TkAgg'

sudo vim /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc

vim技巧 : 按Esc,set nu(显示行号)

11. 安装python2的依赖项

sudo apt-get install python-dev python-pip python-tk
sudo pip2 install numpy
sudo pip2 install matplotlib

修改matplotlibrc的41行,'backend      : TkAgg'

sudo vim /usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc

在下载和编译opencv-3.4.0之前,需要先进行一些修改。具体点说是修改  /usr/local/cuda/include/cuda_gl_interop.h 并修复libGL.so的符号链接

sudo vim /usr/local/cuda/include/cuda_gl_interop.h
cd /usr/lib/aarch64-linux-gnu/
sudo ln -sf tegra/libGL.so libGL.so

下面是cuda_gl_interop.h的62行到68行,修改后如下:

//#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

 

接下来,下载opencv-3.4.0源代码,cmake并编译。注意,opencv_contrib模块(cnn/dnn之类的东西)会在pycaffe上引起问题,所以经过一些实验之后,我决定完全不包含这些模块。

mkdir -p ~/src
cd ~/src
wget https://github.com/opencv/opencv/archive/3.4.0.zip \
       -O opencv-3.4.0.zip
unzip opencv-3.4.0.zip

2. Build opencv (CUDA_ARCH_BIN="6.2" for TX2, or "5.3" for TX1)
cd ~/src/opencv-3.4.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D WITH_CUDA=ON -D CUDA_ARCH_BIN="6.2" -D CUDA_ARCH_PTX="" \
        -D WITH_CUBLAS=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON \
        -D ENABLE_NEON=ON -D WITH_LIBV4L=ON -D BUILD_TESTS=OFF \
        -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF \
        -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j4
sudo make install

如果下载opencv源码较慢,可以在windows的浏览器中输入网址 https://github.com/opencv/opencv/archive/3.4.0.zip 手动下载源码包,之后将源码包放入TX2

 

验证安装是否成功

ls /usr/local/lib/python3.5/dist-packages/cv2.*
/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-aarch64-linux-gnu.so
ls /usr/local/lib/python2.7/dist-packages/cv2.*
/use/local/lib/python2.7/dist-packages/cv2.so
python3 -c 'import cv2; print(cv2.__version__)'
3.4.0
python2 -c 'import cv2; print(cv2.__version__)'
3.4.0

 

在Jetson TX2上正确安装了opencv-3.4.0之后,我们可以使用python脚本捕获和显示来自Jetson板载摄像机、USB网络摄像头或IP摄像头的实时视频。跟随这篇文章:在Jetson TX2上捕获、显示摄像头视频

 

posted @ 2019-10-04 14:28  墨麟非攻  阅读(7407)  评论(0编辑  收藏  举报