rv1126_rv1109移植opencv with ffmpeg for rtsp

rv1126_rv1109移植opencv with ffmpeg for rtsp hury 2024-01-06 09:02:22 1127

一、编译环境和源码包

  1. 宿主机:Ubuntu18.04 x64

  2. 目标平台:rv1126_rv1109,arm

  3. 交叉编译工具链:arm-linux-gnueabihf

  4. 依赖关系:

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时继续执行以下命令:

  1. make
  2. 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;
}

六、注意事项

板端的目录结构和电脑端的目录结构尽量保持一致

声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
hury
红包 点赞 收藏 评论 打赏
评论
0个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
hury
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区