编译OpenCV:
1、解压OpenCV 2.1到/usr/local/OpenCV-2.1.0目录下
2、创建/usr/local/opencv-arm/目录,作为CMake编译arm版本的工作目录
3、在X环境下,运行cmake-gui
选择源代码目录:/usr/local/OpenCV-2.1.0
选择Build目录:/usr/local/opencv-arm/
点击Configure,保持generator为Unix Makefiles,选择Specify options for cross-compiling,点击Next
Operating System填写arm-inux
C Compilers填写/usr/local/arm/4.3.2/bin/arm-linux-gcc
C++ Compilers填写/usr/local/arm/4.3.2/bin/arm-linux-g++
程序库的Target Root填写/usr/local/arm/4.3.2/,然后点击Finish
原因是cmake不认识我定义的arm-linux系统标记,没有加上库pthread和rt的链接选项
5、修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthread -lrt,重新编译,错误消除
修改默认配置,默认安装目录为/usr/local,对于交叉编译的库来说并不合适,所以我把CMAKE_INSTALL_PREFIX变量改为/usr/local/arm/lib/opencv/
另外,我没有安装tiff图像的支持,因此去掉WITH_TIFF
点击Generate生成Makefile
4、在终端界面中,进入目录/usr/local/opencv-arm,运行make编译opencv
编译时发现如下错误:
Linking CXX executable ../../bin/opencv_createsamples
../../lib/libcxcore.so: undefined reference to `clock_gettime'
../../lib/libcxcore.so: undefined reference to `pthread_key_create'
../../lib/libcxcore.so: undefined reference to `pthread_getspecific'
../../lib/libcxcore.so: undefined reference to `pthread_setspecific'
6、运行make install,将opencv生成的库和头文件安装到目录/usr/local/arm/lib/opencv/
测试OpenCV库
1、首先确认一下库是否已编译正确及其安装位置
查看头文件:
- [root@localhost opencv-arm]# ls /usr/local/arm/lib/opencv/include/opencv/
- cvaux.h cvcompat.h cv.hpp cvtypes.h cvvidsurv.hpp cxcore.h cxerror.h cxmat.hpp cxoperations.hpp highgui.h ml.h
- cvaux.hpp cv.h cvinternal.h cvver.h cvwimage.h cxcore.hpp cxflann.h cxmisc.h cxtypes.h highgui.hpp
查看库文件:
- [root@localhost opencv-arm]# ls /usr/local/arm/lib/opencv/lib
- libcv.a libcvaux.a libcvaux.so libcv.so libcxcore.a libcxcore.so libhighgui.a libhighgui.so libml.a libml.so
2、写个简单的测试程序,打开摄像头并创建一个窗口显示
- // test.cpp
- #include <cv.h>
- #include <cxcore.h>
- #include <highgui.h>
- int main()
- {
- 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;
- }
3、编译链接测试程序
arm-linux-g++ -I/usr/local/arm/lib/opencv/include/opencv/ -L/usr/local/arm/lib/opencv/lib/ -lcv -lcxcore -lhighgui -lpthread -lrt -o test test.cpp