编译这个库真他麻烦,明明工作重点不是编译OpenCV库,但是时间就偏偏耗在这里,从昨天晚上到现在终于编译成功,说说编译的过程。
我使用的环境linux mint 不记得几了。
编译器arm-linux-gnueabihf-gcc/g++ 4.6 (支持硬件浮点),
目标板是 cubieboard开发板 1GHz的CPU(支持硬件浮点),1GRAM,目前算是很强悍一款开发板了,而且价格公道358元,可以在上面运行opencv,杠杆的。买它回来就是在为了在上面做视觉处理的。
我的编译过程是参考csk前辈的编译过程http://www.csksoft.net/blog/post/mk802_dev_opencv.html,但是他的过程有些小问题,要真正试过才知道。
这里我放上我编译的opencv库,以及下面要用到的png zlib jpeg依赖库
http://vdisk.weibo.com/s/vMeNN/1364722558
Hi,推荐文件给你 "zlib_jpeg_png.tar.gz" http://vdisk.weibo.com/s/vMeQd/1364722532
1.下载硬件浮点编译器,
sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
2.编译依赖库
2.1 先设定几个环境变量方便后面的编译
export COMPILE_PREFIX=arm-linux-gnueabihf-
export INSTALL_PREFIX=`pwd`/crosscompile_root
export DL_FOLDER=`pwd`/dl
export BUILD_FOLDER=`pwd`/buildpool
export CC=${COMPILE_PREFIX}gcc
同时建立上面的几个目录用于存放编译过程的文件,把下载到的zlib/jpeg/png库放到dl目录中。
mkdir crosscompile_root dl buildpool
2.2 编译zlib,可以把下面的命令制作成一个脚本文件
pushd $BUILD_FOLDER
tar xf $DL_FOLDER/zlib-1.2.7.tar.gz
pushd zlib-1.2.7
./configure --prefix=$INSTALL_PREFIX
make
make install
popd
2.3 编译jpeg
tar xf $DL_FOLDER/jpegsrc.v8d.tar.gz
pushd jpeg-8d
./configure --host=arm-linux-gnueabihf --prefix=$INSTALL_PREFIX
mkdir -p $INSTALL_PREFIX/bin
mkdir -p $INSTALL_PREFIX/man/man1
make
make install
popd
2.4 编译png
tar xf $DL_FOLDER/libpng-1.5.4.tar.gz
pushd libpng-1.5.4
./configure --host=arm-linux-gnueabihf --prefix=$INSTALL_PREFIX CPPFLAGS=-I$INSTALL_PREFIX/include LDFLAGS=-L$INSTALL_PREFIX/lib
make
make install
popd
2.5 配置编译opencv
完成上面的几步应该已经在crosscompile_root/lib目录下有相应的库了
cd ~/OpenCV2.3.1/
mkdir build
这build目录里建立toolchain.cmake文件,输入以下内容
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR arm )
set( CMAKE_C_COMPILER arm-linux-gnueabihf-gcc )
set( CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++ )
set( CMAKE_LIBRARY_PATH ~/crosscompile_root/lib ${CMAKE_LIBRARY_PATH})
set( CMAKE_INCLUDE_PATH ~/crosscompile_root/include ${CMAKE_INCLUDE_PATH})
然后执行命令,csk前辈在这里有个小错误,我已经改了
cmake -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX -DZLIB_INCLUDE_DIR=$INSTALL_PREFIX/lib -DPNG_INCLUDE_DIR=$INSTALL_PREFIX/lib -DJPEG_INCLUDE_DIR=$INSTALL_PREFIX/lib -DOPENCV_EXTRA_C_FLAGS="-mfpu=neon -I$INSTALL_PREFIX/include -L$INSTALL_PREFIX/lib -Wl, -rpath,$INSTALL_PREFIX/lib" -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ..
以下是我的cmake结果,可以看到已经禁掉好多东西,因为是嵌入式开发可以吧一些不需要用到的模块去掉,
//
Extracting svn version, please wait...
-- SVNVERSION: svn:已导出
-- Detected version of GNU GCC: 46 (406)
-- Could NOT find TIFF (missing: TIFF_LIBRARY)
-- Could NOT find Jasper (missing: JASPER_LIBRARY JASPER_INCLUDE_DIR)
-- checking for module 'libv4l1'
-- package 'libv4l1' not found
-- Use NumPy headers from: /usr/lib/python2.7/dist-packages/numpy/core/include
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named sphinx
-- Parsing 'cvconfig.h.cmake'
--
-- General configuration for opencv 2.3.1 =====================================
--
-- Built as dynamic libs?: YES
-- Compiler: /usr/bin/arm-linux-gnueabihf-g++
-- C++ flags (Release): -Wall -pthread -ffunction-sections -mfpu=neon -I/home/msg/crosscompile_root/include -L/home/msg/crosscompile_root/lib -Wl, /home/msg/crosscompile_root/lib -O3 -DNDEBUG -fomit-frame-pointer -DNDEBUG
-- C++ flags (Debug): -Wall -pthread -ffunction-sections -mfpu=neon -I/home/msg/crosscompile_root/include -L/home/msg/crosscompile_root/lib -Wl, /home/msg/crosscompile_root/lib -g -O0 -DDEBUG -D_DEBUG -ggdb3
-- Linker flags (Release):
-- Linker flags (Debug):
--
-- GUI:
-- GTK+ 2.x: NO
-- GThread: NO
--
-- Media I/O:
-- ZLib: YES
-- JPEG: TRUE
-- PNG: TRUE
-- TIFF: build
-- JPEG 2000: build
-- OpenEXR: NO
-- OpenNI: NO
-- OpenNI PrimeSensor Modules: NO
-- XIMEA: NO
--
-- Video I/O:
-- DC1394 1.x: NO
-- DC1394 2.x: NO
-- FFMPEG: NO
-- codec: NO
-- format: NO
-- util: NO
-- swscale: NO
-- gentoo-style: YES
-- GStreamer: NO
-- UniCap: NO
-- PvAPI: NO
-- V4L/V4L2: /1
-- Xine: NO
--
-- Other third-party libraries:
-- Use IPP: NO
-- Use TBB: NO
-- Use ThreadingFramework: NO
-- Use Cuda: NO
-- Use Eigen: YES
--
-- Interfaces:
-- Python: NO
-- Python interpreter: /usr/bin/python -B (ver 2.7)
-- Python numpy: YES
-- Java: NO
--
-- Documentation:
-- Sphinx: NO
-- PdfLaTeX compiler: NO
-- Build Documentation: NO
--
-- Tests and samples:
-- Tests: YES
-- Examples: NO
--
-- Install path: /home/msg/crosscompile_root
--
-- cvconfig.h is in: /home/msg/OpenCV-2.3.1/build
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/msg/OpenCV-2.3.1/build
///
然后使用ccmake 或者cmake-gui微调,
ccmake .
加入了-mfpu=neon 据说是可以在cubieboard的CPU上进行优化,在ccmake中去掉python,gtk,cuba,1394lib等,因为在arm-linux-gcc编译器里面没有这些相应的库,我就是裁在这里.
3 .直接安装opencv
其实cubieboard上的linaro Ubuntu可以直接从apt-get 安装arm版的opencv,
sudo apt-get install libopencv-*
3.1 测试opencv
#include <opencv2/opencv.hpp>
#includee <stdio.h>
int main( int argc, char * argv[] ) {
// grab one frame from the camera specified via arg[1]
int camera_id = 0;
if (argc > 1) camera_id = atoi(argv[1]);
cv::VideoCapture cap(camera_id);
if(!cap.isOpened()) // check if we succeeded
{
printf("failed to open the camera with id %d.\n", camera_id);
return -1;
}
// capture one frame
cv::Mat frame;
cap >> frame; // get a new frame from camera
// save to file
imwrite("camera_captured.png", frame);
return 0;
}
编译以上程序
arm-linux-gnueabihf-g++ main.cpp -o main -L/home/msg/crosscompile_root/lib -I/home/msg/crosscompile_root/include -lpthread -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -rpath-link
3.2 把opencv库放到开发板上
复制到SD卡,由于我的是校园网用锐捷的设备,因此搞不到无线网络,也不能用cubieboard上网,很悲剧
sudo cp -r ~/crosscompile/lib/* /media/yoursdcard/usr/local/lib
sudo cp -r ~/main /media/yoursdcard/home/linaro
3.3 执行
./main 0 //0为摄像头编号
得到 png图像
camera_captured.png