【问题标题】:opencv Mat convert to AVFrameopencv Mat 转换为 AVFrame
【发布时间】:2014-08-03 05:06:24
【问题描述】:

我在 OpenCV 中有一个可以正确查看和保存的图像,我想获取这个图像并将其传递给 FFMPEG,以便我可以对其进行编码,但是当我从 ffpmeg 保存 jpg 输出时,我得到一个空图像,它可能意味着我没有将数据正确复制到 AVFrame。

我做错了什么...

非常感谢任何帮助。

这就是我设置final_frame和yuv422帧的方式

final_frame = avcodec_alloc_frame();
int num_bytes = avpicture_get_size(PIX_FMT_BGR24, 1600, 720);
final_frame1_buffer = (uint8_t *)av_malloc(num_bytes*sizeof(uint8_t));
avpicture_fill((AVPicture*)final_frame, final_frame1_buffer, PIX_FMT_BGR24, 1600, 720);

yuv422_final_frame = avcodec_alloc_frame();
int yuv422_num_bytes = avpicture_get_size( PIX_FMT_YUV422P, 1600, 720 );
final_frame2_buffer = (uint8_t *)av_malloc(yuv422_num_bytes*sizeof(uint8_t));
avpicture_fill((AVPicture*)yuv422_final_frame, final_frame2_buffer, PIX_FMT_YUVJ422P, 1600, 720);

我已经附上了下面的代码

cv::imshow("output image", im3);   <---------- Image shows correctly
cv::Mat rgb_frame;
cv::cvtColor( im3 , rgb_frame, CV_BGR2RGB ) ;
if (final_sws_ctx == NULL)
{
    final_sws_ctx = sws_getContext(1600, 720,
    AV_PIX_FMT_BGR24, 1600, 720,
    AV_PIX_FMT_YUVJ422P, SWS_FAST_BILINEAR, 0, 0, 0);
}

imwrite( "rgbjpeg.jpg", rgb_frame );  <----- Image Saves correctly here too

avpicture_fill((AVPicture*)final_frame, rgb_frame.data, PIX_FMT_RGB24, 1600, 720);
sws_scale(final_sws_ctx, final_frame->data, 
          final_frame->linesize, 
          0, 720, 
          yuv422_final_frame->data, 
          yuv422_final_frame->linesize);

AVPacket encode_packet;
int got_output = 0;
av_init_packet(&encode_packet);
encode_packet.data = NULL;
encode_packet.size = 0;
int ret = avcodec_encode_video2(final_codec_context, 
                                &encode_packet, 
                                yuv422_final_frame, 
                                &got_output);

if (got_output ) {
    CString temp;

    temp.Format( "test%u.jpg", counter); 
    FILE* outputFile = fopen(temp, "wb");
    printf("Write frame (size=%5d)\n", encode_packet.size);
    fwrite(encode_packet.data, 1, encode_packet.size, outputFile);
    av_free_packet(&encode_packet);
    fclose(outputFile);

    counter++;  
}

【问题讨论】:

    标签: c++ opencv ffmpeg


    【解决方案1】:

    尝试显式设置final_frame 的步幅值:

    final_frame = avcodec_alloc_frame();
    avcodec_get_frame_defaults(final_frame);
    final_frame->format = PIX_FMT_RGB24;
    final_frame->width = 1600;
    final_frame->height = 720;
    final_frame->data[0] = rgb_frame.data;
    final_frame->linesize[0] = rgb_frame.step; // <<< stride 
    
    sws_scale(final_sws_ctx, final_frame->data, // etc...
    

    【讨论】:

      猜你喜欢
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 2015-12-13
      • 2017-02-19
      • 2018-11-28
      • 2015-02-16
      相关资源
      最近更新 更多