opencv版本:opencv4.2
opencv4.2下载地址:https://github.com/opencv/opencv/tree/4.2.0
riscv交叉编译工具链:riscv64-unknown-linux-gnu-gcc/g++
riscv交叉编译链地址:https://github.com/riscv/riscv-gnu-toolchain
测试环境:qemu-riscv64
qemu-riscv地址:https://github.com/riscv/riscv-qemu
# 拉取编译链代码(需要科学上网)
git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
# 安装所需依赖
sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev
# 设置环境路径 如将/opt/riscv作为工具链安装路径,将/opt/riscv/bin加入环境变量路径中
# 编译
./configure --prefix=/opt/riscv
make linux
# 安装所需依赖
sudo apt-get install gcc libc6-dev pkg-config bridge-utils uml-utilities zlib1g-dev libglib2.0-dev autoconf automake libtool libsdl1.2-dev
# 拉取代码
git clone https://github.com/riscv/riscv-qemu
cd riscv-qemu
git submodule update --init pixman
# 编译
./configure --target-list=riscv64-linux-user [--prefix=INSTALL_LOCATION]
make
创建bulid目录,打开cmake-gui设置soucecode和bulid路径
参考:嵌入式开发之Opencv(一)交叉编译
可以根据需要选择需要build的功能模块
打开Advancd选项, 我另外设置了这些选项:
CMAKE_BUILD_TYPE Release
CMAKE_EXE_LINKER_FLAGS -lrt -lpthread
CMAKE_INSTALL_PREFIX /usr/lcoal/riscv/open-riscv # 自定义安装目录(默认是/usr/local)
Configure生成之后进入bulid目录,make即可!
Configure下会报无法识别交叉工具链及riscv架构的warning,如下图所示。因为这个版本(4.2)的opencv官方还没有加入riscv的支持,无法识别属于正常。但其实不影响后续的交叉编译工作。
fatal error: zlib.h: No such file or directory
../opencv3.4.0/3rdparty/protobuf/src/google/protobuf/stubs/common.cc:51:2: error: “No suitable threading library available.”
../opencv-4.1.1/modules/imgcodecs/src/grfmt_jpeg.cpp:224:58:
error: invalid conversion from ‘uchar* {aka unsigned char*}’ to ‘const JOCTET* {aka const char*}’ [-fpermissive]
state->source.pub.next_input_byte = m_buf.ptr();
相关链接:https://github.com/opencv/opencv/issues/15308(同一个错误)
#include
#include
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/types_c.h"
using namespace cv;
//将图片灰度化并保存
int main()
{
Mat image;
image = imread("./img.jpg");
if(!image.data)
{
printf("No image data \n");
return -1;
}else{
printf("read the image!\n");
}
Mat dst;
cvtColor(image,dst,CV_BGR2GRAY);
imwrite("dst.jpg", dst);
printf("change success!\n");
return 0;
}
riscv64-unknown-linux-gnu-g++ DisplayImage.cpp -o DispalyImage -I /usr/local/riscv/opencv-riscv/include/opencv4
-L /usr/local/riscv/opencv-riscv/lib -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lz
#-I Dir 指定include目录,在该目录下寻找引用的头文件
#-L Dir 指定库文件目录,在该目录下寻找链接的库文件
编译成功在qume-riscv64上运行
qemu-riscv64 -L /opt/riscv/64/sysroot DisplayImage
fatal error: opencv2/opencv.hpp: No such file or directory
ld: cannot find -lopencv_core
ld: cannot find -lopencv_imgcodecs
...
undefined reference to `gzopen'
undefined reference to `gzclose'
undefined reference to `gzgets'
undefined reference to `gzputs'
undefined reference to symbol '_ZTVN10__cxxabiv117__class_type_infoE@@CXXABI_1.3'
DisplayImage:error while loading shared libraries: libopencv_core.so.4.2: cannot open shared object file: No such file or directory