OpenCV是Intel支持的开源计算机视觉库。它由一系列C函数和少量C++类构成,实现了图像处理和计算机视觉方面的很多通用算法。它不依赖于其它的外部库—尽管也可以使用某些外部库。
OpenCV使用BSD License,对非商业应用和商业应用都可以免费使用。
OpenCV的主要应用环境是Windows和Linux,对于嵌入式系统如arm-linux,很少有资料提到,因此将我在arm-linux编译过程记录下来和大家分享。
预备工作:
a. 安装交叉编译链
我使用的是arm-linux-gcc 4.3.2,解压到目录/usr/local/arm/4.3.2,然后在环境变量PATH中增加/usr/local/arm/4.3.2/bin
b. 安装CMake
OpenCV 2.0之后的版本,必须使用CMake创建Makefile。我使用的CMake版本是2.8.2,解压到目录/usr/local/cmake-2.8.2-Linux-i386,然后在环境变量PATH中增加/usr/local/cmake-2.8.2-Linux-i386/bin
编译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
修改默认配置,默认安装目录为/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 http://www.cnblogs.com/bin/opencv_createsamples
http://www.cnblogs.com/lib/libcxcore.so: undefined reference to `clock_gettime'
http://www.cnblogs.com/lib/libcxcore.so: undefined reference to `pthread_key_create'
http://www.cnblogs.com/lib/libcxcore.so: undefined reference to `pthread_getspecific'
http://www.cnblogs.com/lib/libcxcore.so: undefined reference to `pthread_setspecific'
原因是cmake不认识我定义的arm-linux系统标记,没有加上库pthread和rt的链接选项
5、修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthread -lrt,重新编译,错误消除
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/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
[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;
}
// 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
4、复制程序到嵌入式系统中运行
首先复制主机/usr/local/arm/lib/opencv/lib/下面的几个.so文件到嵌入式Linux系统的/lib/目录下,再复制我们编译的test到嵌入式系统/opt/myworks/目录下(并确保文件test属性为可执行),如果test可正常运行没有报告缺少库文件,说明我们编译的arm-linux版OpenCV库已经可以正常使用。