Tuesday, April 2, 2013

Building OpenCV-2.4.X for Freescale's i.MX6 BSP (LTIB)

I was working on this post for a long time, and I didn't share any information before because I Was facing a big problem to get the highgui library successfully built to our embedded system. And as you may know, the highgui is very important when you need to open a video stream, create a camera device and even work with windows in OpenCV.

Last week I managed to build it successfully using the latest Freescale's BSP (kernel 3.0.35) and I'm going to show you how to do that in the following lines.

First of all,  you can download the Freescale's i.MX6 BSP here !


Unfortunately getting the latest BSP doesn't mean you are going to get all the latest packages. The only things that are really update in this BSP and probably all others are the kernel and specific drivers for the current hardware you are going to work with. The most commons packages like GTK, GLIB2, PANGO, CAIRO and etc... are not updated, and in order to build the new OpenCV 2.4.X we will need to upgrade these packages and install a new one, the v4l-utils.

So let's stop with this BLAH BLAH BLAH and get into what is really interesting  !


Step 1 - Building the dependencies
-----------------------------------------------------------
 1) Assuming you already have the BSP installed (min profile) in your host machine, you will need to upgrade the GLIB2 package and install the v2l-utils one.

2) Get the new spec files at: https://github.com/andreluizeng/ltib-pkgs-upgrade

3) the new GLIB2 now has a dependency, the LIBFFI. (you can also get the spec file of this lib in the link above.

4) Build & install the libffi as follows:
     mkdir ../ltib/dist/lfs-5.1/libffi
     cp ../git/ltib-pkgs-upgrade/libffi.spec ../ltib/dist/lfs-5.1/libffi
     cp ../git/ltib-pkgs-upgrade/libffi-3.0.12-includedir-1.patch /opt/freescale/pkgs
     cp ../downloads/libffi.tar.gz /opt/freescale/pkgs
* you can find the link that you can get the libffi from.
      cd ../ltib
     ./ltib -p libffi.spec -m prep
     ./ltib -p libffi.spec -m scbuild
     ./ltib -p libffi.spec -m scdeploy

5) Build & install the new glib2
    cd ../ltib
    cp ../git/ltib-pkgs-upgrade/glib2.spec ../ltib/dist/lfs-5.1/glib2
    cp ../downloads/glib-2.35.9.tar.xz /opt/freescale/pkgs
    ./ltib -p glib2.spec -m prep
    ./ltib -p glib2.spec -m scbuild
    ./ltib -p glib2.spec -m scdeploy

6) Build & install the v4l-utils packages
    mkdir ../ltib/dist/lfs-5.1/v4l-utils
    cp ../git/ltib-pkgs-upgrade/v4l-utils.spec ../ltib/dist/lfs-5.1/v4l-utils
    cp ../downloads/v4l-utils-0.9.3.tar.bz2 /opt/freescale/pkgs
    cd ../ltib

    ./ltib -p v4l-utils.spec -m prep
    ./ltib -p v4l-utils.spec -m scbuild
    ./ltib -p v4l-utils.spec -m scdeploy


After all these steps you should have the necessary dependencies built for now building the OpenCV 2.4.X.


Step 2 - Cross-Compiling OpenCV-2.4.X
-----------------------------------------------------------------

When Cross-compiling using cmake you need a special file containing the information about the toolchain you are going to use. In our case we can create this file as:


1) touch toolchain.cmake & vi toolchain.cmake

2) add the following information:

               # this one is important
               set( CMAKE_SYSTEM_NAME Linux )

               #this one not so much
               set( CMAKE_SYSTEM_PROCESSOR arm )

               # specify the cross compiler
                set( CMAKE_C_COMPILER /opt/freescale/usr/local/gcc-4.6.2-glibc-    2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-none-linux-gnueabi-gcc )
               set( CMAKE_CXX_COMPILER /opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-none-linux-gnueabi-g++ )

              # where is the target environment - point to your rootfs here
              set( CMAKE_FIND_ROOT_PATH  /home/andre/bsps/imx6x/1301/ltib/rootfs )

              # search for programs in the build host directories
              set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )

             # for libraries and headers in the target directories
             set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
             set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )

             # point to your rootfs path here 
             set( CMAKE_CXX_FLAGS "-L/home/andre/bsps/imx6x/1301/ltib/rootfs/usr/lib" )

3) download OpenCV-2.4.X here or here.

4) untar the package:
    tar -xvf OpenCV-2.4.2.tar.bz2

5) cd OpenCV-2.4.2
    mkdir build
    cd build
    cp ../../toolchain.cmake .
    cmake -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake ../

after these steps you should have the necessary files to be built in your build directory, you now just need to configure which extra packages you want to add or remove for your OpenCV profile.

In your build folder:

6) ccmake .

7) you can let your configuration similar to mine:

BUILD_DOCS                          OFF
 BUILD_EXAMPLES                OFF
BUILD_JASPER                       OFF
BUILD_JPEG                           OFF
 BUILD_PACKAGE                  ON
BUILD_PERF_TESTS              ON
 BUILD_PNG                           OFF
 BUILD_SHARED_LIBS           ON
 BUILD_TESTS                        ON
 BUILD_TIFF                           OFF
 BUILD_WITH_DEBUG_INFO ON
 BUILD_ZLIB                          OFF
 BUILD_opencv_calib3d        ON
 BUILD_opencv_contrib        ON
 BUILD_opencv_core             ON
 BUILD_opencv_features2d  ON
 BUILD_opencv_flann            ON
 BUILD_opencv_gpu              ON
 BUILD_opencv_highgui        ON
 BUILD_opencv_imgproc       ON
 BUILD_opencv_legacy          ON
 BUILD_opencv_ml                 ON
 BUILD_opencv_nonfree        ON
 BUILD_opencv_objdetect     ON
 BUILD_opencv_photo           ON
 BUILD_opencv_stitching      ON
 BUILD_opencv_ts                  ON
 BUILD_opencv_video            ON
 BUILD_opencv_videostab     ON
 CMAKE_BUILD_TYPE
 CMAKE_CONFIGURATION_TYPES  Debug;Release
 CMAKE_INSTALL_PREFIX             /home/andre/imx_applications/OpenCV-2.4.2/build/install
 CMAKE_TOOLCHAIN_FILE             /home/andre/imx_applications/OpenCV-2.4.2/toolchain.cmake
 CMAKE_VERBOSE                   OFF
 CUDA_BUILD_CUBIN               OFF
 CUDA_BUILD_EMULATION     OFF
 CUDA_SDK_ROOT_DIR             CUDA_SDK_ROOT_DIR-NOTFOUND          
 CUDA_TOOLKIT_ROOT_DIR     CUDA_TOOLKIT_ROOT_DIR-NOTFOUND   
 CUDA_VERBOSE_BUILD          OFF
 EIGEN_INCLUDE_PATH            EIGEN_INCLUDE_PATH-NOTFOUND
ENABLE_NOISY_WARNINGS                OFF
 ENABLE_OMIT_FRAME_POINTER      ON
 ENABLE_PRECOMPILED_HEADERS  ON
 ENABLE_PROFILING                           OFF
 ENABLE_SOLUTION_FOLDERS          OFF
 EXECUTABLE_OUTPUT_PATH           /home/andre/imx_applications/OpenCV-2.4.2/build/bin
 INSTALL_C_EXAMPLES                      OFF
 INSTALL_PYTHON_EXAMPLES          OFF
 INSTALL_TO_MANGLED_PATHS        OFF                 
 LIBRARY_OUTPUT_PATH_ROOT         /home/andre/imx_applications/OpenCV-2.4.2/build                                                                                              
 OPENCV_CAN_BREAK_BINARY_COMPAT   OFF
 OPENCV_CONFIG_FILE_INCLUDE_DIR   /home/andre/imx_applications/OpenCV-2.4.2/build                                                                                              
 OPENCV_WARNINGS_ARE_ERRORS
OFF OPENEXR_INCLUDE_PATH                  OPENEXR_INCLUDE_PATH-NOTFOUND PVAPI_INCLUDE_PATH            PVAPI_INCLUDE_PATH-NOTFOUND PYTHON_PACKAGES_PATH     lib/python2.7/site-packages WITH_1394         ON
 WITH_CUBLAS                   OFF
 WITH_CUDA                       OFF
 WITH_CUFFT                     ON
WITH_EIGEN                       ON
WITH_FFMPEG                   ON
 WITH_GSTREAMER           ON
 WITH_GTK                          ON
 WITH_JASPER                    OFF
 WITH_JPEG                        ON
 WITH_OPENEXR                ON
 WITH_OPENGL                  OFF
 WITH_OPENNI                   OFF
 WITH_PNG                         ON
 WITH_PVAPI                       ON
 WITH_QT                           OFF
 WITH_TBB                         OFF
 WITH_TIFF                        ON
 WITH_UNICAP                   OFF
 WITH_V4L                         ON
 WITH_XIMEA                     OFF
 WITH_XINE                        OFF
   
you basically removed the gpu acceleration (we don't support full profile OpenCL), we enabled the v4l library and also disabled some unnecessary ones, you can try your on configuration if you will.

8) make -j

9) make install


If you reached the step 9 with no issues you should have all  your cross built OpenCV in the install folder. Just copy it to your rootfs:

10) sudo cp -a build/install ../ltib/rootfs/usr

and you know have a OpenCV-2.4.X installation in your embedded system.

Now the result using a simple code to open a camera device and display the image:








EOF !


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
                                                            UPDATE
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

I Got some issues building GTK when following the procedure above, so here is the deal to avoid any headache:
  1. Install X11        (package list)
  2. Install GTK+    (package list)
  3. add the LIBFFI and V4L-UTILS like described earlier.
  4. Upgrade the GLIB2 package like described earlier.
  5. Cross Compile the OpenCV Library.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

EOF² !

10 comments:

  1. Hello, do you think I can install OpenCV on the i.mx Sabrelite or the i.mx Wandboard ?
    Can you use Yocto instead of the Freescale BSP ?

    ReplyDelete
  2. hi
    thanks for your post.

    I have an error can you plz on that...
    while upgrading glib2 i face following error
    --------------------------------------------------------------
    configure: error: *** Working zlib library and headers not found ***
    error: Bad exit status from /opt/freescale/ltib/tmp/rpm-tmp.99147 (%build)


    RPM build errors:
    Bad exit status from /opt/freescale/ltib/tmp/rpm-tmp.99147 (%build)
    Build time for glib2: 2 seconds

    Failed building glib2


    f_scbuild() returned an error, exiting
    traceback:
    main:572
    -----------------------------------------------------
    can you throw some light on this.

    ReplyDelete
    Replies
    1. do you have zlib installed ? you can select in the ltib´s package list.

      Delete
    2. you can set zlib to ON in ccmake . step

      Delete
  3. Upgrading v4l-utils too have dependencies.....

    configure: error: cannot find jpeglib.h
    error: Bad exit status from /opt/freescale/ltib/tmp/rpm-tmp.79412 (%build)


    RPM build errors:
    Bad exit status from /opt/freescale/ltib/tmp/rpm-tmp.79412 (%build)
    Build time for v4l-utils: 4 seconds

    Failed building v4l-utils


    f_scbuild() returned an error, exiting
    traceback:
    main:572

    Exiting on error or interrupt

    ReplyDelete
  4. Hello,

    I am trying to install OpenCV for i.mx53 QSB with linux Freescale BSP.

    I was able to install glib2 & libffi, but having issue while installing v4lutils.

    Have you tried this same procedure on i.MX53 QSB? If yes, were you successful in compiling?

    Build Log:

    --------------------------
    libtool: compile: gcc -DHAVE_CONFIG_H -I. -I../.. -fvisibility=hidden -std=c99 -I../../lib/include -Wall -Wpointer-arith -D_GNU_SOURCE -I../../include -g -O2 -MT libv4l_mplane_la-libv4l-mplane.lo -MD -MP -MF .deps/libv4l_mplane_la-libv4l-mplane.Tpo -c libv4l-mplane.c -fPIC -DPIC -o .libs/libv4l_mplane_la-libv4l-mplane.o
    In file included from libv4l-mplane.c:28:
    ../../include/linux/videodev2.h:493: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:523: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:1122: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:1182: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:1275: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:1322: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:1548: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:1597: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:1718: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:1820: warning: declaration does not declare anything
    ../../include/linux/videodev2.h:1876: warning: declaration does not declare anything
    libv4l-mplane.c:47: warning: declaration does not declare anything
    libv4l-mplane.c:49: warning: declaration does not declare anything
    libv4l-mplane.c: In function 'plugin_init':
    libv4l-mplane.c:80: error: 'struct mplane_plugin' has no member named 'mplane_capture'
    libv4l-mplane.c:84: error: 'struct mplane_plugin' has no member named 'mplane_output'
    libv4l-mplane.c:87: error: 'struct mplane_plugin' has no member named 'mplane'
    libv4l-mplane.c:99: error: 'struct mplane_plugin' has no member named 'mplane_capture'
    libv4l-mplane.c:100: error: 'struct mplane_plugin' has no member named 'mplane_output'
    libv4l-mplane.c: In function 'plugin_ioctl':
    libv4l-mplane.c:351: error: 'struct mplane_plugin' has no member named 'mplane_capture'
    libv4l-mplane.c:354: error: 'struct mplane_plugin' has no member named 'mplane_output'
    make[3]: *** [libv4l_mplane_la-libv4l-mplane.lo] Error 1
    make[3]: Leaving directory `/opt/imx53-ltib/ltib/rpm/BUILD/v4l-utils-0.9.3/lib/libv4l-mplane'
    make[2]: *** [all-recursive] Error 1
    make[2]: Leaving directory `/opt/imx53-ltib/ltib/rpm/BUILD/v4l-utils-0.9.3/lib'
    make[1]: *** [all-recursive] Error 1
    make[1]: Leaving directory `/opt/imx53-ltib/ltib/rpm/BUILD/v4l-utils-0.9.3'
    make: *** [all] Error 2
    error: Bad exit status from /opt/imx53-ltib/ltib/tmp/rpm-tmp.59598 (%build)


    RPM build errors:
    Bad exit status from /opt/imx53-ltib/ltib/tmp/rpm-tmp.59598 (%build)
    Build time for v4l-utils: 23 seconds

    Failed building v4l-utils


    f_scbuild() returned an error, exiting
    traceback:
    main:560

    ReplyDelete
    Replies
    1. hello
      have you resolved this, i ve gopt the same issue,
      THX

      Delete
    2. have you guys solved the issue ?

      Delete
  5. Hi Ram,
    thanks for your comment, I really appreciated that.

    cheers,
    Andre

    ReplyDelete