6-2 Cross Compilation of OpenCV
This document outlines the steps to cross-compile OpenCV applications for ARM-based Renesas platforms using a Yocto SDK, including both C++ and Python implementations.
Environment Setup for Renesas Cross Compilation
To begin, follow the instructions in Section 3-3 to build and install the SDK. By default, the SDK is installed at:
/opt/poky/3.1.31
To enable the cross-compilation environment, execute the following commands:
cd /opt/poky/3.1.31
unset LD_LIBRARY_PATH
. environment-setup-aarch64-poky-linux
To verify the setup, run:
echo $CXX
You should see an output like this:
aarch64-poky-linux-g++ -mtune=cortex-a55 -fstack-protector-strong -D_FORTIFY_SOURCE=2 ...
Cross-Compiling OpenCV for ARM-Based Linux Systems
Refer to the following official OpenCV documentation:
OpenCV C++ Example Program and CMake Build
Example Program: DisplayImage.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image = imread(argv[1], 1);
if (!image.data) {
printf("No image data\n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
CMake Configuration: CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(DisplayImage)
find_package(OpenCV REQUIRED)
add_executable(DisplayImage DisplayImage.cpp)
target_link_libraries(DisplayImage ${OpenCV_LIBS})
Sample Image
Download Lenna.jpg
from:
Building and Verifying Locally (x86_64)
mkdir build
cd build
cmake ..
make
file ./DisplayImage
Expected output:
DisplayImage: ELF 64-bit LSB pie executable, x86-64 ...
Run the program:
./DisplayImage ../lena.jpg
Cross-Compiling for aarch64
Step 1: Set Cross-Compilation Environment
cd /opt/poky/3.1.31
unset LD_LIBRARY_PATH
. environment-setup-aarch64-poky-linux
Step 2: Clean and Rebuild
cd /path/to/OpenCV/HelloWorld
rm -r build
mkdir build
cd build
cmake ..
make
Step 3: Verify Architecture
file ./DisplayImage
Expected output:
DisplayImage: ELF 64-bit LSB pie executable, ARM aarch64 ...
Running on the Renesas Board
Transfer the DisplayImage binary to the Renesas board using the scp
command and run it with:
./DisplayImage lena.jpg
Building with a Makefile
Sample Makefile (System Installed OpenCV)
CPP = g++
CPPFLAGS = -L/usr/lib/x86_64-linux-gnu \
-lopencv_core -lopencv_highgui \
-lopencv_imgproc -lopencv_imgcodecs -lOpenCL \
-I/usr/include/opencv4
all: DisplayImage
DisplayImage: DisplayImage.cpp
$(CPP) $^ -o $@ $(CPPFLAGS)
Sample Makefile (Source-Installed OpenCV)
CPP = g++
CPPFLAGS = -L/usr/local/lib \
-lopencv_core -lopencv_highgui \
-lopencv_imgproc -lopencv_imgcodecs -lOpenCL \
-I/usr/local/include/opencv4
all: DisplayImage
DisplayImage: DisplayImage.cpp
$(CPP) $^ -o $@ $(CPPFLAGS)
To build:
make
Direct Compilation with g++
Without pkg-config
:
g++ -o DisplayImage DisplayImage.cpp \
-I/usr/local/include/opencv4 \
-L/usr/local/lib \
-lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs
With pkg-config
:
Check if opencv4.pc
exists:
pkg-config --cflags opencv4
Compile:
g++ -o DisplayImage DisplayImage.cpp `pkg-config --cflags opencv4` `pkg-config --libs opencv4`
For Cross-Compilation with Yocto SDK:
$CXX -o DisplayImage DisplayImage.cpp `pkg-config --cflags opencv4` `pkg-config --libs opencv4`
OpenCV Python Example
Python Script: DisplayImage.py
import cv2
import sys
if len(sys.argv) != 2:
print("usage:", sys.argv[0], "<Image_Path>")
sys.exit(-1)
img = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR)
cv2.namedWindow("Display Image", cv2.WINDOW_AUTOSIZE)
cv2.imshow("Display Image", img)
cv2.waitKey(0)
Run:
python3 DisplayImage.py lena.jpg
Using Matplotlib to Display Image
import cv2
import sys
from matplotlib import pyplot as plt
if len(sys.argv) != 2:
print("usage:", sys.argv[0], "<Image_Path>")
sys.exit(-1)
img = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR)
plt.axis("off")
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
Install Matplotlib:
pip install matplotlib