前期准备
OpenCV库的交叉编译需要以下几个条件:
-
- Ubuntu虚拟机
- 已经编译好的riscv交叉编译工具链
- OpenCV源码
- OpenCV_contrib
- 几个依赖库下载:zlib、libjpeg、libpng、ffmpeg
zlib的下载地址是:https://zlib.net/fossils/zlib-1.2.8.tar.gz
libjpeg的地址是:http://www.ijg.org/files/jpegsrc.v9e.tar.gz
libpng的下载地址:https://sourceforge.net/projects/libpng/files/libpng16/older-releases/1.6.18/libpng-1.6.18.tar.gz/download
x264的下载地址:https://download.videolan.org/x264/snapshots/x264-snapshot-20151030-2245-stable.tar.bz2
xvidcore的下载地址:http://downloads.xvid.org/downloads/xvidcore-1.3.3.tar.gz
yasm的下载地址:http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
ffmpeg的下载地址:https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
首先进行依赖库进行逐个安装,运行命令如下:
export PATH=$PATH:/opt/riscv/bin
zlib:\./configure \-\-prefix=/home/linrui/Downloads/opencv\_sipeed/opencv\_depend –share
修改Makefile:
CC=riscv64\-unknown\-linux\-gnu\-gcc
AR=riscv64\-unknown\-linux\-gnu\-ar
RANLIB=riscv64\-unknown\-linux\-gnu\-ranlib
make & make install
LDSHARED=riscv64\-unknown\-linux\-gcc \-shared\-Wl,\-soname,libz\.so\.1,\-\-version\-script,zlib\.map
libjpeg:\./configure\-\-host=riscv64\-unknown\-linux\-gnu \-\-prefix=/home/linrui/Downloads/opencv\_sipeed/opencv\_depend \-\-enable\-shared \-\-enable\-static
make & make install
libpng:\./configure \-\-host=riscv64\-unknown\-linux\-gnu \-\-prefix=/home/linrui/Downloads/opencv\_sipeed/opencv\_depend \-\-enable\-shared \-\-enable\-static
make & make install
yasm:CC=riscv64\-unknown\-linux\-gnu\-gcc \./configure\-\-enable\-shared \-\-host=riscv64\-unknown\-linux\-gnu \-\-disable\-asm –prefix=/home/linrui/Downloads/opencv\_sipeed/opencv\_depend
make & make install
libx264:CC= riscv64\-unknown\-linux\-gnu\-gcc \./configure\-\-enable\-shared \-\-host=iscv64\-unknown\-linux\-gnu \-\-disable\-asm \-\-prefix=/home/linrui/Downloads/opencv\_sipeed/opencv\_depend
make & make install
libxvidcore:cd build/generic
\./configure \-\-prefix=/home/linrui/Downloads/opencv\_sipeed/opencv\_depend \-\-host= riscv64\-unknown\-linux\-gnu \-\-disable\-assembly
make & make install
ffmpeg:\./configure \-\-prefix=/home/linrui/Downloads/opencv\_sipeed/opencv\_depend \-\-enable\-shared \-\-disable\-static \-\-enable\-gpl \-\-enable\-cross\-compile \-\-cross\-prefix=riscv64\-unknown\-linux\-gnu\- \-\-arch=riscv \-\-disable\-stripping \-\-target\-os=linux \-\-enable\-libxvid\-\-cc=riscv64\-unknown\-linux\-gnu\-gcc \-\-enable\-swscale \-\-extra\-ldflags=\-L/home/linrui/Downloads/opencv\_sipeed/opencv\_depend/lib \-\-extra\-cflags=\-I/home/linrui/Downloads/opencv\_sipeed/opencv\_depend/include
make & make install
OpenCV交叉编译
OpenCV的源码可以在OpenCV的官网进行下载,并且下载opencv_contrib。下载好后,编写toolchain.cmake文件如下:
\#\#\#\#\#\#\#\#\#\#\#user defined\#\#\#\#\#\#\#\#\#\#\#\#\#
set\( CMAKE\_SYSTEM\_NAME Linux \)
set\( CMAKE\_SYSTEM\_PROCESSOR riscv64 \)
set\( CMAKE\_C\_COMPILER riscv64\-unknown\-linux\-gnu\-gcc \)
set\( CMAKE\_CXX\_COMPILER riscv64\-unknown\-linux\-gnu\-g\+\+ \)
\#\#\#\#\#\#\#\#\#\#\#user defined\#\#\#\#\#\#\#\#\#\#\#\#\#
set\(CMAKE\_FIND\_ROOT\_PATH "~/Documents/opencv/opencv\-depend" \)
set\( CMAKE\_FIND\_ROOT\_PATH\_MODE\_PROGRAM NEVER \)
set\( CMAKE\_FIND\_ROOT\_PATH\_MODE\_LIBRARY ONLY\)
set\( CMAKE\_FIND\_ROOT\_PATH\_MODE\_INCLUDE ONLY\)
进入到OpenCV的源码文件夹下,运行命令:
mkdir build && cd build
cmake \-DCMAKE\_TOOLCHAIN\_FILE=toolchain\.cmake \.\.
在/opt/riscv/riscv64-unknown-linux-gnu/文件夹下创建ffmpeg-config.cmake文件,ffmpeg-config.cmake文件编写如下:
set\(ffmpeg\_path "$\{CMAKE\_CURRENT\_LIST\_DIR\}"\)
message\("ffmpeg\_path: $\{ffmpeg\_path\}"\)
set\(FFMPEG\_EXEC\_DIR "$\{ffmpeg\_path\}/bin"\)
set\(FFMPEG\_LIBDIR "$\{ffmpeg\_path\}/lib"\)
set\(FFMPEG\_INCLUDE\_DIRS "$\{ffmpeg\_path\}/include"\)
\
set\(FFMPEG\_LIBRARIES
${FFMPEG_LIBDIR}/libavformat.dylib
${FFMPEG_LIBDIR}/libavdevice.dylib
${FFMPEG_LIBDIR}/libavcodec.dylib
${FFMPEG_LIBDIR}/libavutil.dylib
${FFMPEG_LIBDIR}/libswscale.dylib
${FFMPEG_LIBDIR}/libswresample.dylib
${FFMPEG_LIBDIR}/libavfilter.dylib
\)
\
set\(FFMPEG\_libavformat\_FOUND TRUE\)
set\(FFMPEG\_libavdevice\_FOUND TRUE\)
set\(FFMPEG\_libavcodec\_FOUND TRUE\)
set\(FFMPEG\_libavutil\_FOUND TRUE\)
set\(FFMPEG\_libswscale\_FOUND TRUE\)
set\(FFMPEG\_libswresample\_FOUND TRUE\)
set\(FFMPEG\_libavfilter\_FOUND TRUE\)
set\(FFMPEG\_libavresample\_FOUND FALSE\) //ffmpeg高版本已经没有libavresample了
\
set\(FFMPEG\_libavutil\_VERSION 57\.36\.101 CACHE INTERNAL "FFMPEG\_libavutil\_VERSION"\) \
set\(FFMPEG\_libavcodec\_VERSION 59\.42\.104 CACHE INTERNAL "FFMPEG\_libavcodec\_VERSION"\) \
set\(FFMPEG\_libavformat\_VERSION 59\.30\.100 CACHE INTERNAL "FFMPEG\_libavformat\_VERSION"\) \
set\(FFMPEG\_libavdevice\_VERSION 59\.8\.101 CACHE INTERNAL "FFMPEG\_libavdevice\_VERSION"\) \
set\(FFMPEG\_libavfilter\_VERSION 8\.48\.100 CACHE INTERNAL "FFMPEG\_libavfilter\_VERSION"\) \
set\(FFMPEG\_libswscale\_VERSION 6\.8\.104 CACHE INTERNAL "FFMPEG\_libswscale\_VERSION"\) \
set\(FFMPEG\_libswresample\_VERSION 4\.9\.100 CACHE INTERNAL "FFMPEG\_libswresample\_VERSION"\) \
set\(FFMPEG\_FOUND TRUE\)
set\(FFMPEG\_LIBS $\{FFMPEG\_LIBRARIES\}\)
status\(" \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\# FFMPEG:" FFMPEG\_FOUND THEN "YES \(find\_package\)" ELSE "NO \(find\_package\)"\)
status\(" avcodec:" FFMPEG\_libavcodec\_VERSION THEN "YES \($\{FFMPEG\_libavcodec\_VERSION\}\)" ELSE NO\)
status\(" avformat:" FFMPEG\_libavformat\_VERSION THEN "YES \($\{FFMPEG\_libavformat\_VERSION\}\)" ELSE NO\)
status\(" avutil:" FFMPEG\_libavutil\_VERSION THEN "YES \($\{FFMPEG\_libavutil\_VERSION\}\)" ELSE NO\)
status\(" swscale:" FFMPEG\_libswscale\_VERSION THEN "YES \($\{FFMPEG\_libswscale\_VERSION\}\)" ELSE NO\)
status\(" avresample:" FFMPEG\_libavresample\_VERSION THEN "YES \($\{FFMPEG\_libavresample\_VERSION\}\)" ELSE NO\)
打开cmake-gui,创建几个字符串:
OPENCV_FFMPEG_USE_FIND_PACKAGE(BOOL:ON):让OpenCV编译时自动寻找ffmpeg包的位置。
FFMPEG_DIR:/opt/riscv/riscv64-unknown-linux-gnu:提供ffmpeg包的位置
点击configure,然后将CMAKE选项中的ar,gcc,ld,ranlib,strip改为/opt/riscv/bin中对应的执行文件。CMAKE_EXE_LINKER_FLAGS增加字符串”-lrt -lpthread”。
打开WITH_FFMPEG,WITH_PNG,WITH_JPEG,关闭WITH_GTK,WITH_GTK_2_X(gtk编译未解决),WITH_GSTREAM。打开BUILD_SHARED_LIBRARY。
再次点击configure,generate。