ffmpeg AVFrame到OpenCV Mat的转换 [英] ffmpeg AVFrame to opencv Mat conversion

查看:480
本文介绍了ffmpeg AVFrame到OpenCV Mat的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用ffmepg解码接收到的帧的项目中,解码后,我想将AVFrame转换为opencv Mat帧,以便可以在imShow函数上播放它.

I am currently work on a project which decode the received frame using ffmepg, after decode, I want to convert the AVFrame to opencv Mat frame so that I can play it on the imShow function.

我拥有的是字节流,我将其读入缓冲区,解码为AVFrame:

What I have is the byte stream, I read it into buffer, decoded to AVFrame:

f = fopen(filename, "rb");
if (!f) {
    fprintf(stderr, "Could not open %s\n", filename);
    exit(1);
}

frame = avcodec_alloc_frame();
if (!frame) {
    fprintf(stderr, "Could not allocate video frame\n");
    exit(1);
}

framergb = avcodec_alloc_frame();
if (!framergb) {
    fprintf(stderr, "Could not allocate video frame\n");
    exit(1);
}

bytes=avpicture_get_size(PIX_FMT_RGB24, CAMER_WIDTH, CAMER_HEIGHT);
buffer=(uint8_t *)av_malloc(bytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)framergb, buffer, PIX_FMT_RGB24,
                CAMER_WIDTH, CAMER_HEIGHT);

frame_count = 0;
for(;;) {
    avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
    if (avpkt.size == 0)
        break;

    avpkt.data = inbuf;
    while (avpkt.size > 0)
        if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
            exit(1);
}

avpkt.data = NULL;
avpkt.size = 0;
decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);

decode_write_frame 定义如下:

static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,AVFrame *frame, int *frame_count, AVPacket *pkt, int last)
{
int len, got_frame;
char buf[1024];
struct SwsContext *convert_ctx;

len = avcodec_decode_video2(avctx, frame, &got_frame, pkt);
if (len < 0) {
    fprintf(stderr, "Error while decoding frame %d\n", *frame_count);
    return len;
}
if (got_frame) {
    printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);
    fflush(stdout);

int w = avctx->width;
int h = avctx->height;
convert_ctx = sws_getContext(w, h, avctx->pix_fmt,
                    w, h, PIX_FMT_RGB24, SWS_BICUBIC,
                    NULL, NULL, NULL);

if(convert_ctx == NULL) {
    fprintf(stderr, "Cannot initialize the conversion context!\n");
    exit(1);
}

sws_scale(convert_ctx, frame->data,
            frame->linesize, 0,
            h,
            framergb->data, framergb->linesize);

    /* the picture is allocated by the decoder, no need to free it */
    snprintf(buf, sizeof(buf), outfilename, *frame_count);

    bmp_save(framergb->data[0], framergb->linesize[0],
             avctx->width, avctx->height, buf);
    (*frame_count)++;
}
if (pkt->data) {
    pkt->size -= len;
    pkt->data += len;
}
return 0;
}

此处 bmp_save()由原始代码作者定义,以实现从AVFrame到bmp图片的转换.我想在这里进行修改,以便让AVFrame转换为opencv Mat框架.我应该如何进行转换?

here the bmp_save() is defined by the original code author to realise AVFrame to bmp picture conversion. I want to modify here so that let the AVFrame convert to opencv Mat frame. How should I do this conversion?

谢谢.

推荐答案

使用适当的

Using the appropriate Mat constructor, replace the bmp_save line by:

Mat mat(avctx->height, avctx->width, CV_8UC3, framergb->data[0], framergb->linesize[0]);
imshow("frame", mat);
waitKey(10);

也将sws_getContext中的PIX_FMT_RGB24标志替换为PIX_FMT_BGR24,因为OpenCV在内部使用BGR格式.

Also replace the PIX_FMT_RGB24 flag in sws_getContext by PIX_FMT_BGR24, because OpenCV use BGR format internally.

这篇关于ffmpeg AVFrame到OpenCV Mat的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆