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

发布于:2025-06-08 ⋅ 阅读:(18) ⋅ 点赞:(0)

一.膨胀

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.效果图


网站公告

今日签到

点亮在社区的每一天
去签到