編譯這個庫真他麻煩,明明工做重點不是編譯OpenCV庫,可是時間就恰恰耗在這裏,從昨天晚上到如今終於編譯成功,說說編譯的過程。 python
我使用的環境linux mint 不記得幾了。 linux
編譯器arm-linux-gnueabihf-gcc/g++ 4.6 (支持硬件浮點), 網絡
目標板是 cubieboard開發板 1GHz的CPU(支持硬件浮點),1GRAM,目前算是很強悍一款開發板了,並且價格公道358元,能夠在上面運行opencv,槓桿的。買它回來就是在爲了在上面作視覺處理的。 python2.7
個人編譯過程是參考csk前輩的編譯過程http://www.csksoft.net/blog/post/mk802_dev_opencv.html,可是他的過程有些小問題,要真正試過才知道。 ide
這裏我放上我編譯的opencv庫,以及下面要用到的png zlib jpeg依賴庫 svn
http://vdisk.weibo.com/s/vMeNN/1364722558 post
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 ui
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