一.视频中添加 LOGO 图像大体流程

二.代码具体实现
// 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 //#include #include #include #include #include #include #include #include #include #include #include // #include "common/sample_common.h"#include "rkmedia_api.h" #include // #include #include #include 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 //opencv的Logo处理VI线程void *opencv_vi_logo_handle_thread(void *args){ pthread_detach(pthread_self()); MEDIA_BUFFER mb = NULL; Mat logo_img = imread("zjl.jpg");//读取LOGO图片编码Mat矩阵 cvtColor(logo_img, logo_img, COLOR_RGB2GRAY); //cvtColor把彩色图像转换成灰度图 while (1) { mb = RK_MPI_SYS_GetMediaBuffer(RK_ID_VI, CAMERA_CHN, -1);//获取VI模块的数据 if (!mb) { printf("Get vi break....\n"); break; } printf("Get vi success...\n"); Mat rv1126_img_mat = Mat(HEIGHT, WIDTH, CV_8UC1, RK_MPI_MB_GetPtr(mb));//把VI数据转换成Mat矩阵 Mat rv1126_img_mat_roi = rv1126_img_mat(Rect(100, 100, logo_img.cols, logo_img.rows));//在Mat矩阵里面获取感兴趣区域 logo_img.copyTo(rv1126_img_mat_roi);//把Logo的矩阵拷贝到可感兴趣区域 RK_MPI_SYS_SendMediaBuffer(RK_ID_VENC, VENC_CHN, mb);//把处理后的VI数据传输给VENC编码器 RK_MPI_MB_ReleaseBuffer(mb);//释放资源 } return NULL;} //专门获取上面处理好的数据并保存到文件里void *get_venc_stream_thread(void *args){ pthread_detach(pthread_self()); MEDIA_BUFFER mb = NULL; FILE * h264_opencv_logo_file = fopen("test_opencv_logo.h264", "w+"); // while (1) { mb = RK_MPI_SYS_GetMediaBuffer(RK_ID_VENC, VENC_CHN, -1);//获取VENC编码器数据 if(!mb) { printf("Get venc break.....\n"); break; } fwrite(RK_MPI_MB_GetPtr(mb), RK_MPI_MB_GetSize(mb), 1 , h264_opencv_logo_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 = WIDTH; // Width vi_chn_attr.u32Height = HEIGHT; // 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 = WIDTH; venc_chn_attr.stVencAttr.u32PicHeight = HEIGHT; venc_chn_attr.stVencAttr.u32VirWidth = WIDTH; venc_chn_attr.stVencAttr.u32VirHeight = HEIGHT; 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 = WIDTH * HEIGHT * 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 failed....\n"); } else { printf("start vi success....\n"); } pthread_t pid1, pid2; pthread_create(&pid1, NULL, opencv_vi_logo_handle_thread, NULL); pthread_create(&pid2, NULL, get_venc_stream_thread, NULL); while (1) { sleep(2); } RK_MPI_VENC_DestroyChn(VENC_CHN); RK_MPI_VI_DisableChn(CAMERA_ID, CAMERA_CHN); return 0;}
三.效果图
