Monday 1 April 2013

Cross-compiling OpenCV for OpenWRT (WR703N)

Introduction

OpenCV is currently not available through the package system for the WR703N. This guide shows how to compile OpenCV from source using the OpenWRT toolchain.

This was carried out for the Attitude Adjustment (12.09-rc1) release of OpenWRT and release 2.4.4 of OpenCV.

If you are looking for the libraries and don't want to have to compile them yourself I have placed them on github: http://github.com/mark4h/openwrt


Setting up working directory

Create a working directory
mkdir ~/WR703N_opencv
Download the OpenWRT toolchain (here) and the OpenCV source code (here) into your working directory.

Extract both tars
tar –xjf [filename]
Install make, cmake and cmake-gui.

Set environment variables.
export PATH=$PATH:/home/user/WR703N_opencv/OpenWrt-Toolchain-ar71xx-for-mips_r2-gcc-4.6-linaro_uClibc-0.9.33.2/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/
export STAGING_DIR=/home/user/WR703N_opencv
Create a build directory and change to it.
mkdir ~/WR703N_opencv/opencv-2.4.4/build
cd ~/WR703N_opencv/opencv-2.4.4/build

Configure build

Create a toolchain.cmake file to use with cmake.
vim toolchain.cmake
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR mips )
set( CMAKE_C_COMPILER mips-openwrt-linux-uclibc-gcc )
set( CMAKE_CXX_COMPILER mips-openwrt-linux-uclibc-g++ )
set( CMAKE_FIND_ROOT_PATH /home/user/WR703N_opencv/OpenWrt-Toolchain-ar71xx-for-mips_r2-gcc-4.6-linaro_uClibc-0.9.33.2/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/ )
Execute cmake-gui from within the build directory
cmake-gui ../
In the cmake-gui select configure. Cmake must now be told about the toolchain using the toolchain.cmake file created previously.

Select 'Specify toolchain file for cross-compiling' and then click 'Next'.

Select the toolchain.cmake file you just created and then select 'Finish'.
Cmake should now carry out its configuration.

Once it has finished you are ready to select the build options you require.

The following describes the options I used. They include building all the modules of OpenCV (apart from the python and gpu modules), with V4L and JPEG support. Other support (such as for other image formats) can be included by selecting the required options.

Deselect/select as necessary to include the following options:
  • BUILD_opencv_apps
  • BUILD_opencv_calib3d
  • BUILD_opencv_contrib
  • BUILD_opencv_core
  • BUILD_opencv_features2d
  • BUILD_opencv_flann
  • BUILD_opencv_highgui
  • BUILD_opencv_imgproc
  • BUILD_opencv_legacy
  • BUILD_opencv_ml
  • BUILD_opencv_nonfree
  • BUILD_opencv_objdetect
  • BUILD_opencv_photo
  • BUILD_opencv_stitching
  • BUILD_opencv_ts
  • BUILD_opencv_video
  • BUILD_opencv_videostab
  • BUILD_opencv_world
  • ENABLE_OMIT_FRAME_POINTER
  • WITH_JPEG
  • WITH_V4L
To build shared libraries enable the following option, otherwise static libraries will be built:
  • BUILD_SHARED_LIBS
The following are specific options that did not work:
  • ENABLE_precompiled_headers
  • BUILD_opencv_python
  • BUILD_opencv_gpu
Once you have selected your options click 'configure' again.
Once the configuration has finished click 'generate'.
You are now ready to begin the compiling.
Close cmake.

Compile

On the terminal run make.
make
If no errors were given, you should now find the compiled libraries in the lib folder.

3 comments:

  1. Good job!

    I have tried as you said above, and I make it.
    But when I tried to compile opencv2.1.0 , I got an error: undefined reference of "dlclose"
    Could you please tell me how to solve the problem?
    Thanks!

    ReplyDelete
  2. Hi, I have tried to follow up the short cut by downloading "http://github.com/mark4h/openwrt". I have the folder F:\openwrt-master in my computer, I want to copy this folder into my micro SD that will be plugged in my Arduino YUN. What shuld I do? Many thanks for your support. Regards. Rafa

    ReplyDelete
  3. Should I extract openwrt-ar71xx-WR703N_opencv_libs_debug.tar and the other file? How I can extract them?

    Something like this? tar -x openwrt-ar71xx-WR703N_opencv_libs_debug.tar

    Best regards.

    ReplyDelete