rv1126_rv1109移植opencv with ffmpeg for rtsp
一、编译环境和源码包
宿主机:Ubuntu18.04 x64
目标平台:rv1126_rv1109,arm
交叉编译工具链:arm-linux-gnueabihf
依赖关系:
opencv(3.4.12)
|————zlib(1.2.11)
|--------ffmpeg(4.1.3)
|--------libx264 (20181217 (2245-stable))
|--------libxvidcore(1.3.3)
二、前期准备
配置开发机环境变量:
(1)sudo su
(2)vim /etc/profile
在最后一行加上以下命令
export PATH=/home/xx/work/rkSDk/rv1126_rv1109_v2.2.2/buildroot/output/rockchip_rv1126_rv1109/host/bin:$PATH
export OPENCVDIR=/opt/arm_opencv
export PATH=$OPENCVDIR/rk_opencv_depend/bin:$PATH
export LD_LIBRARY_PATH=$OPENCVDIR/rk_opencv_depend/lib:$LD_LIBRARY_PATH
export PATH=$OPENCVDIR/install/bin:$PATH
export LD_LIBRARY_PATH=$OPENCVDIR/install/lib:$LD_LIBRARY_PATH
三、源码包的交叉编译
1.zlib交叉编译
(1)./configure —prefix=$OPENCVDIR/rk_opencv_depend //指定安装目录
(2)对Makefile文件进行修改,修改内容如下:
CC=arm-linux-gnueabihf-gcc
LDSHARED=arm-linux-gnueabihf-gcc -shared -Wl,-soname,libz.so.1,—version-script,zlib.map
AR=arm-linux-gnueabihf-ar
RANLIB=arm-linux-gnueabihf-ranlib
(3)make
(4)make install
2.libx264交叉编译
(1)./configure —prefix=$OPENCVDIR/rk_opencv_depend —enable-shared —disable-asm —host=arm-linux
(2)打开config.mak进行修改:
CC=arm-linux-gnueabihf-gcc
LD=arm-linux-gnueabihf-gcc –o
LDSHARED=arm-linux-gnueabihf-gcc -shared -Wl,-soname,libz.so.1,—version-script,zlib.map
AR=arm-linux-gnueabihf-ar
RANLIB=arm-linux-gnueabihf-ranlib
(3)make
(4)make install
//make install-lib-static 编译成静态库
3.libxvidcore交叉编译
(1)cd xvidcore-1.3.3/build/generic
(2)直接执行以下命令
CC=arm-linux-gnueabihf-gcc ./configure —prefix=$OPENCVDIR/rk_opencv_depend —host=arm-linux —disable-assembly
(3)make
(4)make install
4.ffmpeg交叉编译
1.编写启动脚本autoffmpegconfig.sh
#!/bin/bash
./configure --prefix=$OPENCVDIR/rk_opencv_depend \
--disable-asm \
--disable-x86asm \
--enable-protocol=tcp \
--enable-network \
--enable-protocol=udp \
--enable-demuxer=rtsp \
--enable-demuxer=rtp \
--disable-doc \
--enable-shared \
--enable-gpl \
--enable-cross-compile \
--arch=arm \
--disable-stripping \
--target-os=linux \
--enable-libx264 \
--enable-libxvid \
--enable-avresample \
--enable-gpl \
--enable-small \
--disable-ffprobe \
--disable-ffplay \
--disable-debug \
--cc=arm-linux-gnueabihf-gcc \
--enable-swscale \
--extra-ldflags=-L/opt/arm_opencv/rk_opencv_depend/lib \
--extra-cflags=-I/opt/arm_opencv/rk_opencv_depend/include \
--ar=arm-linux-gnueabihf-ar \
--ranlib=arm-linux-gnueabihf-ranlib
2../autoffmpegconfig.sh
3.make
4.make install
5.opencv交叉编译
1.mkdir release
2.cd release
3.编写cmake文件toolchain.cmake
###########user defined#############
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
set(OPENCV_ENABLE_PKG_CONFIG ON)
###########user defined#############
set(CMAKE_FIND_ROOT_PATH "/opt/arm_opencv/rk_opencv_depend")
set(FFMPEG_INCLUDE_DIR "/opt/arm_opencv/rk_opencv_depend/include")
set(FFMPEG_LIB_DIR "/opt/arm_opencv/rk_opencv_depend/lib")
set(CMAKE_C_FLAGS "-Wl,-rpath-link=/opt/arm_opencv/rk_opencv_depend/lib")
set(CMAKE_CXX_FLAGS "-Wl,-rpath-link=/opt/arm_opencv/rk_opencv_depend/lib")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
4.编写启动脚本autoopencvconfig.sh
#!/bin/bash
cmake -D CMAKE_TOOLCHAIN_FILE=toolchain.cmake \
-D BUILD_opencv_python3=yes \
-D BUILD_opencv_python2=no \
-D PYTHON3_EXECUTABLE=/usr/bin/python3.7m \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.7m \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.7m.so \
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.7m \
-D CMAKE_BUILD_TYPE=Release \
-D PKG_CONFIG_FOUND=ON \
-D CMAKE_INSTALL_PREFIX=/opt/arm_opencv/install \
-D BUILD_ZLIB=ON \
-D BUILD_JPEG=ON \
-D BUILD_PNG=ON \
-D BUILD_TIFF=ON \
-D ENABLE_CXX11=ON \
-D WITH_FFMPEG=ON \
-D WITH_OPENCL=ON \
-D WITH_GTK=OFF \
-D WITH_OPENGL=ON \
-D WITH_OPENMP=ON \
-D WITH_PNG=ON ..
5.export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/arm_opencv/rk_opencv_depend/lib/pkgconfig
6../autoopencvconfig.sh
当FFMPEG项为YES时继续执行以下命令:
- make
- make install
四、针对ffmpeg始终为NO的解决方法
1.修改cmake/OpenCVFindLibsVideo.cmake,找到关于ffmpeg的段落进行如下修改
# --- FFMPEG ---
ocv_clear_vars(HAVE_FFMPEG)
if(WITH_FFMPEG) # try FFmpeg autodetection
if(OPENCV_FFMPEG_USE_FIND_PACKAGE)
if(OPENCV_FFMPEG_USE_FIND_PACKAGE STREQUAL "1" OR OPENCV_FFMPEG_USE_FIND_PACKAGE STREQUAL "ON")
set(OPENCV_FFMPEG_USE_FIND_PACKAGE "FFMPEG")
endif()
find_package(${OPENCV_FFMPEG_USE_FIND_PACKAGE}) # Required components: AVCODEC AVFORMAT AVUTIL SWSCALE
if(FFMPEG_FOUND OR FFmpeg_FOUND)
set(HAVE_FFMPEG TRUE)
else()
message(STATUS "Can't find FFmpeg via find_package(${OPENCV_FFMPEG_USE_FIND_PACKAGE})")
endif()
elseif(WIN32 AND NOT ARM AND NOT OPENCV_FFMPEG_SKIP_DOWNLOAD)
include("${OpenCV_SOURCE_DIR}/3rdparty/ffmpeg/ffmpeg.cmake")
download_win_ffmpeg(FFMPEG_CMAKE_SCRIPT)
if(FFMPEG_CMAKE_SCRIPT)
set(HAVE_FFMPEG TRUE)
set(HAVE_FFMPEG_WRAPPER 1)
include("${FFMPEG_CMAKE_SCRIPT}")
endif()
elseif(PKG_CONFIG_FOUND)
set(HAVE_FFMPEG TRUE)
ocv_check_modules(FFMPEG libavcodec libavformat libavutil libswscale)
ocv_check_modules(FFMPEG_libavresample libavresample)
if(FFMPEG_libavresample_FOUND)
ocv_append_build_options(FFMPEG FFMPEG_libavresample)
endif()
else()
message(STATUS "Can't find ffmpeg - 'pkg-config' utility is missing")
endif()
endif()
if(HAVE_FFMPEG
AND NOT HAVE_FFMPEG_WRAPPER
)
set(HAVE_FFMPEG TRUE)
set(FFMPEG_INCLUDE_DIRS /opt/arm_opencv/rk_opencv_depend/include)
set(FFMPEG_LIBRARY_DIRS /opt/arm_opencv/rk_opencv_depend/lib)
set(FFMPEG_LIBRARIES avcodec avformat avutil swscale z)
message(STATUS "FFMPEG_INCLUDE_DIR: ${FFMPEG_INCLUDE_DIRS}")
message(STATUS "FFMPEG_LIBRARY_DIRS: ${FFMPEG_LIBRARY_DIRS}")
message(STATUS "FFMPEG_LIBRARIES: ${FFMPEG_LIBRARIES}")
try_compile(__VALID_FFMPEG
"${OpenCV_BINARY_DIR}"
"${OpenCV_SOURCE_DIR}/cmake/checks/ffmpeg_test.cpp"
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${FFMPEG_INCLUDE_DIRS}"
"-DLINK_DIRECTORIES:STRING=${FFMPEG_LIBRARY_DIRS}"
"-DLINK_LIBRARIES:STRING=${FFMPEG_LIBRARIES}"
OUTPUT_VARIABLE TRY_OUT
)
if(False)
#message(FATAL_ERROR "FFMPEG: test check build log:\n${TRY_OUT}")
message(STATUS "WARNING: Can't build ffmpeg test code")
set(HAVE_FFMPEG FALSE)
else()
ocv_append_build_options(VIDEOIO FFMPEG)
endif()
endif()
2.修改CMakeVars.txt,找到关于FFMPEG_INCLUDE_DIR=的段落进行修改
FFMPEG_INCLUDE_DIR=/opt/arm_opencv/rk_opencv_depend/include
FFMPEG_INCLUDE_DIRS=/opt/arm_opencv/rk_opencv_depend/include
五、测试代码
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
string video_address = "rtsp://admin:admin@192.168.8.189";
Mat frame;
VideoCapture cap;
int ret;
ret = cap.open(video_address, cv::CAP_FFMPEG);
if (ret == 0){
cerr << "ERROR! Unable to open camera!\n";
return -1;
}
cout << "successfully opened camera ! " << endl;
cap.read(frame);
if (!frame.empty()){
cout << "pic size:" << frame.channels() << "*" << frame.cols << "*" << frame.rows << endl;
}
string image_address = "./new_image.jpg";
imwrite(image_address, frame);
cap.release();
return 0;
}
六、注意事项
板端的目录结构和电脑端的目录结构尽量保持一致
- 分享
- 举报
-
浏览量:2764次2023-04-24 14:50:18
-
2023-11-30 10:06:37
-
浏览量:1236次2023-08-31 16:23:55
-
浏览量:4706次2023-03-24 22:56:50
-
浏览量:3942次2021-06-18 16:05:42
-
浏览量:7198次2020-11-17 15:46:28
-
浏览量:1591次2023-12-29 17:51:55
-
浏览量:1211次2023-08-31 16:29:15
-
浏览量:950次2024-01-23 11:28:32
-
浏览量:1184次2024-01-09 17:35:19
-
浏览量:6187次2022-10-24 14:44:33
-
浏览量:2266次2023-01-31 22:36:19
-
浏览量:6033次2022-05-11 15:14:51
-
浏览量:2454次2023-11-29 08:59:50
-
浏览量:6921次2021-04-01 17:04:01
-
浏览量:959次2023-10-26 10:43:59
-
浏览量:7154次2021-12-03 17:12:00
-
浏览量:1266次2024-05-24 17:11:01
-
浏览量:971次2023-11-30 11:48:53
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
hury
感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明