[英]opencv Mat convert to AVFrame
我在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...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.