首页 > 代码库 > 【Xilinx-Petalinux学习】-04-OpenCV的移植
【Xilinx-Petalinux学习】-04-OpenCV的移植
交叉编译PC平台 VMware12, CentOS 6.5 32 bit
在VMware中安装CentOS,用户名:xilinx-arm-opencv 密码:root
至于这里为什么用CentOS,而不是Ubuntu,是因为CentOS的内核可以支持V4L,而Ubuntu因为内核版本较高,已经没有了V4L的支持,只有V4L2。
总之为了方便还是再安装一个CentOS的虚拟机吧。
step1: 安装包、工具下载
首先下载各种依赖库源文件和工具。提到的都是我用到的版本,已经验证过,都是可以互相配合可用的。
OpenCV依赖库:
zlib-1.2.7.tar.gz
jpegsrc.v9b.tar.gz
libpng-1.5.14.tar.gz
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 updateyum install glibc-devel.i686yum install gtk2-devel.i686yum install libcanberra.i686yum install libcanberra-gtk2.i686yum install PackageKit-gtk-module.i686yum install GConf2.i686yum install ncurses-libs.i686yum 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=armexport CROSS_COMPILE=arm-xilinx-linux-gnueabi-export PATH=/root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/:$PATH#保存并退出
#sourcesource ~/.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-libmkdir -p /usr/local/arm/opencv-arm#添加环境变量export ZYNQ_CV_INSTALL=/usr/local/arm/opencv-libexport CC=arm-xilinx-linux-gnueabi-gcc
解压所有的依赖库源代码,然后分别进入各个源码的文件夹进行交叉编译。一定要按照顺序安装,因为它们之间也有依赖关系。
zlib安装
./configure --prefix=$ZYNQ_CV_INSTALL --sharedmakemake install
jpeg安装
./configure --prefix=$ZYNQ_CV_INSTALL --host=arm-xilinx-linux-gnueabi --enable-sharedmakemake 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/includemakemake install
x264安装
./configure --host=arm-linux --cross-prefix=arm-xilinx-linux-gnueabi- --enable-shared --disable-asm --prefix=$ZYNQ_CV_INSTALLmakemake install
xvidcore安装
cd build/generic/./configure --prefix=$ZYNQ_CV_INSTALL --host=arm-xilinx-linux-gnueabi --disable-assemblymakemake 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/includemakemake 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-asmmakemake install
为了在编译OpenCV时确保ffmpeg的正常链接,需要添加PKG_CONFIG_PATH环境变量
gedit /etc/bashrc#在文件最后添加如下内容PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$ZYNQ_CV_INSTALL/lib/pkgconfigexport PKG_CONFIG_PATH#保存并退出#sourcesource /etc/bashrc
这些我都试了很多遍了,就按照上面的方法配置、编译和安装即可,就不去详细的说明各个配置参数的意义了。
step4: 交叉编译OpenCV 2.4.9
安装cmake:
yum install cmakeyum install cmake-gui
解压OpenCV 2.4.9源码,进行配置:
#进入文件夹cd opencv-2.4.9#建立并进入编译文件夹mkdir buildcd 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#在CentOS中,使OpenCV支持V4Lcp /usr/include/linux/videodev.h /root/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/arm-xilinx-linux-gnueabi/libc/usr/include/linux/#cmakecmake -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/#编译并安装makemake install
最终,我们完成了OpenCV的交叉编译,并得到了两个文件夹的文件,分别是:
#opencv依赖库/usr/local/arm/opencv-lib#opencv库/usr/local/arm/opencv-arm
通过它们,就可以编译和运行我们Zynq板的PetaLInux中运行opencv程序了。
下一次再记录如何使用我们编译好的库文件。
【Xilinx-Petalinux学习】-04-OpenCV的移植