9.RV1126-OPENCV 视频的膨胀和腐蚀

一.膨胀

1.视频流的膨胀流程

之前膨胀都是在图片中进行的,现在要在视频中进行也简单,大概思路就是:获取VI数据,然后把VI数据给Mat化发给VENC模块,然后VENC模块获取,这样就完成了。流程图:

2.代码具体实现

复制代码
// Copyright 2020 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <opencv2/imgproc.hpp>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

// #include "common/sample_common.h"
#include "rkmedia_api.h"

#include <opencv2/core.hpp>
// #include <opencv2/imgoroc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

#define CAMERA_PATH "rkispp_scale0"
#define CAMERA_ID 0
#define CAMERA_CHN 0
#define VENC_CHN 0
#define WIDTH 1920
#define HEIGHT 1080

void *opencv_vi_dliate_thread(void *args)
{
  pthread_detach(pthread_self());
  MEDIA_BUFFER mb = NULL;

  while (1)
  {
    mb = RK_MPI_SYS_GetMediaBuffer(RK_ID_VI, CAMERA_CHN, -1);//获取VI数据
    if (!mb)
    {
      printf("Get Vi stream break...\n");
      break;
    }

    Mat rv1126_vi_mat = Mat(HEIGHT, WIDTH, CV_8UC1, RK_MPI_MB_GetPtr(mb)); //把VI数据转换成OPENCV的Mat矩阵
    Mat rv1126_vi_structure = getStructuringElement(MORPH_RECT, Size(15, 15));//获取内核,内核的形状是矩形,长度大小是15 * 15
    dilate(rv1126_vi_mat, rv1126_vi_mat, rv1126_vi_structure);//对Mat矩阵进行dilate膨胀
    RK_MPI_SYS_SendMediaBuffer(RK_ID_VENC, VENC_CHN, mb);//把膨胀后的数据传输到VENC编码器
    RK_MPI_MB_ReleaseBuffer(mb);//释放资源
  }

  return NULL;
}

void *get_venc_stream_thread(void * args)
{
  pthread_detach(pthread_self());

  MEDIA_BUFFER mb = NULL;
  FILE *opencv_dliate_file = fopen("test_opencv_dliate.h264", "w+");

  while (1)
  {

    mb = RK_MPI_SYS_GetMediaBuffer(RK_ID_VENC, VENC_CHN, -1);
    if (!mb)
    {
      printf("Get Venc stream break...\n");
      break;
    }

    printf("Get Dlite_Venc Stream Success...\n");

    fwrite(RK_MPI_MB_GetPtr(mb), RK_MPI_MB_GetSize(mb), 1, opencv_dliate_file);
    RK_MPI_MB_ReleaseBuffer(mb);
  }

  return NULL;
}

int main()
{
  int ret;
  VI_CHN_ATTR_S vi_chn_attr;
  vi_chn_attr.pcVideoNode = CAMERA_PATH;        // Path
  vi_chn_attr.u32Width = 1920;                  // Width
  vi_chn_attr.u32Height = 1080;                 // Height
  vi_chn_attr.enPixFmt = IMAGE_TYPE_NV12;       // ImageType
  vi_chn_attr.enBufType = VI_CHN_BUF_TYPE_MMAP; // BufType
  vi_chn_attr.u32BufCnt = 3;                    // Cnt
  vi_chn_attr.enWorkMode = VI_WORK_MODE_NORMAL; // Mode
  ret = RK_MPI_VI_SetChnAttr(CAMERA_ID, CAMERA_CHN, &vi_chn_attr);
  if (ret)
  {
    printf("Vi Set Attr Failed.....\n");
    return 0;
  }
  else
  {
    printf("Vi Set Attr Success.....\n");
  }

  ret = RK_MPI_VI_EnableChn(CAMERA_ID, CAMERA_CHN);
  if (ret)
  {
    printf("Vi Enable Attr Failed.....\n");
    return 0;
  }
  else
  {
    printf("Vi Enable Attr Success.....\n");
  }

  VENC_CHN_ATTR_S venc_chn_attr;
  memset(&venc_chn_attr, 0, sizeof(VENC_CHN_ATTR_S));
  venc_chn_attr.stVencAttr.u32PicWidth = 1920;
  venc_chn_attr.stVencAttr.u32PicHeight = 1080;
  venc_chn_attr.stVencAttr.u32VirWidth = 1920;
  venc_chn_attr.stVencAttr.u32VirHeight = 1080;
  venc_chn_attr.stVencAttr.imageType = IMAGE_TYPE_NV12;
  venc_chn_attr.stVencAttr.enType = RK_CODEC_TYPE_H264;
  venc_chn_attr.stVencAttr.u32Profile = 66;
  venc_chn_attr.stRcAttr.enRcMode = VENC_RC_MODE_H264CBR;
  venc_chn_attr.stRcAttr.stH264Cbr.u32Gop = 25;
  venc_chn_attr.stRcAttr.stH264Cbr.u32BitRate = 1920 * 1080 * 3;
  venc_chn_attr.stRcAttr.stH264Cbr.fr32DstFrameRateDen = 1;
  venc_chn_attr.stRcAttr.stH264Cbr.fr32DstFrameRateNum = 25;
  venc_chn_attr.stRcAttr.stH264Cbr.u32SrcFrameRateDen = 1;
  venc_chn_attr.stRcAttr.stH264Cbr.u32SrcFrameRateNum = 25;
  ret = RK_MPI_VENC_CreateChn(VENC_CHN, &venc_chn_attr);
  if (ret)
  {
    printf("ERROR: Create venc failed!\n");
    exit(0);
  }

  ret = RK_MPI_VI_StartStream(CAMERA_ID, CAMERA_CHN);
  if (ret)
  {
    printf("start vi stream failed.....\n");
  }
  else
  {
    printf("start vi stream success.....\n");
  }

  pthread_t pid;
  pthread_create(&pid, NULL, opencv_vi_dliate_thread, NULL);//膨胀处理线程
  pthread_create(&pid, NULL, get_venc_stream_thread, NULL);//获取VENC线程

  while (1)
  {
    sleep(2);
  }

  RK_MPI_VENC_DestroyChn(VENC_CHN);
  RK_MPI_VI_DisableChn(CAMERA_ID, CAMERA_CHN);


  return 0;
}

3.效果图

二.腐蚀

1.视频流的腐蚀流程

原理和膨胀一样,那我从上面再复制一下。大概思路就是:获取VI数据,然后把VI数据给Mat化发给VENC模块,然后VENC模块获取,这样就完成了。流程图:

2.代码具体实现

复制代码
// Copyright 2020 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <opencv2/imgproc.hpp>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

// #include "common/sample_common.h"
#include "rkmedia_api.h"

#include <opencv2/core.hpp>
// #include <opencv2/imgoroc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

#define CAMERA_PATH "rkispp_scale0"
#define CAMERA_ID 0
#define CAMERA_CHN 0
#define VENC_CHN 0
#define WIDTH 1920
#define HEIGHT 1080

void *opencv_vi_erode_thread(void *args)
{
  pthread_detach(pthread_self());
  MEDIA_BUFFER mb = NULL;

  while (1)
  {
    mb = RK_MPI_SYS_GetMediaBuffer(RK_ID_VI, CAMERA_CHN, -1);//获取VI数据
    if (!mb)
    {
      printf("Get Vi Stream break.....\n");
      break;
    }

    Mat rv1126_mat = Mat(HEIGHT, WIDTH, CV_8UC1, RK_MPI_MB_GetPtr(mb));//把VI数据转换成OPENCV的Mat矩阵
    Mat rv1126_structure = getStructuringElement(MORPH_RECT, Size(15, 15));//获取内核,内核的形状是矩形,长度大小是15 * 15
    erode(rv1126_mat, rv1126_mat, rv1126_structure);//对Mat矩阵进行erode腐蚀
    RK_MPI_SYS_SendMediaBuffer(RK_ID_VENC, VENC_CHN, mb);//把腐蚀后的数据传输到VENC编码器
    RK_MPI_MB_ReleaseBuffer(mb);//释放资源
  }

  return NULL;
}

void *get_venc_stream_thread(void *args)
{
  pthread_detach(pthread_self());
  MEDIA_BUFFER mb = NULL;
  FILE * opencv_erode_h264_file = fopen("opencv_erode.h264", "w+");

  while (1)
  {
    mb = RK_MPI_SYS_GetMediaBuffer(RK_ID_VENC, VENC_CHN, -1);
    if (!mb)
    {
      printf("Get Venc Stream break.....\n");
      break;
    }

    printf("Get ERODE_STREAM Success...\n");
    fwrite(RK_MPI_MB_GetPtr(mb), RK_MPI_MB_GetSize(mb), 1, opencv_erode_h264_file);
    RK_MPI_MB_ReleaseBuffer(mb);
  }

  return NULL;
}

int main()
{
  int ret;
  VI_CHN_ATTR_S vi_chn_attr;
  vi_chn_attr.pcVideoNode = CAMERA_PATH;        // Path
  vi_chn_attr.u32Width = 1920;                  // Width
  vi_chn_attr.u32Height = 1080;                 // Height
  vi_chn_attr.enPixFmt = IMAGE_TYPE_NV12;       // ImageType
  vi_chn_attr.enBufType = VI_CHN_BUF_TYPE_MMAP; // BufType
  vi_chn_attr.u32BufCnt = 3;                    // Cnt
  vi_chn_attr.enWorkMode = VI_WORK_MODE_NORMAL; // Mode
  ret = RK_MPI_VI_SetChnAttr(CAMERA_ID, CAMERA_CHN, &vi_chn_attr);
  if (ret)
  {
    printf("Vi Set Attr Failed.....\n");
    return 0;
  }
  else
  {
    printf("Vi Set Attr Success.....\n");
  }

  ret = RK_MPI_VI_EnableChn(CAMERA_ID, CAMERA_CHN);
  if (ret)
  {
    printf("Vi Enable Attr Failed.....\n");
    return 0;
  }
  else
  {
    printf("Vi Enable Attr Success.....\n");
  }

  VENC_CHN_ATTR_S venc_chn_attr;
  memset(&venc_chn_attr, 0, sizeof(VENC_CHN_ATTR_S));
  venc_chn_attr.stVencAttr.u32PicWidth = 1920;
  venc_chn_attr.stVencAttr.u32PicHeight = 1080;
  venc_chn_attr.stVencAttr.u32VirWidth = 1920;
  venc_chn_attr.stVencAttr.u32VirHeight = 1080;
  venc_chn_attr.stVencAttr.imageType = IMAGE_TYPE_NV12;
  venc_chn_attr.stVencAttr.enType = RK_CODEC_TYPE_H264;
  venc_chn_attr.stVencAttr.u32Profile = 66;
  venc_chn_attr.stRcAttr.enRcMode = VENC_RC_MODE_H264CBR;
  venc_chn_attr.stRcAttr.stH264Cbr.u32Gop = 25;
  venc_chn_attr.stRcAttr.stH264Cbr.u32BitRate = 1920 * 1080 * 3;
  venc_chn_attr.stRcAttr.stH264Cbr.fr32DstFrameRateDen = 1;
  venc_chn_attr.stRcAttr.stH264Cbr.fr32DstFrameRateNum = 25;
  venc_chn_attr.stRcAttr.stH264Cbr.u32SrcFrameRateDen = 1;
  venc_chn_attr.stRcAttr.stH264Cbr.u32SrcFrameRateNum = 25;
  ret = RK_MPI_VENC_CreateChn(VENC_CHN, &venc_chn_attr);
  if (ret)
  {
    printf("ERROR: Create venc failed!\n");
    exit(0);
  }

  ret = RK_MPI_VI_StartStream(CAMERA_ID, CAMERA_CHN);
  if (ret)
  {
    printf("ERROR: Start Vi failed.....!\n");
    return -1;
  }
  else
  {
    printf("SUCCESS: Start Vi success.....!\n");
  }

  pthread_t pid;
  pthread_create(&pid, NULL, opencv_vi_erode_thread, NULL);//腐蚀处理线程
  pthread_create(&pid, NULL, get_venc_stream_thread, NULL);//获取VENC线程

  while (1)
  {
    sleep(2);
  }

  RK_MPI_VENC_DestroyChn(VENC_CHN);
  RK_MPI_VI_DisableChn(CAMERA_ID, CAMERA_CHN);

  return 0;
}

3.效果图

相关推荐
音视频牛哥1 小时前
打通视频到AI的第一公里:轻量RTSP服务如何重塑边缘感知入口?
人工智能·计算机视觉·音视频·大牛直播sdk·机器视觉·轻量级rtsp服务·ai人工智能
Wendy14412 小时前
【灰度实验】——图像预处理(OpenCV)
人工智能·opencv·计算机视觉
中杯可乐多加冰3 小时前
五大低代码平台横向深度测评:smardaten 2.0领衔AI原型设计
人工智能
无线图像传输研究探索3 小时前
单兵图传终端:移动场景中的 “实时感知神经”
网络·人工智能·5g·无线图传·5g单兵图传
zzywxc7874 小时前
AI在编程、测试、数据分析等领域的前沿应用(技术报告)
人工智能·深度学习·机器学习·数据挖掘·数据分析·自动化·ai编程
铭keny4 小时前
YOLOv8 基于RTSP流目标检测
人工智能·yolo·目标检测
墨尘游子5 小时前
11-大语言模型—Transformer 盖楼,BERT 装修,RoBERTa 直接 “拎包入住”|预训练白话指南
人工智能·语言模型·自然语言处理
金井PRATHAMA5 小时前
主要分布于内侧内嗅皮层的层Ⅲ的网格-速度联合细胞(Grid × Speed Conjunctive Cells)对NLP中的深层语义分析的积极影响和启示
人工智能·深度学习·神经网络·机器学习·语言模型·自然语言处理·知识图谱
天道哥哥5 小时前
InsightFace(RetinaFace + ArcFace)人脸识别项目(预训练模型,鲁棒性很好)
人工智能·目标检测
幻风_huanfeng5 小时前
学习人工智能所需知识体系及路径详解
人工智能·学习