目录
获取app路径:
下载:OpenCV-android-sdk
cmakelist配置:
头文件路径:
编译报错:clang++: error: linker command failed with exit code 1 (use -v to see invocation)
读取图片例子
保存mp4
获取app路径:
public String getExternalPath() {File externalFilesDir = getExternalFilesDir(null);if (externalFilesDir != null) {return externalFilesDir.getAbsolutePath();} else {return null;}}
下载:OpenCV-android-sdk
下载地址:
在主页上点击github,
https://opencv.org/
https://github.com/opencv/opencv/releases
关于版本:4.9.0版本 是.a文件,在rknn下编译项目直接报错
opencv-4.8.0-android-sdk
cmakelist配置:
set(OpenCV_DIR D:/GZ/soft/rknn-toolkit2/rknn-toolkit2-master/rknpu2/examples/3rdparty/opencv/OpenCV-android-sdk/sdk/native/jni/abi-${CMAKE_ANDROID_ARCH_ABI})
find_package(OpenCV REQUIRED)
target_link_libraries( # Specifies the target library.rknn4j${OpenCV_LIBS}
so文件不用自行添加引用了。
头文件路径:
OpenCV-android-sdk\sdk\native\jni\include\opencv2
adb push frame.jpg /data/RK3588/
编译报错:clang++: error: linker command failed with exit code 1 (use -v to see invocation)
点击 Build command failed,可以查看错误详细信息
C:/Users/Administrator/AppData/Local/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin\ld: skipping incompatible D:/GZ/soft/rknn-toolkit2/rknn-toolkit2/rknpu2/examples/rknn_yolov5_android_apk_demo_o/app/src/main/jniLibs/arm64-v8a/libopencv_core.so when searching for -lopencv_core
C:/Users/Administrator/AppData/Local/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin\ld: cannot find -lopencv_core
找不到-lopencv_core
原因:在cmakelist中直接加了so库
target_link_libraries( # Specifies the target library.rknn4j
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_core.so ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_video.so ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_videoio.so
解决方法:把so去掉即可。
读取图片例子
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"char *input_path="/data/RK3588/frame.jpg";cv::Mat orig_img = imread(input_path, cv::IMREAD_COLOR);// if origin model is from Caffe, you maybe not need do BGR2RGB.cv::Mat orig_img_rgb;cv::cvtColor(orig_img, orig_img_rgb, cv::COLOR_BGR2RGBA);cv::Mat cv_img = orig_img_rgb.clone();if (orig_img.cols != model_width || orig_img.rows != model_height) {printf("resize %d %d to %d %d\n", orig_img.cols, orig_img.rows, model_width, model_height);cv::resize(orig_img_rgb, cv_img, cv::Size(model_width, model_height), 0, 0, cv::INTER_LINEAR);}
保存mp4
int fps = 25;int duration = 2; // 总时长为 2 秒cv::VideoWriter writer_top;cv::Mat img(480, 640, CV_8UC3); // 创建一个 640x480 的彩色帧const char * aaa="/storage/emulated/0/Android/data/com.rockchip.gpadc.yolodemo/files/output2.mp4";bool opened = writer_top.open(aaa, cv::VideoWriter::fourcc('H', '2', '6', '4'), fps, cv::Size(640, 480));// 创建一个 VideoWriter 对象,用来写视频if (!opened) {std::cerr << "Could not open the output video file for write\n";return -1;}// 定义每种颜色显示的帧数int frames_per_color = fps * duration / 3; // 总帧数除以颜色数(RGB三色)// 循环创建每一帧for (int i = 0; i < frames_per_color * 3; ++i) {cv::Mat frame(480, 640, CV_8UC3); // 创建一个 640x480 的彩色帧// 根据帧号选择颜色if (i < frames_per_color) {frame.setTo(cv::Scalar(0, 0, 255)); // 红色} else if (i < 2 * frames_per_color) {frame.setTo(cv::Scalar(0, 255, 0)); // 绿色} else {frame.setTo(cv::Scalar(255, 0, 0)); // 蓝色}writer_top.write(frame); // 写帧到视频}// 释放资源writer_top.release();
在安卓手机上运行成功
在rkn3588上测试失败