opencv 移植

这个我有亲自试过,是可以移植的,而且在嵌入式linux系统中进行了图像处理,用opencv做的


Building OpenCV for ARM Cortex-A8

Translate this page to   

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
  $ export PATH=$PATH:/opt/arm-2010.12/bin
  $ 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/
cmake -D CMAKE_BUILD_TYPE=RELEASE 可以选择release 
  • 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.

然后 make

make install


遇到错误:

可以参考以下文章

环境:arm开发板是 ok6410,主机是Ubuntu10.04

1,准备工作:交叉编译工具用光盘自带的arm-linux-4.4.1.tar.gz,解压到/usr/local/arm目录下(将/usr/local/arm/bin加入环境变量);下载opencv2.2的源码;安装cmake build-essential;安装必要的库:  libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev libjpeg62-dev libtiff4-dev  libjasper-dev

2,用到的主要目录说明:
交叉编译工具链所在  /usr/local/arm
编译好的opencv库所在  /usr/local/opencv-arm
安装opencv的目录     /usr/local/arm-linux
opencv源码所在      ~/tiaozhansai/OpenCV-2.2.0

3,在终端运行sudo cmake-gui  
    选择源代码目录:/home/xin/tiaozhansai/OpenCV-2.2.0
      选择Build目录:/usr/local/opencv-arm/
       点击Configure,保持generator为Unix Makefiles,选择Specify options for cross-compiling,点击Next,
       Operating System填写arm-linux
       C Compilers填写/usr/local/arm/4.4.1/bin/arm-linux-gcc
       C++ Compilers填写/usr/local/arm/4.4.1/bin/arm-linux-g++
       程序库的Target Root填写/usr/local/arm/4.4.1/
然后点击Finish。
修改默认配置,默认安装目录为/usr/local,为便于查找生成的库文件,把CMAKE_INSTALL_PREFIX变量改为/usr/local/arm-linux  ,点击Generate生成Makefile。

4,在/usr/local/opencv-arm目录下$sudo make
(1),80%报错:
。。。。/home/xin/tiaozhansai/OpenCV-2.2.0/modules/ml/src/gbt.cpp:474: error: 'expl' was not declared in this scope

    修改gbt.cpp中14行,16行:注释掉

                    //#if ANDROID

                    #define expl(x) exp(x)
                    //#endif
        错误消除。

(2),83%报错:
Linking CXX executable ../../bin/opencv_createsamples
../../lib/libopencv_core.so: undefined reference to `clock_gettime'
../../lib/libopencv_highgui.so: undefined reference to `_TIFFerrorHandler'
../../lib/libopencv_highgui.so: undefined reference to `_TIFFrealloc'
../../lib/libopencv_core.so: undefined reference to `pthread_key_create
    解决方法:修改CMakeCache.txt,CMAKE_EXE_LINKER_FLAGS原来为空,加上-lpthread -lrt,重新编译,错误消除 

(3),83%报错:
Linking CXX executable ../../bin/opencv_createsamples
../../lib/libopencv_highgui.so: undefined reference to `_TIFFerrorHandler'
../../lib/libopencv_highgui.so: undefined reference to `_TIFFrealloc'
../../lib/libopencv_highgui.so: undefined reference to `_TIFFmalloc'
../../lib/libopencv_highgui.so: undefined reference to `_TIFFmemcpy'
../../lib/libopencv_highgui.so: undefined reference to `TIFFOpen'
../../lib/libopencv_highgui.so: undefined reference to `_TIFFfree'
../../lib/libopencv_highgui.so: undefined reference to `_TIFFwarningHandler'
../../lib/libopencv_highgui.so: undefined reference to `_TIFFmemcmp'
../../lib/libopencv_highgui.so: undefined reference to `_TIFFmemset'
    解决方法:修改CMakeCache.txt,WITH_TIFF:BOOL=ON,改成OFF(不再对TIFF图像格式支持。这里我前面已经装过libtiff4-dev,但是编译报错,我不明白是为什么),重新编译,错误消除。

(4),96%报错:
CMakeFiles/opencv_test.dir/src/highguitest.obj: In function `CV_HighGuiTest::run(int)':
highguitest.cpp:(.text._ZN14CV_HighGuiTest3runEi+0x18): warning: the use of `tmpnam' is dangerous, better use `mkstemp'
../../lib/libopencv_features2d.so: undefined reference to `cv::SIFT::SIFT(double, bool, bool, int, int, int, int)'
    解决方法:在OpenCV2.2.0/modules/features2d/src/sift.cpp下 
            #ifdef __arm__
            #define ARM_NO_SIFT
            #endif   
        注释掉#define ARM_NO_SIFT.错误消除,
成功!

$sudo make install 

5,opencv库复制到ok6410,并测试
(1)借用某个博客上的测试程序,来测试编译的是否成功。
新建test.cpp:
    // test.cpp  
      
    #include <cv.h>  
    #include <cxcore.h>  
    #include <highgui.h>  
      
      
    int main()  
    {  
            CvCapture* capture = NULL;  
            IplImage* frame = NULL;  
      
            if( !(capture = cvCaptureFromCAM(-1)))  
            {  
                    fprintf(stderr, "Can not open camera./n");  
                    return -1;  
            }  
      
            cvNamedWindow("video", 1);  
      
            while(frame = cvQueryFrame( capture ) )  
            {  
                    cvShowImage("video", frame);  
            }  
      
            cvDestroyWindow("video");  
            cvReleaseCapture(&capture);  
            return 0;  
    }  

编译test.cpp:$ arm-linux-g++ -I/usr/local/arm-linux/include/opencv/ -L/usr/local/arm-linux/lib/ -lcv -lcxcore -lhighgui -lpthread -lrt -o test test.cpp
报错:/usr/local/arm/4.4.1/bin/../lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -lcv

google之,发现这个问题是找不到libcv.so这个动态库。
通过find命令发现在/usr/local/arm-linux/lib/下面确实没有这个库,而且这个目录下的库都是libopencv*形式的,我猜测是不是opencv2.2相比2.1作了不少改动,网上调研发现确实时这样的,opencv2.2重新组织库结构,用较详细,更小的模块来取代cxcore, cv, cvaux, highgui 和 ml库.
于是,试着把命令改成:$ arm-linux-g++ -I/usr/local/arm-linux/include/opencv/ -L/usr/local/arm-linux/lib/ -lopencv_core  -lopencv_highgui -lpthread -lrt -o test test.cpp
结果还是报上面的错误,这次我注意到它是在。。。/arm-none-linux-gnueabi/。。这个目录下找动态库的,也就是说,尽管我指定了头文件,库文件的搜索路径,但是它没有去我指定的路径下找。于是我把/usr/local/opencv-arm/include/opencv/下的头文件和/usr/local/opencv-arm/lib下的动态库全都拷贝到/usr/local/arm/4.4.1/arm-none-linux-gnueabi下面对应的目录下,然后再编译:
$ arm-linux-g++ -lopencv_core -lopencv_highgui -lpthread -lrt -o test test.cpp 

成功!

(2)将库文件s 和 编译好的测试程序test 拷贝到开发板测试
用sd卡拷贝,遇到问题:
cp: cannot create '../../lib/libopencv_calib3d.so': Read-only file system       
cp: cannot create '../../lib/libopencv_contrib.so': Read-only file system       
cp: cannot create '../../lib/libopencv_core.so': Read-only file system  

这个跟cramfs文件系统是只读的文件系统相关,烧写yaffs文件系统即可解决这个问题。


库的问题

export LD_LIBRARY_PATH=/opt/opencv8148:$LD_LIBR
ARY_PATH

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值