Cross-compile OpenCV 4.5.4 (including cross-compilation environment configuration)

This article is reproduced from the 交叉编译OpenCV 4.5.4(包含交叉编译环境配置) - Duo - MilkV Community, and the original author is xiaomin

Optimizing OpenCV for C906 with Cross-Compilation

I. Getting the OpenCV Source Code

You can download the source code from the following link: 平头哥芯片开放社区-平头哥 (t-head.cn)
Choose the latest OpenCV-V1.0.3.r0-20230426.tar.gz file for download, and you’ll need to register an account.

It seems that the forum does not support uploading attachments, so everyone may need to download it themselves.

II. Obtaining the riscv-v Cross-Compilation Toolchain

These are precompiled tools from the Alcide platform, including an ELF compilation toolchain for C906L and two versions of the GNU and Musl toolchains.

I chose the toolchain with Musl.

You can download the cross-compilation toolchain from Alcide’s official repository on Ubuntu by running the following command:

wget https://sophon-file.sophon.cn/sophon-prod-s3/drive/23/03/07/16/host-tools.tar.gz

III. Installing the riscv-v Cross-Compilation Toolchain

After downloading, use the following command to extract the toolchain:

tar xvf host-tools.tar.gz

To make it more convenient for later use, create an environment setup script. Follow these steps:

  1. Enter the cross-compilation toolchain directory:

    cd host-tools
    
  2. Create a script:

    touch riscv-crosstool-env-init
    
  3. Modify the script:

    vi riscv-crosstool-env-init
    
  4. Paste the following content into the script. Make sure to replace HOST_TOOL_PATH with your actual toolchain path, which you can obtain by running pwd in the shell while in that directory:

    HOST_TOOL_PATH=/home/xxx/milk-v/CV180x/host-tools
    
    export PATH="$HOST_TOOL_PATH/gcc/riscv64-linux-x86_64/bin:$HOST_TOOL_PATH/gcc/riscv64-linux-musl-x86_64/bin:$HOST_TOOL_PATH/gcc/riscv64-elf-x86_64/bin:$PATH"
    
    echo -e "\033[33;1mCross-Compilation Toolchain Environment Configuration Complete!\033[0m"
    

    Save the file by pressing ESC, then typing :q.

Now, every time you want to use the cross-compilation toolchain, simply run:

source riscv-crosstool-env-init

image
Type riscv64 and then press TAB to check if the current GCC toolchain is installed. The toolchains in the 1 section are for compiling firmware for real-time systems, while the ones in the 2 and 3 sections are for compiling Linux applications.

image

IV. Cross-Compiling OpenCV

Extract the OpenCV source code that you downloaded earlier:

tar xvf OpenCV-V1.0.3.r0-20230426.tar.gz

In the OpenCV/platforms/linux/ directory, make a copy of riscv64-gcc.toolchain.cmake and rename it to riscv64-musl-gcc.toolchain.cmake. Open the file and make the modifications as shown in the image provided in your article.
image

This version of CMake checks that the build is not in the source code directory, or else the cmake command will fail. So, create a build directory at the same level as the source code:

mkdir OpenCV_build

Enter the build directory:

cd OpenCV_build

Execute the following compilation command (adjust paths as needed):

cmake /home/yxm/milk-v/CV180x/OpenCV -DCMAKE_TOOLCHAIN_FILE="/home/yxm/milk-v/CV180x/OpenCV/platforms/linux/riscv64-musl-gcc.toolchain.cmake" -DWITH_OPENCL=OFF -DBUILD_opencv_calib3d=ON -DBUILD_ZLIB=ON -DBUILD_PNG=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_WITH_INSTALL_RPATH=1 -DCMAKE_INSTALL_PREFIX=./c906_install -DBUILD_CSI_CV=ON -DWITH_OPENMP=OFF -DWITH_PTHREADS_PF=OFF -DCORE=C906FDV -D BUILD_SHARED_LIBS=ON -D BUILD_opencv_features2d=ON -D BUILD_opencv_apps=OFF -D BUILD_TESTS=OFF -D BUILD_opencv_highgui=OFF -D BUILD_PERF_TESTS=OFF
  • /home/xxx/milk-v/CV180x/OpenCV: Path to the extracted OpenCV source code files.
  • -DCMAKE_TOOLCHAIN_FILE="/home/yxm/milk-v/CV180x/OpenCV/platforms/linux/riscv64-musl-gcc.toolchain.cmake": Replace this with your actual path.
  • -DCMAKE_INSTALL_PREFIX=./c906_install: Set the path where make install will install the files.

After running the cmake command successfully, it will generate the necessary configuration files for compilation.
image

Now, compile OpenCV with the following command (adjust the number after -j based on your machine’s capabilities):

make -j4

This will take some time to complete, depending on your system.
image

V. Installation

After compilation, you can install OpenCV by running:

make install

You can find the installed files in the parent directory by running:

cd ..

In the build directory, you’ll see a c906_install/ directory containing the compiled OpenCV files.
image

image
image

VI. Testing

To test the OpenCV installation, create a test program to convert an image to grayscale. Create a main.cpp file with the following content:

#include <ctime>
#include <iostream>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc/types_c.h>
using namespace cv;

int main(int argc, char **argv) {
    Mat image;
    image = imread("./demo.jpg");
    if (!image.data) {
        printf("No image data (./demo.jpg)\n");
        return -1;
    } else {
        printf("Read image successfully!\n");
    }

    Mat dst;
    cvtColor(image, dst, CV_BGR2GRAY);
    imwrite("image.jpg", dst);
    printf("Conversion successful!\n");
    return 0;
}

Create a CMakeLists.txt file to build the project using CMake:

cmake_minimum_required(VERSION 3.16.5)

project(opencv_test_demo)

set(CMAKE_CROSSCOMPILING TRUE)

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR riscv64)

set(CMAKE_CXX_STANDARD 11)

# Uncomment and set TOOLCHAIN_DIR and the compiler paths if you didn't configure the environment.
# set(TOOLCHAIN_DIR "/home/yxm/milk-v/CV180x/host-tools/gcc/riscv64-linux-x86_64/bin")
# set(CMAKE_C_COMPILER "${TOOLCHAIN_DIR}/riscv64-unknown-linux-musl-gcc")
# set(CMAKE_CXX_COMPILER "${TOOLCHAIN_DIR}/riscv

64-unknown-linux-musl-g++")

# Set the compiler paths if you configured the environment.
set(CMAKE_C_COMPILER "riscv64-unknown-linux-musl-gcc")
set(CMAKE_CXX_COMPILER "riscv64-unknown-linux-musl-g++")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=rv64imafdcvxthead -mcmodel=medany -mabi=lp64d")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=rv64imafdcvxthead -mcmodel=medany -mabi=lp64d")

# Set the path to the installed OpenCV library on your machine.
set(OPENCV_PATH /home/yxm/milk-v/CV180x/OpenCV_build/c906_install)

include_directories(${OPENCV_PATH}/include/opencv4)
link_directories(${OPENCV_PATH}/lib)
set(OPENCV_LIBS ${OPENCV_LIBS} opencv_core opencv_imgcodecs opencv_imgproc)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} ${OPENCV_LIBS})

Compile your project using the cross-compilation toolchain:

mkdir build
cd build
cmake .. -DCMAKE_C_COMPILER=riscv64-unknown-linux-musl-gcc -DCMAKE_CXX_COMPILER=riscv64-unknown-linux-musl-g++
make

The target files compiled by opencv_test_demo.

Copy all the .so files generated in the previous compilation from the OpenCV c906_install/lib directory to the /usr/lib directory of the Duo file system. Then, import the opencv_test_demo program to the target board and run it to achieve grayscale conversion of the demo.jpg image.

image

image

V. Package Installation and Development Environment

We provide a package for easy setup, which can be used as mentioned above.

image

Since the forum does not support file uploads, the package has been uploaded to the official enterprise chat group.

VI. Troubleshooting

image

If you encounter issues when running the demo, please check the environment on your build system and the target board.

image

image

Ensure that the environment on your board matches your compilation environment. If there are any discrepancies, you may need to create a symbolic link to address the issue. For example, you can create a symlink for the missing library as follows:

ln -s /usr/lib64v0p7_xthead/lp64d/libc.so /lib/ld-musl-riscv64v_xthead.so.1

After addressing any environment mismatches and creating any necessary symbolic links, try running the demo again.

image

Run the demo once more.

image