How to use OpenCV with SDSoC



Introduction

You are probably here because you want to create a SDSoC project based on OpenCV. In this tutorial the steps to configure your project will be presented, the SDSoC version used is 2015.4 with the included Zybo platform. Just a reminder before starting: OpenCV requires Linux to be easily used because it uses many other libraries such as pthread... We won't cover any bare-metal configuration here.

Prerequisites

  • SDSoC
  • A platform including a Linux kernel + root file system
  • A ZYNQ powered board
  • A Linux based computer (for step 2)

Step 1 : Get OpenCV

You have at least two choices, the first one is to download and cross-compile the library. The second one is to get an already cross-compiled library. The good news is that we already have the version 2.4.5 built in the SDSoC installation folder (this is what I chose for this tutorial).

Step 2 : Modify the plateform

The idea is to add the compiled library to the root file system of the SDSoC platform you want to use for your project. After this operation the executable (your application) will be able to load the library. The type of the file system image used in the Zybo platform is initramfs, if you have another type you might get more information on Xilinx's wiki.

2.1) Copy the root file system to a temp folder

Copy the archive named "uramdisk.image.gz" located in the boot folder of the platform and copy it to a temp folder. Open a terminal (if not already opened) and move into this folder.

2.2) Remove U-Boot's header to get a cpio archive

2.3) Uncompress this cpio archive in a newly created folder

2.4) Copy the library with it's headers (-p to preserve links)

2.5) Compress to cpio and gunzip

2.6) Add U-Boot's header


Make sure the permissions of the file are set to 755:

2.7) Replace the newly created image in the platform

Step 3 : Create and configure a new project in SDSoC

3.1) Include path

Create a new project, choose your modified platform and select that you want to use Linux. Right click on the project from the project explorer (left panel) and click on properties. Go to "C/C++ Build" -> "Settings" and "SDS++ compiler" -> "Directories", add the include path (/opt/Xilinx/SDSoC/2015.4/SDK/2015.4/data/embeddedsw/ThirdParty/opencv/include) to OpenCV for the selected "build/debug/estimate configuration" as shown on the picture below: SDS++ add include path

3.2) Link the library

Once done, move to the SDS++ linker's configuration and under "Libraries", add the libraries you want to use in your project and the path where they reside (for me it is : /opt/Xilinx/SDSoC/2015.4/SDK/2015.4/data/embeddedsw/ThirdParty/opencv/lib). SDS++ add linked libs and path

3.3) Add -rpath-link

Finally, you need to a specify the path where the linker has to search for the dependencies (libs) needed by OpenCV. Move to SDS++ linker's configuration and under "Miscellaneous" -> "Linker Flag" add the following line (-Wl,-rpath-link=/opt/Xilinx/SDSoC/2015.4/SDK/2015.4/data/embeddedsw/ThirdParty/opencv/lib). The -Wl option indicates to the tool-chain that the option -rpath-link=... will be given to the linker. SDS++ add link flags

Step 4 : Test!

Now the most exciting part of this tutorial : testing! The following piece of code will perform a FAST corner detection on an image file :


Build your program, once finished copy the content of the “sd_card” folder to the target's SD card and don't forget to add a test image in its root!
Once the boot operation is finished, run the executable on the target with this command :

If everything worked as expected, the executable should have generated an output image “ocv.jpg” which looks like this:

output OpenCV image

Conclusion

We added the built library into Linux's root file system used in the SDSoC platform. Then, we configured a new project based on this new SDSoC platform with the correct include path and links. Starting from this point you are free to use OpenCV!