The ultimate OpenCV cross compilation guide for embedded processors

Isma-Ilou Sadou
Mar 12 · 3 min read
Image for post
Image for post
Photo by Louis Reed on Unsplash

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

sudo apt-get install gcc-powerpc-linux-gnu g+-powerpc-linux-gnu

b) Cloning OpenCV

git clone https://github.com/opencv/opencv.git
cd ~/opencv
mkdir build

c) Cross compiling OpenCV

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

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.

./test_opencv imagepath

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!

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Isma-Ilou Sadou

Written by

Embedded Systems master student, Deep Learning enthusiast, and Aspiring CEO

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Isma-Ilou Sadou

Written by

Embedded Systems master student, Deep Learning enthusiast, and Aspiring CEO

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store