目的:
OpenCV是Intel支持的开源计算机视觉库。它由一系列C函数和少量C++类构成,实现了图像处理和计算机视觉方面的很多通用算法。它不依赖于其它的外部库—尽管也可以使用某些外部库。OpenCV使用BSD License,对非商业应用和商业应用都可以免费使用。OpenCV的主要应用环境是Windows和Linux,本文主要介绍于嵌入式系统下的安装。
开发环境:
Linux版本:fedora24
Opencv版本:opencv-2.1.0 ( 下载地址:http://opencv.org/ )
移植opencv2.4.2编译arm程序时
arm-linux-g++ -o opencv_test opencv_test.cpp -I /usr/local/arm/opencv/include/opencv -L /usr/local/arm/opencv/lib -lopencv_core -lopencv_highgui
出现如下错误:
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: warning: ../../lib/libcxcore.so, needed by /usr/local/arm/opencv-2.1/lib/libcv.so, not found (try using -rpath or -rpath-link)
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: warning: ../../lib/libcv.so, needed by /usr/local/arm/opencv-2.1/lib/libcvaux.so, not found (try using -rpath or -rpath-link)
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: warning: ../../lib/libhighgui.so, needed by /usr/local/arm/opencv-2.1/lib/libcvaux.so, not found (try using -rpath or -rpath-link)
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: warning: ../../lib/libml.so, needed by /usr/local/arm/opencv-2.1/lib/libcvaux.so, not found (try using -rpath or -rpath-link)
放弃Opencv2.4.2的道路,转向Opencv2.0。
在交叉编译中LFLAGS即-L换成-Wl,-rpath-link -Wl,发现这个问题是不见了,但是尽然出现了找不到.so的库文件,我也无语了,这在之前已经解决的问题又回来了,拷贝了库也设置了路径,老问题又出现了。arm-none-linux-gnueabi缺少这样的库,拷贝过去就行了,发现不行,没办法只能放弃Opencv2.4.2了。
交叉编译工具:arm-linux-gcc-4.3.2
安装与配置:
1.安装CMake:
下载地址:https://cmake.org/download/
./bootstrap
make
make install
2.编译OpenCV:
A.解压文件
B.创建/usr/local/opencv-arm/目录,作为CMake编译arm版本的工作目录
C.在X环境下,运行cmake-gui:
a.选择源代码目录:/usr/local/OpenCV-2.1.0
b.选择Build目录:/usr/local/opencv-arm/
c.点击Configure,保持generator为Unix Makefiles,选择Specify options for cross-compiling,点击Next
d.Operating System填写arm-inux
e.C Compilers填写/usr/local/arm/4.3.2/bin/arm-linux-gcc
f.C++ Compilers填写/usr/local/arm/4.3.2/bin/arm-linux-g++
g.程序库的Target Root填写/usr/local/arm/4.3.2/
h.点击Finish
i.修改默认配置,默认安装目录为/usr/local,对于交叉编译的库来说并不合适,所以我把CMAKE_INSTALL_PREFIX变量改为/usr/local/arm/lib/opencv/
j.点击Generate生成Makefile
D.在终端界面中,进入目录/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 topthread_key_create’
../../lib/libcxcore.so: undefined reference to pthread_getspecific'
../../lib/libcxcore.so: undefined reference topthread_setspecific’
../../lib/libopencv_ocl.so:undefined reference to ‘dlopen’
原因是cmake不认识我定义的arm-linux系统标记,没有加上库pthread和rt的链接选项
E.修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthread -lrt -ldl,重新编译,错误消除
F.运行make install,将opencv生成的库和头文件安装到目录/usr/local/arm/lib/opencv/
测试OpenCV库:
1、首先确认一下库是否已编译正确及其安装位置
查看头文件:
[c-sharp] view plain copy
[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
查看库文件:
[c-sharp] view plain copy
[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
4、复制程序到嵌入式系统中运行
首先复制主机/usr/local/arm/lib/opencv/lib/下面的几个.so文件到嵌入式Linux系统的/lib/目录下,再复制我们编译的test到嵌入式系统/opt/myworks/目录下(并确保文件test属性为可执行),如果test可正常运行没有报告缺少库文件,说明我们编译的arm-linux版OpenCV库已经可以正常使用。