The ultimate OpenCV cross compilation guide for embedded processors
Here I was, working on this project to evaluate computer vision and convolutional neural networks at the edge. I had exported my trained models as frozen graphs and was ready to do some inference using OpenCV. All was going well until I realised OpenCV needed to be built from source for my devices, one was ARMv8-based and the other PowerPC 32-bit.
Building OpenCV on the ARMv8 device was straight-forward as it had enough memory and was fast enough. The PowerPC could not store more than binaries and needed OpenCV to be cross compiled. The OpenCV source code comes with cross-compilation scripts but there was none for the 32-bit PowerPC. I could not find any concrete solution online so I am putting mine here as a source for others.
Note: This instructions below are for a 32-bit PowerPC and you just need to replace PowerPC with your architecture.
a) Downloading cross compilation tool
Download both the gcc and g++ compilers depending on your host and target platforms. A list of the supported architectures can be found on the GNU installation page
sudo apt-get install gcc-powerpc-linux-gnu g+-powerpc-linux-gnu
b) Cloning OpenCV
Download or clone OpenCV from the Github repository to a working directory of your choice (home in my case) and create a build directory inside it.
git clone https://github.com/opencv/opencv.git
c) Cross compiling OpenCV
Create a toolchain.cmake file as shown below in the opencv/platforms/linux directory. Replace the paths with the ones corresponding to the toolchain downloaded in section a.
Move to the build directory and run the command below. To run the command faster in parallel, replace the number after -j with the number of processors you want to use. Running the command nproc will give you the number of available processors.
cmake -D CMAKE_VERBOSE_MAKEFILE=ON \
-D CMAKE_GENERATE_PKGCONFIG=YES \
-D CMAKE_TOOLCHAIN_FILE=../platforms/linux/ppc-gnu.toolchain.cmake \ ..make -j16
sudo make install
OpenCV should now be installed in the install_prefix directory. Next, copy the binaries to your target platform. Copying all the binaries in your prefix_directory may cause some mismatch errors and so it is advisable to only copy the files added after installing OpenCV.
Lastly, update the paths to locate the new OpenCV library. Depending on your target system, the /etc directory will have either an ld.so.conf.d directory or an ld.so.conf file. In the case of the former create an open.cv file, else edit the file directly. Run the following commands on your target device.
vi /etc/ld.so.conf#add the path to the OpenCV library, save, and exitldconfig -v
export LD_LIBRARY_PATH = $LD_LIBRARY_PATH:/path/to/opencv/library
d) Testing OpenCV
I added a short c++ code to load and resize an image using OpenCV.
Cross compile this code on your host platform.
powerpc-linux-gnu-g++-8 test_opencv.cpp -o test_opencv $(pkg-config --cflags --libs opencv4)
Execute the compiled binary on your target device.
Thanks for reading and I hope this helps you speed your work. If you enjoyed it, share so others can benefit from it as well!