OpenCV是目前开源项目中最著名的基于机器视觉方向的图像处理的开发包,目前已经有被移植到嵌入式Linux环境上。本文介绍了OpenCV交叉编译的基本步骤。
一、编译环境及库文件
linux环境:fedora14
linux环境:fedora14
交叉编译:4.5.1
arm板子:tiny6410
libz: zlib-1.2.7
libjpeg: jpegsrc.v7
libpng: libpng-1.5.10
libyasm: yasm-1.2.0
opencv: opencv-2.4.2
libx264: x264-snapshot-20120608-2245
libxvid: xvidcore-1.3.2
lffmpeg: ffmpeg-0.10.3
arm板子:tiny6410
libz: zlib-1.2.7
libjpeg: jpegsrc.v7
libpng: libpng-1.5.10
libyasm: yasm-1.2.0
opencv: opencv-2.4.2
libx264: x264-snapshot-20120608-2245
libxvid: xvidcore-1.3.2
lffmpeg: ffmpeg-0.10.3
cmake: cmake-2.8.8-Linux-i386.tar,gz
二、opencv-2.4.1的移植过程
1、libz的交叉编译:
# ./configure --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --shared
二、opencv-2.4.1的移植过程
1、libz的交叉编译:
# ./configure --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --shared
修改makefile
CC=arm-linux-gcc
AR=arm-linux-ar rc
RANLIB=arm-linux-ranlib
CDSHARED=arm-linux-gcc -shared -wl, -soname,libz.so.1,--version-script,zl
lib.map
#make
#make install
2、libjpeg的交叉编译:
#./configure --host=arm-linux --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --enable-shared --enable-static
#make install
3、libpng的交叉编译
#./configure --host=arm-linux --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --enable-shared --enable-static
#make
#make install
4、yasm的交叉编译:
#./configure --host=arm-linux --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --enable-shared --enable-static
#make
#make install
5、libx264的交叉编译:
#CC=arm-linux-gcc ./configure --enable-shared --host=arm-linux --disable-asm --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi
#make
#make install
6、libxvid的交叉编译:
#cd build/generic
#./configure --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --host=arm-linux --disable-assembly
#make
#make install
7、ffmpeg的交叉编译:
#./configure --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --enable-shared --disable-static --enable-gpl --enable-cross-compile --arch=arm --disable-stripping --target-os=linux --enable-libx264 --enable-libxvid --cc=arm-linux-gcc --enable-swscale
#make
#make install
#make
#make install
2、libjpeg的交叉编译:
#./configure --host=arm-linux --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --enable-shared --enable-static
#make install
3、libpng的交叉编译
#./configure --host=arm-linux --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --enable-shared --enable-static
#make
#make install
4、yasm的交叉编译:
#./configure --host=arm-linux --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --enable-shared --enable-static
#make
#make install
5、libx264的交叉编译:
#CC=arm-linux-gcc ./configure --enable-shared --host=arm-linux --disable-asm --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi
#make
#make install
6、libxvid的交叉编译:
#cd build/generic
#./configure --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --host=arm-linux --disable-assembly
#make
#make install
7、ffmpeg的交叉编译:
#./configure --prefix=/opt/FriendlyARM/toolschain/4.5.1/arm-none-linux-gnueabi --enable-shared --disable-static --enable-gpl --enable-cross-compile --arch=arm --disable-stripping --target-os=linux --enable-libx264 --enable-libxvid --cc=arm-linux-gcc --enable-swscale
#make
#make install
8.cmake-gui安装
到官网上下载已经编译好的cmake-2.8.8-Linux-i386.tar,gz,解压就可以了
9、opencv2.4.1的交叉编译:
用到的主要目录说明:
交叉编译工具链所在目录 /opt/FriendlyARM/toolschain/4.5.1/
用到的主要目录说明:
交叉编译工具链所在目录 /opt/FriendlyARM/toolschain/4.5.1/
安装opencv的目录 /usr/local/
opencv源码所在目录 /home/6410/opencv/opencv-2.4.2/
编译好的opencv库所在目录 /home/6410/opencv/opencv-2.4.2/build
#mkdir build
#cd build
#cmake-gui
选择源代码目录/home/6410/opencv/opencv-2.4.2/
#mkdir build
#cd build
#cmake-gui
选择源代码目录/home/6410/opencv/opencv-2.4.2/
选择Build目录:/home/6410/opencv/opencv-2.4.2/build
点击Configure,保持generator为Unix Makefiles,选择Specify options for cross-compiling,点击Next,
Operating System填写arm-linux
C Compilers填写opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-gcc
C++ Compilers填写opt/FriendlyARM/toolschain/4.5.1//bin/arm-linux-g++
程序库的Target Root填写opt/FriendlyARM/toolschain/4.5.1/
点击Configure,保持generator为Unix Makefiles,选择Specify options for cross-compiling,点击Next,
Operating System填写arm-linux
C Compilers填写opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-gcc
C++ Compilers填写opt/FriendlyARM/toolschain/4.5.1//bin/arm-linux-g++
程序库的Target Root填写opt/FriendlyARM/toolschain/4.5.1/
然后点击Finish。
默认安装目录为/usr/local,点击Generate生成Makefile。
#make
错误一:
Linking CXX executable http://www.cnblogs.com/bin/opencv_createsamples
http://www.cnblogs.com/lib/libopencv_core.so: undefined reference to `clock_gettime'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFerrorHandler'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFrealloc'
http://www.cnblogs.com/lib/libopencv_core.so: undefined reference to `pthread_key_create
解决方法:
修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthread -lrt,重新编译,错误消除
错误二:
Linking CXX executable http://www.cnblogs.com/bin/opencv_createsamples
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFerrorHandler'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFrealloc'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFmalloc'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFmemcpy'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `TIFFOpen'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFfree'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFwarningHandler'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFmemcmp'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFmemset'
解决方法:修改CMakeCache.txt,WITH_TIFF:BOOL=ON,改成OFF,重新编译,错误消除。
#make install
三、测试
测试程序:
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int main(int argc,char **argv)
{
CvCapture* capture = NULL;
IplImage* frame = NULL;
if(!(capture = cvCaptureFromCAM(-1)))
{
fprintf(stderr, "Can not open camera./n");
return -1;
}
cvNamedWindow("video", 1);
while(frame = cvQueryFrame( capture ) )
{
cvShowImage("video", frame);
}
cvDestroyWindow("video");
cvReleaseCapture(&capture);
return 0;
}
编译:
#arm-linux-g++ -o opencv_test opencv_test.cpp -I /usr/local/include -L /usr/local/lib -lopencv_core -lopencv_highgui -lpthread -lrt
默认安装目录为/usr/local,点击Generate生成Makefile。
#make
错误一:
Linking CXX executable http://www.cnblogs.com/bin/opencv_createsamples
http://www.cnblogs.com/lib/libopencv_core.so: undefined reference to `clock_gettime'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFerrorHandler'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFrealloc'
http://www.cnblogs.com/lib/libopencv_core.so: undefined reference to `pthread_key_create
解决方法:
修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthread -lrt,重新编译,错误消除
错误二:
Linking CXX executable http://www.cnblogs.com/bin/opencv_createsamples
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFerrorHandler'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFrealloc'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFmalloc'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFmemcpy'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `TIFFOpen'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFfree'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFwarningHandler'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFmemcmp'
http://www.cnblogs.com/lib/libopencv_highgui.so: undefined reference to `_TIFFmemset'
解决方法:修改CMakeCache.txt,WITH_TIFF:BOOL=ON,改成OFF,重新编译,错误消除。
#make install
三、测试
测试程序:
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int main(int argc,char **argv)
{
CvCapture* capture = NULL;
IplImage* frame = NULL;
if(!(capture = cvCaptureFromCAM(-1)))
{
fprintf(stderr, "Can not open camera./n");
return -1;
}
cvNamedWindow("video", 1);
while(frame = cvQueryFrame( capture ) )
{
cvShowImage("video", frame);
}
cvDestroyWindow("video");
cvReleaseCapture(&capture);
return 0;
}
编译:
#arm-linux-g++ -o opencv_test opencv_test.cpp -I /usr/local/include -L /usr/local/lib -lopencv_core -lopencv_highgui -lpthread -lrt
单个opencv的make就花了大概1个半小时,终于成功了,但是这只是开始,要想运用opecv+qt+x264+ffmpeg做东西,这还需要更长远的学习啊