Toybrick

linux-rga 图像合成请教

小旺的尾巴

注册会员

积分
82
楼主
发表于 2021-8-9 17:32:57    查看: 4897|回复: 0 | [复制链接]    打印 | 只看该作者
本帖最后由 小旺的尾巴 于 2021-8-10 10:09 编辑

(哪位大佬给提供一个RGA合成两张图片的demo呀, 万分感谢。)

下面是我写的一个demo, 输入两张brga的图像后,通过 imcomposite 合成出来的图像 总是第一张图,不知道什么原因,请大佬指点一下


int test_fuse(const char *img_bgr_path, const char *img_bgr2_path)
{
    //1,两张图像
    struct timeval start_time, stop_time;
    gettimeofday(&start_time, NULL);

    Mat img_bgr = cv::imread(img_bgr_path, -1);
    Mat img_bgr2 = cv::imread(img_bgr2_path, -1);
    if(!img_bgr.data || !img_bgr2.data)
    {
        printf("cv imread error.\n");
        return -1;
    }

    //2.1,拷贝图像数据到 media buffer
    unsigned int img_bgr_h = img_bgr.rows;
    unsigned int img_bgr_w = img_bgr.cols;
    unsigned int img_bgr_c = img_bgr.channels();
    unsigned int img_bgr2_h = img_bgr2.rows;
    unsigned int img_bgr2_w = img_bgr2.cols;
    unsigned int img_bgr2_c = img_bgr2.channels();
    printf(" --- %d, %d, %d; %d, %d, %d\n", img_bgr_h, img_bgr_w, img_bgr_c, img_bgr2_h, img_bgr2_w, img_bgr2_c);
    if(img_bgr_h != img_bgr2_h || img_bgr_w != img_bgr2_w || img_bgr_c != img_bgr2_c)
    {
        printf("image size not equal.\n");
        return -1;
    }

    RK_U32 u32FrameSize = img_bgr_h * img_bgr_w * img_bgr_c;
    printf("img_bgr_w:%d, img_bgr_h:%d, u32FrameSize: %d\n", img_bgr_w, img_bgr_h, u32FrameSize);

    MB_IMAGE_INFO_S mb_img_info = {img_bgr_w, img_bgr_h, img_bgr_w, img_bgr_h, IMAGE_TYPE_ABGR8888};
    MEDIA_BUFFER img_bgr_mb = RK_MPI_MB_CreateImageBuffer(&mb_img_info, RK_TRUE, 0);
    if (!img_bgr_mb)
    {
        printf("ERROR: rgb no space left!\n");
        return -1;
    }
    memcpy(RK_MPI_MB_GetPtr(img_bgr_mb), (void*)img_bgr.data, u32FrameSize);
    RK_MPI_MB_SetSize(img_bgr_mb, u32FrameSize);

    MEDIA_BUFFER img_bgr2_mb = RK_MPI_MB_CreateImageBuffer(&mb_img_info, RK_TRUE, 0);
    if (!img_bgr2_mb)
    {
        printf("ERROR: bgr2 no space left!\n");
        return -1;
    }
    memcpy(RK_MPI_MB_GetPtr(img_bgr2_mb), (void*)img_bgr2.data, u32FrameSize);
    RK_MPI_MB_SetSize(img_bgr2_mb, u32FrameSize);

    //check
    RK_U32 u32MbFrameSize = RK_MPI_MB_GetSize(img_bgr2_mb);
    printf("check u32MbFrameSize:%u\n", u32MbFrameSize);

    //2.2, 图像数据转为 rga buffer 格式, 进行resize
    rga_buffer_t rga_buf_bgr = wrapbuffer_fd(RK_MPI_MB_GetFD(img_bgr_mb), img_bgr_w, img_bgr_h, RK_FORMAT_BGRA_8888);
    rga_buffer_t rga_buf_bgr2 = wrapbuffer_fd(RK_MPI_MB_GetFD(img_bgr2_mb), img_bgr2_w, img_bgr2_h, RK_FORMAT_BGRA_8888);

    MEDIA_BUFFER img_rs_mb = RK_MPI_MB_CreateImageBuffer(&mb_img_info, RK_TRUE, 0);
    if (!img_rs_mb)
    {
        printf("ERROR: fuse no space left!\n");
        return -1;
    }
    rga_buffer_t rga_buf_rs = wrapbuffer_fd(RK_MPI_MB_GetFD(img_rs_mb), img_bgr_w, img_bgr_h, RK_FORMAT_BGRA_8888);

  //2.3 缩放bgr图
  int left_top_flag = 1;
  if(left_top_flag)
  {
    IM_STATUS STATUS = imresize(rga_buf_bgr, rga_buf_rs, 0.5, 0.5);
    printf("resizing .... %s\n", imStrError(STATUS));
  }
  else
  {
    rga_buf_rs.wstride = (int)(0.5 * rga_buf_bgr.width);
    rga_buf_rs.hstride = (int)(0.5 * rga_buf_bgr.height);
    IM_STATUS STATUS = imresize(rga_buf_bgr, rga_buf_rs, 0.5, 0.5);
    printf("resizing .... %s\n", imStrError(STATUS));
  }

    //2.4保存cv Mat缩放图片
    Mat img_rs;
  if(left_top_flag)
  {
      img_rs = Mat(img_bgr_h, img_bgr_w, CV_8UC4, (unsigned char *)RK_MPI_MB_GetPtr(img_rs_mb));
  }
  else
  {
    img_rs = Mat((int)(img_bgr_h/2), (int)(img_bgr_w/2), CV_8UC4, (unsigned char *)RK_MPI_MB_GetPtr(img_rs_mb));
  }
    cv::imwrite("img_rs_bgr0_5lt.png", img_rs);

    //3.1图像融合: 缩放后的bgra与bgr2图融合
  MEDIA_BUFFER img_fuse_mb = RK_MPI_MB_CreateImageBuffer(&mb_img_info, RK_TRUE, 0);
    if (!img_fuse_mb)
    {
        printf("ERROR: fuse no space left!\n");
        return -1;
    }
    rga_buffer_t rga_buf_fuse = wrapbuffer_fd(RK_MPI_MB_GetFD(img_fuse_mb), img_bgr_w, img_bgr_h, RK_FORMAT_BGRA_8888);

    // rga_buf_bgr2.global_alpha = 0x30;
    rga_buf_rs.global_alpha = 0x30;
    imcomposite(rga_buf_rs, rga_buf_bgr2, rga_buf_fuse, IM_ALPHA_BLEND_DST_OVER, 1);
    // imblend(rga_buf_rs, rga_buf_bgr2, IM_ALPHA_BLEND_SRC, 1);
    Mat img_fuse_s = Mat(img_bgr_h, img_bgr_w, CV_8UC4, (unsigned char *)RK_MPI_MB_GetPtr(img_fuse_mb));
    cv::imwrite("img_fuse_s.png", img_fuse_s);
    Mat img_bgr2_s = Mat(img_bgr_h, img_bgr_w, CV_8UC4, (unsigned char *)RK_MPI_MB_GetPtr(img_bgr2_mb));
    cv::imwrite("img_bgr2_s.png", img_bgr2_s);

    gettimeofday(&stop_time, NULL);
    printf("rga fuse use %f ms\n", (__get_us(stop_time) - __get_us(start_time)) / 1000);

    //4, release资源
    RK_MPI_MB_ReleaseBuffer(img_bgr_mb);
    RK_MPI_MB_ReleaseBuffer(img_bgr2_mb);
    RK_MPI_MB_ReleaseBuffer(img_rs_mb);
    RK_MPI_MB_ReleaseBuffer(img_fuse_mb);

    return 0;
}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

产品中心 购买渠道 开源社区 Wiki教程 资料下载 关于Toybrick


快速回复 返回顶部 返回列表