交叉编译opencv2.3.1,并在DM8168 cortex A8中运行图像处理。
开发环境:
PC:ubuntu12.04LTS。Intel Core 2 Duo CPU E7200@2.53GHZ;
目标平台:SEED DVS DM8168 DVRRDK 02.00.XXXX
第一篇:OpenCV交叉编译
参考了http://blog.csdn.net/embeddedman/article/details/7416934
1.安装交叉编译链
使用DVRRDK中的ti_tools/cgt_a8/arm-2009q1/bin/arm-none-linux-xxx
需将其加入环境变量:可以加到用户目录下的.bashrc或/etc/profile
格式如下:
export PATH=/...../bin:$PATH
2.获得OpenCV源码,本文基于2.3.1
3.安装cmake cmake-gui
4.建立编译目录如/opencv231/build_arm
5.进入目录执行cmake-gui
选择源代码目录:/opt/OpenCV-2.31
选择Build目录:/opt/opencv2.3.1_forArm,大家根据自己的喜好设置编译目录吧。
点击Configure,保持generator为Unix Makefiles,选择Specify options for cross-compiling,点击Next
Operating System填写arm-inux
C Compilers选择DVRRDK_02.00.00.23/ti_tools/cgt_a8/arm-2009q1/bin/arm-none-linux-gnueabi-gcc
C++ Compilers填写.......-g++
程序库的Target Root填写OpenCV安装目录,然后点击Finish
上面标注的表示opencv将要安装的目录,默认为/usr/local,为了便于管理,大家可以安装在自己设定的目录下,可以修改,同时,网上资料说:另外,我没有安装tiff图像的支持,因此去掉WITH_TIFF(出自:http://blog.csdn.net/eagelangel/article/details/7232364)。在此点击Configure,然后点击Gennerate就会生成Makefile。
进入/opt/opencv2.3.1_forArm目录,执行make命令,就会编译opencv,编译过程中遇到如下错误:
In file included from /opt/OpenCV-2.3.1/modules/flann/src/precomp.hpp:9,
from /opt/opencv2.3.1_forArm/modules/flann/opencv_flann_pch_dephelp.cxx:1:
/opt/OpenCV-2.3.1/modules/flann/include/opencv2/flann/dist.h: In function 'T cvflann::abs(T) [with T = long double]':
/opt/OpenCV-2.3.1/modules/flann/include/opencv2/flann/dist.h:63: error: 'fabsl' was not declared in this scope
make[2]: *** [modules/flann/CMakeFiles/opencv_flann_pch_dephelp.dir/opencv_flann_pch_dephelp.obj] Error 1
make[1]: *** [modules/flann/CMakeFiles/opencv_flann_pch_dephelp.dir/all] Error 2
make: *** [all] Error 2
解决方法:修改OpenCV-2.3.1/modules/flann/include/opencv2/flann/dist.h文件第63行的源码:将absl()修改为abs()
如果编译过程遇到如下错误:
Linking CXX executable ../../bin/opencv_test_calib3d
../../lib/libopencv_core.so: undefined reference to `pthread_key_create'
../../lib/libopencv_core.so: undefined reference to `pthread_getspecific'
../../lib/libopencv_ts.so: undefined reference to `pthread_key_delete'
../../lib/libopencv_core.so: undefined reference to `pthread_once'
../../lib/libopencv_core.so: undefined reference to `clock_gettime'
../../lib/libopencv_core.so: undefined reference to `pthread_setspecific'
collect2: ld returned 1 exit status
make[2]: *** [bin/opencv_test_calib3d] Error 1
make[1]: *** [modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/all] Error 2
make: *** [all] Error 2
解决方案:修改/opt/opencv2.3.1目录下的CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthread -lrt,如果没有自己加上。
注意:每次错误产生,经修改后,只要再次执行make命令就接着编译,编译成功后,执行make intall命令就会安装opencv。
由于本人的安装目录为:/opt/opencv2.3.1_arm,大家可以看到生成了bin、lib、include等目录,如下:
第二篇
运行
1.将以上文件夹lib include是必须的拷贝的板子上。目录可以任意,也可以放到系统动态库目录。关于这两点可以参考:
http://blog.chinaunix.net/uid-23215128-id-2521319.html
一个示例代码:
#include "cv.h"
#include "highgui.h"
int main( int argc, char** argv )
{
IplImage* pImg; //声明IplImage指针
//载入图像,强制转化为Gray
if( argc == 3 &&
(pImg = cvLoadImage( argv[1], 0)) != 0 )
{
IplImage* pImg2 = cvCreateImage(cvGetSize(pImg),
pImg->depth,
pImg->nChannels);
cvCopy(pImg, pImg2, NULL);
cvSaveImage(argv[2], pImg2);//把图像写入文件
cvReleaseImage( &pImg ); //释放图像
cvReleaseImage( &pImg2 ); //释放图像
return 0;
}
return -1;
}
采用以下命令方式在pc上进行编译,当然编译时所连接的库是arm版的:
arm-none-linux-gnueabi-g++ test.cpp -o main1 -I/usr/local/arm/opencv/include/opencv -I/usr/local/arm/opencv/include -L/usr/local/arm/opencv/lib -lpthread -ldl -lrt -lopencv_core -lopencv_highgui
(目录可能有所不同)
如果PC中 arm的库的路径与lib在板子上的路径不一致的话,执行程序会提示找不到库,这时可以通过板卡设置export LD_LIBRARY_PATH= 使程序可以搜索到库。
另外最好将板子上opencvlib的目录见一个软连接到/usr/lib.
2.执行
./
结论:
最终运行结果:cvSplit cvMin cvCreateImage等等的操作,大概是台式机PC的1/40的速度。
计划:与DSP联合运算。