gpt4 book ai didi

c++ - opencv Mat 转换为 AVFrame

转载 作者:行者123 更新时间:2023-12-04 23:31:39 25 4
gpt4 key购买 nike

我在 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++;
}

最佳答案

尝试为 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...

关于c++ - opencv Mat 转换为 AVFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25097939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com