本帖最后由 毛巾卷 于 2023-8-16 21:59 编辑
试试交叉编译opencv4.8.0到板子上玩玩。
首先是要按照官方的文档先配好SDK环境,这里就不再赘述。
把源码拉到自己的交叉编译主机上,开始进行操作,首先需要修改cmake的配置,我们打开 opencv-4.8.0/platforms/linux/aarch64-gnu.toolchain.cmake 下的文件,进行如下修改。 接下来进行cmake编译,有些路径需要根据自己的实际安装路径进行适当的修改 - cmake .. -DCMAKE_TOOLCHAIN_FILE=/home/cz/cyy/YG2LX/opencv-4.8.0/platforms/linux/aarch64-gnu.toolchain.cmake -DCMAKE_INSTALL_PREFIX=/home/cz/cyy/YG2LX/openCV_install
复制代码
至此就安装好了,我们只需要把交叉编译完成后的lib (不改变环境变量的情况放到 /lib64 目录)放到板子上即可在板子上使用opencv了,交叉主机端编译的时候就需要指定到lib和include即可。
编写一个简单的程序进行测试。
CMakeLists.txt
- cmake_minimum_required(VERSION 3.16.5)
- project(opencv_test_demo)
- set(CMAKE_CROSSCOMPILING TRUE)
- set(CMAKE_SYSTEM_NAME Linux)
- set(CMAKE_SYSTEM_PROCESSOR aarch64)
- set(CMAKE_CXX_STANDARD 11)
- #需要配置环境
- set(CMAKE_C_COMPILER "aarch64-poky-gcc")
- set(CMAKE_CXX_COMPILER "aarch64-poky-linux-g++")
- #set(CMAKE_C_COMPILER_FORCED TRUE)
- #set(CMAKE_CXX_COMPILER_FORCED TRUE)
- set(CMAKE_C_FLAGS "-mtune=cortex-a55 -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/cz/cyy/YG2LX/poky/3.1.20/sysroots/aarch64-poky-linux")
- set(CMAKE_CXX_FLAGS "-mtune=cortex-a55 -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/cz/cyy/YG2LX/poky/3.1.20/sysroots/aarch64-poky-linux")
- #本机实际交叉编译出来的opencv库的路径
- set(OPENCV_PATH /home/cz/cyy/YG2LX/openCV_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})
复制代码
main.cpp
- #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 succes!");
- }
- Mat dst;
- cvtColor(image, dst, CV_BGR2GRAY);
- imwrite("image.jpg", dst);
- printf("change succes!\n");
- return 0;
- }
复制代码
cmake编译完后再放到板子上执行即可验证是否正常。
ok,收工。
|