13 Şubat 2015 Cuma

Building OpenCV for ARM (Cross-Compilation)

The main purpose of this post is to build OpenCV for ARM, write a simple code, cross-compile it and make it work in our target environment (ARM). This is post is composed of two parts: building, coding and testing.

Note that the host where we're working is Ubuntu 12.04 and the cross-compiler used for ARM is arm-none-linux-gnueabi

BUILD:

In order to cross-compile OpenCV, we will use Cmake to generate makefiles. Install it first.
  • $ sudo apt-get install cmake cmake-curses-gui
We need to download OpenCV source code. We've used  opencv-2.4.9
  • $ cd ~
  • $ wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip
  • $ unzip opencv-2.4.9.zip
 Toolchain for cross-compilation should be added to your environment PATH.
  • $ gedit .bashrc
Add the path of your toolchain at the end of file as below and save it. Do not forget to update the line below with yours. After closing .bashrc, "reboot" or "logout and login".
  • export PATH=$PATH:/home/sezerb/Projects/ToolChain/arm-2012.09/bin
 For cross-compilation:
  • $ mkdir ~/build && cd ~/build
  • $ touch toolchain.cmake 
  • $ gedit toolchain.cmake
Edit toolchain.cmake as below:

Note that since we have added our toolchain to environment PATH before, we've commented out the last line.

OpenCV supports "neon" since version 2.3. Since we are using 2.4.9 now, we can enable/disable neon support from CMake gui later on.

Now it's time to cross-compile OpenCV:
  • $ cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../opencv-2.4.9/
  • ccmake .
A screen will be shown full of options. You can set options On/Off by Enter. For the first compilation we've set most of the options starting with "WITH" to Off. Adjust them according to your needs. For neon support do not forget to set USE_O2 on.
Now press 'c' and 'g' to configure and generate makefile.
  • $ make
  • $ make install
Under "/build" folder, there will be an install folder including built opencv libraries, headers etc. In "/install" there are 4 directories: bin, include, lib, share. Now we're done. We can use these libraries in our target environment.

CODING and TESTING:

Create a file named as Main.cpp as below: Create a Makefile as below and please do not forget to update the paths with yours. In terminal, go to the directory where these files located and "$ make". Now, we have a "Main" binary to be executed in our target environment. Before running this binary in our target environment, we need to copy built libraries to our target system. "/lib", "/usr/lib/" and "/usr/local/lib" are directories where you may copy the built opencv libraries in "/install/lib/". We have copied them to "/lib" folder in our target system. If you do not have any of these directories in your target system, you may create a folder such as "/usr/lib" to copy built opencv libraries. Also copy the headers located in "/install/include/" to "/usr/include/" in your target system.
After copying the libraries and header, copy the "Main" executable binary to your target system and run it by typing:
  • ./Main
It's done, OpenCV library is able to work in our target system :).