gpt4 book ai didi

opencv - 使用 ffmpeg 将 cv::Mat 图像从 BGR 转换为 YUV

转载 作者:行者123 更新时间:2023-12-02 17:46:00 25 4
gpt4 key购买 nike

我正在尝试将 BGR 图像转换为 YUV420P,但是当我尝试分别查看每个 YUV 平面时,this is what I see.

cv::Mat::data 和 AVFrame::data[0] 不应该以相同的方式打包吗?我应该可以直接做memcpy。我错过了什么吗?

有任何想法吗?

Mat frame;
VideoCapture cap;
if(!cap.open(0)){
return 0;
}

// capture a frame
cap >> frame;
if( frame.empty() ) return 0;

cv::Size s = frame.size();
int height = s.height;
int width = s.width;

// Creating two frames for conversion
AVFrame *pFrameYUV =av_frame_alloc();
AVFrame *pFrameBGR =av_frame_alloc();

// Determine required buffer size and allocate buffer for YUV frame
int numBytesYUV=av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width,
height,1);

// Assign image buffers
avpicture_fill((AVPicture *)pFrameBGR, frame.data, AV_PIX_FMT_BGR24,
width, height);

uint8_t* bufferYUV=(uint8_t *)av_malloc(numBytesYUV*sizeof(uint8_t));
avpicture_fill((AVPicture *)pFrameYUV, bufferYUV, AV_PIX_FMT_YUV420P,
width, height);

// Initialise Software scaling context
struct SwsContext *sws_ctx = sws_getContext(width,
height,
AV_PIX_FMT_BGR24,
width,
height,
AV_PIX_FMT_YUV420P,
SWS_BILINEAR,
NULL,
NULL,
NULL
);

// Convert the image from its BGR to YUV
sws_scale(sws_ctx, (uint8_t const * const *)pFrameBGR->data,
pFrameYUV->linesize, 0, height,
pFrameYUV->data, pFrameYUV->linesize);



// Trying to see the different planes of YUV
Mat MY = Mat(height, width, CV_8UC1);
memcpy(MY.data,pFrameYUV->data[0], height*width);
imshow("Test1", MY); // fail
Mat MU = Mat(height/2, width/2, CV_8UC1);
memcpy(MU.data,pFrameYUV->data[1], height*width/4);
imshow("Test2", MU); // fail
Mat MV = Mat(height/2, width/2, CV_8UC1);
memcpy(MV.data,pFrameYUV->data[2], height*width/4);
imshow("Test3", MV); // fail

waitKey(0); // Wait for a keystroke in the window

最佳答案

对于 sws_scale()第三个参数不应该是 pFrameYUV->linesize而是pFrameBGR->linesize ,即指向 的指针宽度*3 .

关于opencv - 使用 ffmpeg 将 cv::Mat 图像从 BGR 转换为 YUV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35907094/

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