整體思路:利用TI提供的交叉編譯器(arm-none-liunux-gnueabi-gcc)交叉編譯opencv庫;利用eclipse 編譯算法工程,在工程中調用交叉編譯後的opencv庫;編譯好的算法放到文件系統下,在板子上運行,此過程可以利用GDB server實現代碼的遠程調試。
1.交叉編譯opencv庫。
這裏參考的文章是http://processors.wiki.ti.com/index.php/Building_OpenCV_for_ARM_Cortex-A8及http://blog.csdn.net/yr119111/article/details/7732336。唯恐以後資源找不到,還是貼到這裏吧。
Introduction:
This wiki provides instructions that specify how to building OpenCV for ARM Cortex-A8 platform using Cmake in a linux environment. The instructions below have been tested for OpenCV 2.2 by cross compiling it on a Ubuntu 10.04 machine. The toolchain is Codesourcery 2010.12.
Build Instructions
OpenCV 2.2 uses cmake to build. Cmake is a meta-makefile system that generates makefiles. We need to install this first.
- Update Ubuntu to install and setup cmake
$ sudo apt-get install cmake cmake-curses-gui
- Download and install Codesourcery 2010.12 from http://www.codesourcery.com/sgpp/lite/arm/portal/release1600. Assuming you have installed it in /opt/arm-2010.12, add /opt/arm-2010.12/bin to your path
$ export PATH=$PATH:/opt/arm-2010.12/bin
- Now download OpenCV 2.2 from http://opencv.willowgarage.com/wiki/. Untar it into your home directory.
$ tar -jxf OpenCV-2.2.0.tar.bz2
- Cmake supports building outside the source tree. We will use this method to build OpenCV. So, create a directory in $HOME as build
$ mkdir ~/build && cd ~/build
- We need to now create a toolchain.cmake file that we will pass to cmake so that it knows how to cross-compile.
$ vi toolchain.cmake
- Add the following to toolchain.cmake and save it. This assumes that your NFS targetfs is at ~/targetfs
set( CMAKE_SYSTEM_NAME Linux ) set( CMAKE_SYSTEM_PROCESSOR arm ) set( CMAKE_C_COMPILER arm-none-linux-gnueabi-gcc ) set( CMAKE_CXX_COMPILER arm-none-linux-gnueabi-g++ ) set( CMAKE_FIND_ROOT_PATH ~/targetfs )
Important: The CMake build setup in OpenCV 2.2 and earlier does not have NEON ENABLE flag to to take advantage of neon accelearation. Hence please make the following modifications to the file CMakeLists.txt in OpenCV 2.2 to enable neon acceleration.
# Other optimizations if(USE_O2) set(EXTRA_C_FLAGS_RELEASE "${EXTRA_C_FLAGS_RELEASE} -O2 -mfpu=neon") endif()
This may not be required in later versions of OpenCV since OpenCV 2.3 has the option to enable neon in the CMake build process.
- Now we run cmake to process and generate makefile
$ cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../OpenCV-2.2.0/
- When cross compiling for ARM, I found that some items do not build. So, we need to use the cmake curses gui to customize cmake output and re-generate the makefile before we can build. Now launch the cmake curses gui to customize the make file generation
$ ccmake .
- This will bring up a screen full of options that can be edited. Make sure that the following items are OFF. If it is ON, you can toggle it by scrolling to the option and hitting Enter
- BUILD_NEW_PYTHON_SUPPORT
- BUILD_TESTS
- WITH_1394
- WITH_CUDA
- WITH_EIGEN2
- WITH_FFMPEG
- WITH_GSTREAMER
- WITH_GTK
- WITH_JASPER
- WITH_JPEG
- WITH_OPENEXR
- WITH_PNG
- WITH_PVAPI
- WITH_QT
- WITH_QT_OPENGL
- WITH_TBB
- WITH_TIFF
- WITH_UNICAP
- WITH_V4L
- WITH_XINE
Note: Do not forget to keep the USE_O2 option ON in the configuration to take adavntage of neon acceleration that you added to this option
- Now, press 'c' to configure and 'g' to generate the makefiles again.
- Now you are ready to build OpenCV. Run make from the command line and it should generate the OpenCV libraries. You can try to customize the build by turning on the various options that we disabled earlier and see if it builds for you.
opencv2.3.1在arm端的移植( 更新測試結果)
所以我估計在omapl138上也會成功。
更新:更新了 編譯命令
`pkg-config --cflags --libs opencv` 的更改以及使用方法
首先,opencv是個好東西,對於我這樣的菜鳥,來說,既是好用的工具,也是最好的學習資料。
此次移植是在ubuntu12.04 32位,針對omapl138的arm端來進行的。(其實只要是arm應該都一樣)
本來呢,是想移植到c6748的。但是我感覺這個對我來說難度有點大。。而網上對於移植arm的資料也很多。
所以先移植到arm再說把。
不過還是希望有高手能給我指導一下,將opencv移植至c6748該怎麼做。在CCS裏或者linux裏都行。因爲我想使用gpp+dsp模式。將算法做成包,然後跑在dsp端。
此文有一部分綜合了網上多人的方法,也有自己在進行移植時的經驗。
當然,移植的效果還沒有給出,因爲板子不在身邊,無法進行測試。等有板子了,再進行測試,確定是否移植成功。
結果會在本文更新。
好。先下載opencv。
下載OpenCV 2.3.1 , http://sourceforge.net/projects/opencvlibrary/files/
然後解壓
tar xvf OpenCV-2.3.1.tar.bz2
在此要說明的是,此次移植是在前面omapl138的DVSDK都安裝並且配置好的情況下進行的。
我的DVSDK安裝路徑是/home/yr/ti-dvsdk_omapl138-evm_04_03_00_06
我使用omapl138中的交叉編譯鏈。
即交叉編譯鏈存在於
/home/yr/ti-dvsdk_omapl138-evm_04_03_00_06/linux-devkit/bin 這個目錄中。
剛纔解壓了opencv,現在在你的工作目錄裏建立一個新的文件夾opencv_arm。名稱和具體路徑看個人愛好。
然後進入終端運行cmake-gui。如果以前沒有安裝過,終端會提示你使用apt-get命令安裝。
安裝好之後就可以運行了。
cmake-gui
之後出現下面的界面
選擇source code目錄 /home/yr/OpenCV-2.3.1
以及build目錄 /home/yr/opencv_arm
點configure
然後按照下圖中所選擇的:
generator爲Unix Makefiles,選擇Specify options for cross-compiling
next之後,在下圖這幾個位置填寫路徑
Operating System填寫arm-arago-linux
Compilers裏,C選擇 /home/yr/ti-dvsdk_omapl138-evm_04_03_00_06/linux-devkit/bin/arm-arago-linux-gnueabi-gcc
C++選擇 /home/yr/ti-dvsdk_omapl138-evm_04_03_00_06/linux-devkit/bin/arm-arago-linux-gnueabi-g++
可以看出來我都是用的DVSDK中的目錄。
Target Root選擇 /home/yr/ti-dvsdk_omapl138-evm_04_03_00_06/linux-devkit
然後finish。
cmake便返回下圖
圖中紅色部分是我們可以配置的位置。這裏要提醒一下的是,更改完配置之後,記得再點一下左下角的configure按鈕。不然更改不會應用。
具體的配置當然是根據需要來進行的。
這裏要說明一下:
因爲在之前安裝過opencv 是x86版本的,即在PC上使用的,所以/usr/local中的include以及lib都有opencv的相關庫存在,
所以爲了方便自己使用以及不出問題,建議將arm版的opencv的安裝目錄更改一下,
我將下圖位置的CMAKE_INSTALL_PREFIX後面的/usr/local改成了
/usr/local/arm
同時,我還更改了一些配置,卻掉了一些我不用的功能,以及導致編譯出錯的功能。比如
WITH_CUDA是我用不到的功能,
WITH_TIFF是因爲我在編譯的時候出錯了,
然後我去掉了這個功能。(我在網上看到,有人沒有去掉這個功能也能編譯,可能是因爲我係統裏缺少什麼東西吧?)
配置更改完了之後點一下左下角的configure。然後再點generate.
到這裏,cmake的工作做完了。
從終端進入 opencv_arm這個文件夾然後
make。
但是,肯定會出錯。
必然會遇到的錯誤如下:
/home/yr/OpenCV-2.3.1/modules/flann/include/opencv2/flann/dist.h:
In function 'T cvflann::abs(T) [with T = long double]':
/home/yr/OpenCV-2.3.1/modules/flann/include/opencv2/flann/dist.h:63: error: 'fabsl'
was not declared in this scope
make[2]: *** [modules/flann/CMakeFiles/opencv_flann_pch_dephelp.dir/opencv_flann_pch_dephelp.obj] Error 1
make[1]: *** [modules/flann/CMakeFiles/opencv_flann_pch_dephelp.dir/all] Error 2
make: *** [all] Error 2
../../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
make[2]: *** [bin/opencv_test_calib3d] Error 1
make[1]: *** [modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/all] Error 2
make: *** [all] Error 2
- #include "cv.h"
- #include "highgui.h"
- int main( int argc, char** argv )
- {
- IplImage* pImg; //聲明IplImage指針
- //載入圖像,強制轉化爲Gray
- if( argc == 3 &&
- (pImg = cvLoadImage( argv[1], 0)) != 0 )
- {
- IplImage* pImg2 = cvCreateImage(cvGetSize(pImg),
- pImg->depth,
- pImg->nChannels);
- cvCopy(pImg, pImg2, NULL);
- cvSaveImage(argv[2], pImg2);//把圖像寫入文件
- cvReleaseImage( &pImg ); //釋放圖像
- cvReleaseImage( &pImg2 ); //釋放圖像
- return 0;
- }
- return -1;
- }
#include "cv.h"
#include "highgui.h"
int main( int argc, char** argv )
{
IplImage* pImg; //聲明IplImage指針
//載入圖像,強制轉化爲Gray
if( argc == 3 &&
(pImg = cvLoadImage( argv[1], 0)) != 0 )
{
IplImage* pImg2 = cvCreateImage(cvGetSize(pImg),
pImg->depth,
pImg->nChannels);
cvCopy(pImg, pImg2, NULL);
cvSaveImage(argv[2], pImg2);//把圖像寫入文件
cvReleaseImage( &pImg ); //釋放圖像
cvReleaseImage( &pImg2 ); //釋放圖像
return 0;
}
return -1;
}
- # Package Information for pkg-config
- prefix=/usr/local/arm
- exec_prefix=${prefix}
- libdir=${exec_prefix}/lib
- includedir_old=${prefix}/include/opencv
- includedir_new=${prefix}/include
- Name: OpenCV
- Description: Open Source Computer Vision Library
- Version: 2.3.1
- 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 -lpthread -lrt
- Cflags: -I${includedir_old} -I${includedir_new}
# Package Information for pkg-config
prefix=/usr/local/arm
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 2.3.1
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 -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}
總結:依靠上面資料是可以完成opencv庫的編譯。