Opencv 加载ffmpeg 视频帧
#概要
avframe->data = rgbBuffer = img_1->data;
#AVframe 转换
//AVframe 初始化 YUV需要转为BGR24
pFrameRGB = av_frame_alloc();
rgbNumBytes = av_image_get_buffer_size(AV_PIX_FMT_BGR24, dec_ctx->width, dec_ctx->height, 1);
rgbBuffer = (unsigned char*)av_malloc(rgbNumBytes);
ret = av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, rgbBuffer,
AV_PIX_FMT_BGR24, dec_ctx->width, dec_ctx->height, 1);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "av_image_fill_arrays failed, error num is %d\n", ret);
goto failure;
}
pImgToRgbConvertCtx = sws_getContext(dec_ctx->width, dec_ctx->height,
dec_ctx->pix_fmt, dec_ctx->width, dec_ctx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
if (!pImgToRgbConvertCtx) {
av_log(NULL, AV_LOG_ERROR, "initting sws_ctx failed\n");
goto failure;
}
pFrameRGB->format = AV_PIX_FMT_BGR24;
pFrameRGB->width = dec_ctx->width;
pFrameRGB->height = dec_ctx->height;
#Opencv Mat 初始化方式
//方式一
img_1 = new cv::Mat();
img_1->create(cv::Size(width, height), CV_8UC3);
memcpy(img_1->data, rgbBuffer , rgbNumBytes )
//方式二
cv::Mat* bgrImg = new cv::Mat(pFrameRGB->height, pFrameRGB->width, CV_8UC3, rgbBuffer);