Tegra平台构建OpenCV_Building OpenCV for Tegra with CUDA
內(nèi)容目錄
- 獲取源碼
- 編譯準(zhǔn)備
- 編譯配置
- 編譯Opencv
- 測(cè)試Opencv
- 安裝Opencv
- 編譯Opencv2.4.x
- CMake參數(shù)指南
Tegra平臺(tái)下基于CUDA使用Opencv
本文檔是構(gòu)建OpenCV庫的基本指南,CUDA支持在Tegra環(huán)境中使用。它涵蓋了從源代碼中構(gòu)建版本3.1.0庫的基本元素,包括三種不同類型的平臺(tái):
- NVIDIA DRIVE? PX 2 (V4L)
- NVIDIA??Tegra??Linux Driver Package (L4T)
- Desktop Linux (Ubuntu 14.04 LTS and 16.04 LTS)
本文檔并不是對(duì)構(gòu)建OpenCV時(shí)可用的所有選項(xiàng)的詳盡指南。具體來說,它涵蓋了構(gòu)建每個(gè)平臺(tái)時(shí)所使用的基本選項(xiàng),但不包括任何不需要的選項(xiàng)(或與它們的默認(rèn)值保持不變)。另外,CUDA工具包的安裝也不在這里。
本文檔的重點(diǎn)是構(gòu)建OpenCV的3.1.0版本,但是這里的指導(dǎo)原則也可以從git存儲(chǔ)庫的主分支中構(gòu)建。關(guān)于OpenCV 2.4.13版本的構(gòu)建,有些CMake選項(xiàng)存在差異,這在OpenCV 2.4的構(gòu)建中總結(jié)如下Building OpenCV 2.4.X部分。
大多數(shù)配置命令都基于安裝了CUDA 8.0的系統(tǒng)。在Jetson TK1的例子中,使用了一個(gè)較舊的CUDA,因?yàn)樵撈脚_(tái)不支持8.0。這些指令可能也適用于舊版本的CUDA,但只測(cè)試了8.0。
關(guān)于本地編譯和交叉編譯的說明
OpenCV構(gòu)建系統(tǒng)支持所有支持平臺(tái)的本機(jī)編譯,以及ARM等平臺(tái)的交叉編譯。本機(jī)編譯過程更簡(jiǎn)單,而交叉編譯通常更快。
目前,本文只關(guān)注本機(jī)編譯。
獲取源碼
- ?OpenCV downloads?下載頁面
- ?從?GitHub?clone倉庫
對(duì)于本指南,重點(diǎn)是使用git存儲(chǔ)庫。這是因?yàn)镺penCV的3.1.0版本如果不應(yīng)用git存儲(chǔ)庫中的一些微小的上游更改,就無法使用CUDA 8.0構(gòu)建。
# Clone the opencv repository locally: $ git clone https://github.com/opencv/opencv.git想要編譯3.1.0版本?(而不是編譯最新的版本), 你必須checkout到3.1.0分支:
$ cd opencv $ git checkout -b v3.1.0 3.1.0注意:該操作在你克隆倉庫中創(chuàng)建一個(gè)新的本地分支。
有一些上游更改必須通過git cherry-pick命令進(jìn)行應(yīng)用。第一種方法是應(yīng)用修復(fù)程序,具體地使用8.0版本的CUDA,而不是3.1.0版本的一部分:
# While still in the opencv directory: $ git cherry-pick 10896你將看到以下命令的輸出:
[v3.1.0 d6d69a7] GraphCut deprecated in CUDA 7.5 and removed in 8.0Author: Vladislav Vinogradov <vlad.vinogradov@itseez.com>1 file changed, 2 insertions(+), 1 deletion(-)其次,對(duì)于CMake宏調(diào)用有一個(gè)修復(fù),這在一些系統(tǒng)上是有問題的:
$ git cherry pick cdb9c你應(yīng)該看到如下類似輸出:
[v3.1.0-28613 e5ac2e4] gpu samples: fix REMOVE_ITEM errorAuthor: Alexander Alekhin <alexander.alekhin@itseez.com>1 file changed, 1 insertion(+), 1 deletion(-)需要的最后一個(gè)上游修復(fù)涉及到與開發(fā)人員包捆綁在一起的pkg-config配置文件 (libopencv-dev):
$ git cherry-pick 24dbb你應(yīng)該看到如下類似輸出:
[v3.1.0 3a6d7ab] pkg-config: modules list contains only OpenCV modules (fixes #5852)Author: Alexander Alekhin <alexander.alekhin@itseez.com>1 file changed, 7 insertions(+), 4 deletions(-)到此為止,opencv倉庫已經(jīng)可以編譯了。
OpenCV Extra
opencv_extra倉庫包含OpenCV庫的額外數(shù)據(jù),包括測(cè)試和演示使用的數(shù)據(jù)文件。必須分別克隆:
# In the same base directory from which you cloned OpenCV: $ git clone https://github.com/opencv/opencv_extra.git與OpenCV源代碼一樣,必須使用與上面相同的方法將源代碼樹設(shè)置為3.1.0版本。當(dāng)您從特定的標(biāo)記構(gòu)建時(shí),兩個(gè)存儲(chǔ)庫都必須在該標(biāo)記處簽出。
$ cd opencv_extra $ git checkout -b v3.1.0 3.1.0如果你不打算運(yùn)行測(cè)試或安裝測(cè)試數(shù)據(jù)以及示例和示例程序,你可以選擇不獲取這個(gè)存儲(chǔ)庫。如果在CMake的調(diào)用中沒有引用它,就不會(huì)使用它。
注意:如果您計(jì)劃運(yùn)行測(cè)試,一些測(cè)試期望數(shù)據(jù)出現(xiàn),如果沒有數(shù)據(jù)就會(huì)失敗。
編譯準(zhǔn)備
要編譯OpenCV,您需要一個(gè)目錄來創(chuàng)建配置并構(gòu)建庫。您還需要一些OpenCV所依賴的第三方庫。
Ubuntu Linux準(zhǔn)備
這些是在Linux上為Tegra編譯OpenCV的基本要求:
- CMake 2.8.10 or newer
- CUDA toolkit 8.0 (7.0 or 7.5 may also be used)
- Build tools (make, gcc, g++)
- Python 2.6 or greater
這些要求是與平臺(tái)無關(guān)的(DRIVE PX 2, Desktop等等)。
在Linux上編譯opencv所需要的軟件包:
- libglew-dev
- libtiff5-dev
- zlib1g-dev
- libjpeg-dev
- libpng12-dev
- libjasper-dev
- libavcodec-dev
- libavformat-dev
- libavutil-dev
- libpostproc-dev
- libswscale-dev
- libeigen3-dev
- libtbb-dev
- libgtk2.0-dev
- pkg-config
上面的一些包在Ubuntu Linux系統(tǒng)的通用庫中。如果你還沒有啟用該倉庫,那么在嘗試安裝上面列出的所有包之前,需要執(zhí)行以下操作:
$ sudo apt-add-repository universe $ sudo apt-get update可以將以下命令粘貼到shell中,以便安裝所需的包:
$ sudo apt-get install \libglew-dev \libtiff5-dev \zlib1g-dev \libjpeg-dev \libpng12-dev \libjasper-dev \libavcodec-dev \libavformat-dev \libavutil-dev \libpostproc-dev \libswscale-dev \libeigen3-dev \libtbb-dev \libgtk2.0-dev \pkg-config(為了可讀性,添加了換行符和延續(xù)字符 / Line-breaks and continuation characters are added for readability.)
如果希望構(gòu)建Python綁定,還需要為Python 2和Python 3中的任意一個(gè)或兩個(gè)都提供適當(dāng)?shù)陌?
- python-dev / python3-dev
- python-numpy / python3-numpy
- python-py / python3-py
- python-pytest / python3-pytest
命令如下:
$ sudo apt-get install python-dev python-numpy python-py python-pytest # And, optionally: $ sudo apt-get install python3-dev python3-numpy python3-py python3-pytest一旦安裝了所有必要的包,你就可以做編譯前的配置了
準(zhǔn)備編譯區(qū)域
Software projects that use the CMake system for configuring their builds expect the actual builds to be done outside of the source tree itself. For configuring and building OpenCV, create a directory called "build" in the same base directory into which you cloned the git repositories:
使用CMake系統(tǒng)來配置構(gòu)建的軟件項(xiàng)目期望在源代碼樹本身之外完成實(shí)際的構(gòu)建。為了配置和構(gòu)建OpenCV,在復(fù)制git存儲(chǔ)庫的同一基本目錄中創(chuàng)建一個(gè)名為“build”的目錄:
$ mkdir build $ cd buildYou are now ready to configure and build OpenCV.
現(xiàn)在可以配置和編譯OpenCV了。
編譯前的配置
The CMake configuration options given below for the different platforms are targeted towards the functionality needed for Tegra. They are based on the original configuration options used for building OpenCV 2.4.13.
下面給出的針對(duì)不同平臺(tái)的CMake配置選項(xiàng)針對(duì)的是Tegra所需的功能。它們基于構(gòu)建OpenCV 2.4.13所用的原始配置選項(xiàng)。
The build of OpenCV is configured with CMake. If run with no parameters, it detects what it needs to know about your system. However, it may have difficulty finding the CUDA files if they are not in a standard location, and it may try to build some options that you might otherwise not want included, so the following invocations of CMake are recommended.
使用Cmake對(duì)Opencv進(jìn)行編譯。如果不帶參數(shù)運(yùn)行,它會(huì)檢測(cè)到需要了解的系統(tǒng)信息。但是,如果CUDA文件不在標(biāo)準(zhǔn)位置,它可能很難找到它們,并且它可能嘗試構(gòu)建一些您可能不希望包含的選項(xiàng),因此建議使用以下CMake調(diào)用。
In each?cmake?command listed in the following sub-sections, line-breaks and indentation are added for readability. Continuation characters are also added in examples for Linux-based platforms, allowing you to copy and paste the examples directly into a shell. When entering these commands by hand, enter the command and options as a single line. For a detailed explanation of the parameters passed to?cmake, see the "CMake Parameter Reference" section.
在下面的小節(jié)中列出的每個(gè)cmake命令中,為了可讀性,都添加了換行符和縮進(jìn)。在基于linux平臺(tái)的示例中還添加了延續(xù)字符,允許您直接將示例復(fù)制粘貼到shell中。手動(dòng)輸入這些命令時(shí),將命令和選項(xiàng)作為一行輸入。有關(guān)傳遞給cmake的參數(shù)的詳細(xì)說明,請(qǐng)參閱“cmake參數(shù)引用”一節(jié)。
For the Linux-based platforms, the shown value for the?CMAKE_INSTALL_PREFIX?parameter is?/usr. You can set this to whatever you want, based on the layout of your system.
對(duì)于基于linux的平臺(tái),CMAKE_INSTALL_PREFIX參數(shù)的顯示值是/usr。您可以根據(jù)系統(tǒng)的布局將其設(shè)置為任意值。
In each of the?cmake?invocations below, the last parameter,?OPENCV_TEST_DATA_PATH, tells the build system where to find the test-data that is provided by the?opencv_extra?repository. When this is included, a?make install?installs this test-data alongside the libraries and example code, and a?make testautomatically provides this path to the tests that have to load data from it. If you did not clone the?opencv_extra?repository, do not include this parameter.
在下面的cmake調(diào)用中,最后一個(gè)參數(shù)OPENCV_TEST_DATA_PATH告訴構(gòu)建系統(tǒng)在哪里找到opencv_extra存儲(chǔ)庫提供的測(cè)試數(shù)據(jù)。當(dāng)包含這部分內(nèi)容時(shí),make install會(huì)將這些測(cè)試數(shù)據(jù)與庫和示例代碼一起安裝,而make testtest會(huì)自動(dòng)提供這個(gè)路徑,以供必須從其中加載數(shù)據(jù)的測(cè)試使用。如果沒有克隆opencv_extra存儲(chǔ)庫,不要包含這個(gè)參數(shù)。
Vibrante V4L 環(huán)境下編譯配置
Supported platform: Drive PX 2
支持的平臺(tái):Drive PX 2
$ cmake \-DCMAKE_BUILD_TYPE=Release \-DCMAKE_INSTALL_PREFIX=/usr \-DBUILD_PNG=OFF \-DBUILD_TIFF=OFF \-DBUILD_TBB=OFF \-DBUILD_JPEG=OFF \-DBUILD_JASPER=OFF \-DBUILD_ZLIB=OFF \-DBUILD_EXAMPLES=ON \-DBUILD_opencv_java=OFF \-DBUILD_opencv_python2=ON \-DBUILD_opencv_python3=OFF \-DENABLE_NEON=ON \-DWITH_OPENCL=OFF \-DWITH_OPENMP=OFF \-DWITH_FFMPEG=ON \-DWITH_GSTREAMER=OFF \-DWITH_GSTREAMER_0_10=OFF \-DWITH_CUDA=ON \-DWITH_GTK=ON \-DWITH_VTK=OFF \-DWITH_TBB=ON \-DWITH_1394=OFF \-DWITH_OPENEXR=OFF \-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \-DCUDA_ARCH_BIN=6.2 \-DCUDA_ARCH_PTX="" \-DINSTALL_C_EXAMPLES=ON \-DINSTALL_TESTS=OFF \-DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \../opencvThe configuration provided above builds the Python bindings for Python 2 (but not Python 3) as part of the build process. If you want the Python 3 bindings (or do not want the Python 2 bindings), change the values of?BUILD_opencv_python2?and/or?BUILD_opencv_python3?as needed. To enable bindings, set the value to?ON, to disable them set it to?OFF:
上面提供的配置作為構(gòu)建過程的一部分為Python 2(而不是Python 3)構(gòu)建Python綁定。如果希望使用Python 3綁定(或者不希望使用Python 2綁定),請(qǐng)根據(jù)需要更改BUILD_opencv_python2和/或BUILD_opencv_python3的值。要啟用綁定,請(qǐng)將值設(shè)置為ON,以禁用它們將其設(shè)置為OFF:
-DBUILD_opencv_python2=OFFJetson L4T 環(huán)境下編譯配置
支持的平臺(tái):
- Jetson TK1
- Jetson TX1
Configuration is slightly different for the Jetson TK1 and the Jetson TX1 systems.
Jetson TK1和Jetson TX1系統(tǒng)的配置略有不同。
Jetson TK1
$ cmake \-DCMAKE_BUILD_TYPE=Release \-DCMAKE_INSTALL_PREFIX=/usr \-DCMAKE_CXX_FLAGS=-Wa,-mimplicit-it=thumb \-DBUILD_PNG=OFF \-DBUILD_TIFF=OFF \-DBUILD_TBB=OFF \-DBUILD_JPEG=OFF \-DBUILD_JASPER=OFF \-DBUILD_ZLIB=OFF \-DBUILD_EXAMPLES=ON \-DBUILD_opencv_java=OFF \-DBUILD_opencv_python2=ON \-DBUILD_opencv_python3=OFF \-DENABLE_NEON=ON \-DWITH_OPENCL=OFF \-DWITH_OPENMP=OFF \-DWITH_FFMPEG=ON \-DWITH_GSTREAMER=OFF \-DWITH_GSTREAMER_0_10=OFF \-DWITH_CUDA=ON \-DWITH_GTK=ON \-DWITH_VTK=OFF \-DWITH_TBB=ON \-DWITH_1394=OFF \-DWITH_OPENEXR=OFF \-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-6.5 \-DCUDA_ARCH_BIN=3.2 \-DCUDA_ARCH_PTX="" \-DINSTALL_C_EXAMPLES=ON \-DINSTALL_TESTS=OFF \-DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \../opencvNote:?This uses CUDA 6.5, not 8.0.
注意:這里編譯時(shí)使用的版本為6.5,而不是8.0
Jetson TX1
$ cmake \-DCMAKE_BUILD_TYPE=Release \-DCMAKE_INSTALL_PREFIX=/usr \-DBUILD_PNG=OFF \-DBUILD_TIFF=OFF \-DBUILD_TBB=OFF \-DBUILD_JPEG=OFF \-DBUILD_JASPER=OFF \-DBUILD_ZLIB=OFF \-DBUILD_EXAMPLES=ON \-DBUILD_opencv_java=OFF \-DBUILD_opencv_python2=ON \-DBUILD_opencv_python3=OFF \-DENABLE_PRECOMPILED_HEADERS=OFF \-DWITH_OPENCL=OFF \-DWITH_OPENMP=OFF \-DWITH_FFMPEG=ON \-DWITH_GSTREAMER=OFF \-DWITH_GSTREAMER_0_10=OFF \-DWITH_CUDA=ON \-DWITH_GTK=ON \-DWITH_VTK=OFF \-DWITH_TBB=ON \-DWITH_1394=OFF \-DWITH_OPENEXR=OFF \-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \-DCUDA_ARCH_BIN=5.3 \-DCUDA_ARCH_PTX="" \-DINSTALL_C_EXAMPLES=ON \-DINSTALL_TESTS=OFF \-DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \../opencvNote:?This configuration does not set the?ENABLE_NEON?parameter.
注意:這個(gè)配置沒有設(shè)置ENABLE_NEON參數(shù)。
Ubuntu Desktop Linux 環(huán)境下編譯配置
支持的平臺(tái):
- Ubuntu Desktop Linux 14.04 LTS
- Ubuntu Desktop Linux 16.04 LTS
The configuration options given to?cmake?below are targeted towards the functionality needed for Tegra. For a desktop system, you may wish to adjust some options to enable (or disable) certain features. The features enabled below are based on the building of OpenCV 2.4.13.
下面提供給cmake的配置選項(xiàng)針對(duì)的是Tegra所需的功能。對(duì)于桌面系統(tǒng),你可能希望調(diào)整一些選項(xiàng)以啟用(或禁用)某些特性。下面啟用的特性基于OpenCV 2.4.13的構(gòu)建。
$ cmake \-DCMAKE_BUILD_TYPE=Release \-DCMAKE_INSTALL_PREFIX=/usr \-DBUILD_PNG=OFF \-DBUILD_TIFF=OFF \-DBUILD_TBB=OFF \-DBUILD_JPEG=OFF \-DBUILD_JASPER=OFF \-DBUILD_ZLIB=OFF \-DBUILD_EXAMPLES=ON \-DBUILD_opencv_java=OFF \-DBUILD_opencv_python2=ON \-DBUILD_opencv_python3=OFF \-DWITH_OPENCL=OFF \-DWITH_OPENMP=OFF \-DWITH_FFMPEG=ON \-DWITH_GSTREAMER=OFF \-DWITH_GSTREAMER_0_10=OFF \-DWITH_CUDA=ON \-DWITH_GTK=ON \-DWITH_VTK=OFF \-DWITH_TBB=ON \-DWITH_1394=OFF \-DWITH_OPENEXR=OFF \-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \-DCUDA_ARCH_BIN='3.0 3.5 5.0 6.0 6.2' \-DCUDA_ARCH_PTX="" \-DINSTALL_C_EXAMPLES=ON \-DINSTALL_TESTS=OFF \-DOP 同樣需要注意的是:這里的cuda版本為8.0,根據(jù)自己需要可以更改?
總結(jié)
以上是生活随笔為你收集整理的Tegra平台构建OpenCV_Building OpenCV for Tegra with CUDA的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql中没有sock文件_mysql
- 下一篇: thinkpadt410接口介绍_【Th