【Xilinx-Petalinux學習】-04-OpenCV的移植

交叉編譯PC平臺 VMware12, CentOS 6.5 32 bitjava

在VMware中安裝CentOS,用戶名:xilinx-arm-opencv 密碼:rootpython

 

至於這裏爲何用CentOS,而不是Ubuntu,是由於CentOS的內核能夠支持V4L,而Ubuntu由於內核版本較高,已經沒有了V4L的支持,只有V4L2。linux

總之爲了方便仍是再安裝一個CentOS的虛擬機吧。android

 

step1: 安裝包、工具下載bash

首先下載各類依賴庫源文件和工具。提到的都是我用到的版本,已經驗證過,都是能夠互相配合可用的。ide

OpenCV依賴庫:
工具

zlib-1.2.7.tar.gzui

jpegsrc.v9b.tar.gzspa

libpng-1.5.14.tar.gzprototype

last_stable_x264.tar.bz2 (x264-snapshot-20161127-2245-stable)

xvidcore-1.3.2.tar.gz

tiff-4.0.7.tar.gz

ffmpeg-0.10.16.tar.bz2

到時候這些依賴庫須要按照上面的順序安裝。

交叉編譯工具:

xilinx-2011.09-50-arm-xilinx-linux-gnueabi.bin

可百度去搜索下載,也能夠用下面位大俠共享的網盤。網址:http://pan.baidu.com/share/link?shareid=2118817891&uk=2047711911&fid=131808308022716

還能夠從網上下載較新版本的源碼,但我尚未搞懂怎麼去用。網址:https://www.xilinx.com/guest_resources/member/mb_gnu/xilinx-2015.05-17-arm-xilinx-linux-gnueabi.src.tar.bz2

OpenCV:

opencv-2.4.9

從官網下載Linux 32 bit版本的源碼就能夠了

 

step2: 交叉編譯工具的安裝

首先安裝交叉編譯工具須要用到的一些依賴庫:

$ su -
yum update
yum install glibc-devel.i686
yum install gtk2-devel.i686
yum install libcanberra.i686
yum install libcanberra-gtk2.i686
yum install PackageKit-gtk-module.i686
yum install GConf2.i686
yum install ncurses-libs.i686
yum install xulrunner.i686

 

再來安裝交叉編譯工具。

複製 xilinx-2011.09-50-arm-xilinx-linux-gnueabi.bin 安裝包到Linux中,運行:

./xilinx-2011.09-50-arm-xilinx-linux-gnueabi.bin

就會彈出相似Windows安裝軟件同樣的界面,一直點Next直到安裝成功。

接着添加環境變量,運行命令:

gedit ~/.bashrc
#在文件最後加入下面幾行文本
export ARCH=arm
export CROSS_COMPILE=arm-xilinx-linux-gnueabi-
export PATH=/root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/:$PATH
#保存並退出
#source source ~/.bashrc

這樣,咱們就可以直接調用 arm-xilinx-linux-gnueabi-gcc指令了。

假如須要更新版本,卸載當前版本的話,運行:

/root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/uninstall/Uninstall_Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/Uninstall_Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux

 

step3: 交叉編譯OpenCV須要的依賴庫

先建立用來保存依賴庫和OpenCV庫的文件夾,執行命令:

#新建安裝目錄
mkdir -p /usr/local/arm/opencv-lib
mkdir -p /usr/local/arm/opencv-arm
#添加環境變量
export ZYNQ_CV_INSTALL=/usr/local/arm/opencv-lib
export CC=arm-xilinx-linux-gnueabi-gcc

解壓全部的依賴庫源代碼,而後分別進入各個源碼的文件夾進行交叉編譯。必定要按照順序安裝,由於它們之間也有依賴關係。

zlib安裝

./configure --prefix=$ZYNQ_CV_INSTALL --shared
make
make install

jpeg安裝

./configure --prefix=$ZYNQ_CV_INSTALL --host=arm-xilinx-linux-gnueabi --enable-shared
make
make install

libpng安裝

./configure --prefix=$ZYNQ_CV_INSTALL --host=arm-xilinx-linux-gnueabi --enable-arm-neon --enable-shared --with-pkgconfigdir=$ZYNQ_CV_INSTALL/lib/pkgconfig LDFLAGS=-L$ZYNQ_CV_INSTALL/lib CPPFLAGS=-I$ZYNQ_CV_INSTALL/include
make
make install

x264安裝

./configure --host=arm-linux --cross-prefix=arm-xilinx-linux-gnueabi- --enable-shared  --disable-asm --prefix=$ZYNQ_CV_INSTALL
make
make install

xvidcore安裝

cd build/generic/
./configure --prefix=$ZYNQ_CV_INSTALL --host=arm-xilinx-linux-gnueabi  --disable-assembly
make
make install

tiff安裝

./configure --prefix=$ZYNQ_CV_INSTALL --host=arm-xilinx-linux-gnueabi --enable-shared LDFLAGS=-L$ZYNQ_CV_INSTALL/lib CFLAGS=-I$ZYNQ_CV_INSTALL/include
make
make install

ffmpeg安裝

./configure --prefix=$ZYNQ_CV_INSTALL --enable-shared --disable-static --enable-gpl --enable-cross-compile --arch=arm --disable-stripping --target-os=linux --enable-libx264 --enable-libxvid --cc=arm-xilinx-linux-gnueabi-gcc --enable-swscale --extra-cflags=-I$ZYNQ_CV_INSTALL/include --extra-ldflags=-L$ZYNQ_CV_INSTALL/lib --disable-asm
make
make install

爲了在編譯OpenCV時確保ffmpeg的正常連接,須要添加PKG_CONFIG_PATH環境變量

gedit /etc/bashrc
#在文件最後添加以下內容
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$ZYNQ_CV_INSTALL/lib/pkgconfig
export PKG_CONFIG_PATH
#保存並退出
#source
source /etc/bashrc

這些我都試了不少遍了,就按照上面的方法配置、編譯和安裝便可,就不去詳細的說明各個配置參數的意義了。

 

step4: 交叉編譯OpenCV 2.4.9

安裝cmake:

yum install cmake
yum install cmake-gui

解壓OpenCV 2.4.9源碼,進行配置:

#進入文件夾
cd opencv-2.4.9
#創建並進入編譯文件夾
mkdir build
cd build

#建立文件
gedit toolchain.cmake
#修改文件,在其中加入以下內容:
###########user defined#############
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR arm )
set( CMAKE_C_COMPILER arm-xilinx-linux-gnueabi-gcc )
set( CMAKE_CXX_COMPILER arm-xilinx-linux-gnueabi-g++ )
###########user defined#############
set( CMAKE_FIND_ROOT_PATH "/usr/local/arm/opencv-lib" )
set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
######################################
#保存並退出

#添加LDFLAGS環境變量
export LDFLAGS=-Wl,-rpath-link,/usr/local/arm/opencv-lib/lib

#在CentOS中,使OpenCV支持V4L
cp /usr/include/linux/videodev.h /root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/arm-xilinx-linux-gnueabi/libc/usr/include/linux/

#cmake
cmake -D CMAKE_TOOLCHAIN_FILE=toolchain.cmake -D CMAKE_INSTALL_PREFIX=/usr/local/arm/opencv-arm ../

#cmake配置
cmake-gui

接着就打開了cmake-gui的配置界面,在裏面能夠配置opencv的編譯選項。

 

在cmake GUI中,勾選 Grouped 和 Advanced 選項框,覈對如下信息:

1. 源碼與安裝目錄。須要跟下面如出一轍

Where is the source code: /home/xilinx-arm-opencv/PetaLinux/package/opencv/opencv-2.4.9

Where to build the binaries: /home/xilinx-arm-opencv/PetaLinux/package/opencv/opencv-2.4.9/build

2. CMAKE項。須要跟下面如出一轍

CMAKE_INSTALL_PREFIX : /usr/local/arm/opencv-arm

CMAKE_EXE_LINKER_FLAGS : -Wl,-rpath-link,/usr/local/arm/opencv-lib

CMAKE_C_COMPILER : /root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-gcc

CMAKE_CXX_COMPILER : /root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-g++

3.WITH項。只勾選以下選項,其餘都不勾選

WITH_FFMPEG

WITH_JASPER

WITH_JPEG

WITH_OPENEXR

WITH_PNG

WITH_TIFF

WITH_V4L

界面以下圖:

配置完成後點擊Configure按鈕,會打印出以下信息:(特別須要注意標紅的LOG信息與個人是否相同!)

 

General configuration for OpenCV 2.4.9 =====================================
  Version control:               unknown

  Platform:
    Host:                        Linux 2.6.32-431.el6.i686 i686
    Target:                      Linux arm
    CMake:                       2.8.12.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/gmake
    Configuration:               Release

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                /root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-g++  (ver 4.6.1)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-gcc
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,-rpath-link,/usr/local/arm/opencv-lib
    Linker flags (Debug):        -Wl,-rpath-link,/usr/local/arm/opencv-lib
    Precompiled headers:         YES

  OpenCV modules:
    To be built:                 core flann imgproc highgui features2d calib3d ml video legacy objdetect photo gpu nonfree contrib stitching superres ts videostab
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 androidcamera dynamicuda java ocl python viz

  GUI:
    QT:                          NO
    GTK+ 2.x:                    NO
    GThread :                    NO
    GtkGlExt:                    NO
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O:
    ZLib:                        /usr/local/arm/opencv-lib/lib/libz.so (ver 1.2.7)
    JPEG:                        /usr/local/arm/opencv-lib/lib/libjpeg.so (ver 90)
    PNG:                         /usr/local/arm/opencv-lib/lib/libpng.so (ver 1.5.14)
    TIFF:                        /usr/local/arm/opencv-lib/lib/libtiff.so (ver 42 - 4.0.7)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES
      codec:                     YES (ver 53.61.100)
      format:                    YES (ver 53.32.100)
      util:                      YES (ver 51.35.100)
      swscale:                   YES (ver 2.1.100)
      gentoo-style:              YES
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    UniCap:                      NO
    UniCap ucil:                 NO
    V4L/V4L2:                    YES/YES
    XIMEA:                       NO
    Xine:                        NO

  Other third-party libraries:
    Use IPP:                     NO
    Use Eigen:                   NO
    Use TBB:                     NO
    Use OpenMP:                  NO
    Use GCD                      NO
    Use Concurrency              NO
    Use C=:                      NO
    Use Cuda:                    NO
    Use OpenCL:                  NO

  Python:
    Interpreter:                 /usr/bin/python2 (ver 2.6.6)

  Java:
    ant:                         NO
    JNI:                         NO
    Java tests:                  NO

  Documentation:
    Build Documentation:         NO
    Sphinx:                      NO
    PdfLaTeX compiler:           NO

  Tests and samples:
    Tests:                       YES
    Performance tests:           YES
    C/C++ Examples:              NO

  Install path:                  /usr/local/arm/opencv-arm

  cvconfig.h is in:              /home/xilinx-arm-opencv/PetaLinux/package/opencv/opencv-2.4.9/build
-----------------------------------------------------------------

Configuring done

 

再點擊Generate按鈕,即完成了makefile文件的配置。關閉cmake gui界面。

最後編譯並安裝opencv:

#編譯前須要將交叉編譯好的依賴庫鏈接或複製到交叉編譯工具中去
#不然會在opencv編譯時出現相似undefined reference to `png_set_strip_alpha@PNG15_0'的錯誤
cp -r /usr/local/arm/opencv-lib/lib/* /root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/arm-xilinx-linux-gnueabi/libc/usr/lib/

#編譯並安裝
make
make install

最終,咱們完成了OpenCV的交叉編譯,並獲得了兩個文件夾的文件,分別是:

#opencv依賴庫
/usr/local/arm/opencv-lib
#opencv庫
/usr/local/arm/opencv-arm

經過它們,就能夠編譯和運行咱們Zynq板的PetaLInux中運行opencv程序了。

 

下一次再記錄如何使用咱們編譯好的庫文件。

相關文章
相關標籤/搜索