Linux下安装opencv并移植至ARM

2019-07-12 15:24发布

目的:

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) 读了这边作者的博文:http://blog.csdn.net/yanzi1225627/article/details/7524698,发现他遇到的和我类似的问题,且尝试了多种方式也没能解决,最终我也和他一样走向了放弃Opencv2.4.2的道路,转向Opencv2.0。其中也尝试了他提供的解决方法,并参考博文:http://blog.sina.com.cn/s/blog_602f87700100nyda.html 在交叉编译中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 to
pthread_key_create’
../../lib/libcxcore.so: undefined reference to pthread_getspecific'
../../lib/libcxcore.so: undefined reference to
pthread_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库:

参考另一篇博客:http://blog.csdn.net/noodies/article/details/5798434