我正在尝试将 RGB 图像转换为 YUV。 我正在使用 openCV 加载图像。
我调用函数如下:
//I know IplImage is outdated
IplImage* im = cvLoadImage("1.jpg", 1);
//....
bgr2yuv(im->imageData, dst, im->width, im->height);
下面给出将彩色图像转换为yuv图像的函数。 我正在使用 ffmpeg 来做到这一点。
void bgr2yuv(unsigned char *src, unsigned char *dest, int w, int h)
{
AVFrame *yuvIm = avcodec_alloc_frame();
AVFrame *rgbIm = avcodec_alloc_frame();
avpicture_fill(rgbIm, src, PIX_FMT_BGR24, w, h);
avpicture_fill(yuvIm, dest, PIX_FMT_YUV420P, w, h);
av_register_all();
struct SwsContext * imgCtx = sws_getCachedContext(imgCtx,
w, h,(::PixelFormat)PIX_FMT_BGR24,
w, h,(::PixelFormat)PIX_FMT_YUV420P,
SWS_BICUBIC, NULL, NULL, NULL);
sws_scale(imgCtx, rgbIm->data, rgbIm->linesize,0, h, yuvIm->data, yuvIm->linesize);
av_free(yuvIm);
av_free(rgbIm);
}
转换后输出错误。 我认为这是由于 IplImage 中发生了填充。 (我的输入图像宽度不是 4 的倍数)。
我更新了 linesize 变量,即使在那之后我也没有得到正确的输出。 当我使用宽度为 4 的倍数的图像时,它工作正常。
谁能告诉我代码中的问题是什么。
最佳答案
检查 IplImage::align或 IplImage::widthStep并使用这些来设置 AVFrame::linesize .例如,对于 RGB 帧,您可以设置:
frame->linesize[0] = img->widthStep;
dst
数组的布局可以是任何你想要的,这取决于你之后如何使用它。
关于c++ - FFmpeg 中 RGB 到 YUV 的转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40995105/