安装环境
宿主机:
Red Hat Enterprise Linux Server 6.3
开发板:
mini2440
相关软件:
cmake-3.5.1.tar.gz、OpenCV-2.3.1a.tar.bz2
下载地址:
CMake:
https://cmake.org/files/v3.5/cmake-3.5.1.tar.gz
OpenCV:
http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.3.1/OpenCV-2.3.1a.tar.bz2
安装过程
CMake安装
说明:
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。用它来产生OpenCV编译时用到的Makefile文件。
步骤:
1.解压
tar xvf cmake-3.5.1.tar.gz
cd cmake-3.5.1
2.配置
./configure --prefix=/usr //生成Makefile,安装位置/usr
3.编译
make //通过Makefile编译源代码
4.安装
make install //文件安装在/usr/路径下
5.测试
cmake //执行cmake命令显示cmake版本号和帮助信息表示安装成功
OpenCV安装
说明:
OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库。编译和运行调用OpenCV库函数的程序时需要用到OpenCV库和头文件。
1.解压
tar xvf OpenCV-2.3.1a.tar.bz2
cd OpenCV-2.3.1
2.配置
2.1.编写配置文件
vim toolchain.cmake
# this one is important SET(CMAKE_SYSTEM_NAME Linux) # specify the cross compiler SET(CMAKE_C_COMPILER /usr/local/arm/4.3.2/bin/arm-linux-gcc) SET(CMAKE_CXX_COMPILER /usr/local/arm/4.3.2/bin/arm-linux-g++) # where is the target environment SET(CMAKE_FIND_ROOT_PATH /usr/local/arm/4.3.2) # search for programs in the build host directories SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) # for libraries and headers in the target directories SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
2.2.生成编译文件
mkdir _install //创建安装目录
cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake -DCMAKE_INSTALL_PREFIX=${PWD}/_install/ //生成Makefile
2.3.修改编译文件
ccmake .
WITH_FFMPEG OFF //关闭视频解码,如果需要先编译相关库 WITH_GSTREAMER OFF //关闭视频解码,如果需要先编译相关库 WITH_GTK OFF //关闭图形界面,如果需要先编译相关库 WITH_TIFF OFF //关闭图片格式,如果需要先编译相关库
按下c生效配置,按下e退出配置,按下q退出配置。
3.编译
make //通过Makefile编译源代码
如果遇到错误:
/space/work/thesis/000/OpenCV-2.3.1/modules/flann/include/opencv2/flann/dist.h:63:error: ‘fabsl‘ was not declared in this scope。
修改fabsl为fabs,然后继续make。
如果遇到错误:
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
修改/opt/opencv2.3.1目录下的CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthread –lrt,然后继续make。
4.安装
make install //文件安装在${PWD}/_install/路径下
5.测试
5.1.编写Makefile
vim Makefile
prefix = .../OpenCV-2.3.1/_install libdir = $(prefix)/lib includedir_old = $(prefix)/include/opencv includedir_new = $(prefix)/include Libs = -L$(libdir) -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann Cflag = -I$(includedir_old) -I$(includedir_new) % : %.c arm-linux-g++ $(Libs) $(Cflag) $^ -o $@ % : %.cpp arm-linux-g++ $(Libs) $(Cflag) $^ -o $@
5.2.编写测试程序
vim test.cpp
#include <stdio.h> #include <cv.h> #include <highgui.h> using namespace cv; int main(int argc, char **argv){ //参数检测 if(argc != 2){ printf("Usage:\n\%s <image.jpg>", argv[1]); } //打开图片 Mat src; src = imread(argv[1], 0); //灰度转换 //保存图片 imwrite("dst.jpg", src); return 0; }
5.3.复制程序和链接库到开发板
cp .../OpenCV-2.3.1/_install/lib/* .../rootfs/lib/
cp test .../rootfs/sbin
5.4.运行程序
./test image.jpg //如果成功保存灰度图片说明移植成功
如果缺少相关库通过下面命令查找并复制到开发板
arm-linux-gcc -print-file-name=libxxx.so
cp .../lib/libxxx.* .../rootfs/lib
参考文档
http://blog.csdn.net/embeddedman/article/details/7416934
http://blog.csdn.net/eagelangel/article/details/7232364
http://www.cnblogs.com/emouse/archive/2013/04/01/2993842.html
http://blog.sina.com.cn/s/blog_5ca0198e0101iphz.html
http://blog.csdn.net/wc7620awjh/article/details/8268447
http://www.tuicool.com/articles/yAFnQ3
http://blog.sina.com.cn/s/blog_627bb2cf0101ge3r.html
http://www.vtk.org/Wiki/CMake_Cross_Compiling#Setting_up_the_system_and_toolchain
相关移植
libtiff交叉编译
./configure --host=arm-linux CC=arm-linux-gcc CXX=arm-linux-g++ --prefix=${PWD}/_install/
make
make install
file libtiff.so.5.2.4 //查看版本是否为ARM
cp _install/lib/* .../rm-none-linux-gnueabi/libc/armv4t/usr/lib -rf //复制到编译器链接库目录
cp _install/inclue/* .../rm-none-linux-gnueabi/libc/armv4t/usr/inclue -rf //复制到编译器头文件目录
arm-linux-gcc -print-file-name=libtiff.so //显示库全路径表示已经生效
cp _install/lib/* .../rootfs/lib -rf /复制到开发板链接库目录
ffmpeg交叉编译
./configure --enable-cross-compile --arch=arm --target-os=linux --cross-prefix=arm-linux- --enable-shared --prefix=${PWD}/_install
make
make install
file libavcodec.so.53.8.0 //查看版本是否为ARM
cp _install/lib/* .../rm-none-linux-gnueabi/libc/armv4t/usr/lib -rf //复制到编译器链接库目录
cp _install/inclue/* .../rm-none-linux-gnueabi/libc/armv4t/usr/inclue -rf //复制到编译器头文件目录
arm-linux-gcc -print-file-name=libavcodec.so //显示库全路径表示已经生效
cp _install/lib/* .../rootfs/lib -rf /复制到开发板链接库目录