sws_scale YUV --gt; RGB distorted image(sws_scale YUV --RGB 失真图像)
问题描述
我想将 YUV420P 图像(从 H.264 流接收)转换为 RGB,同时使用 调整其大小sws_scale.
原图大小为480 × 800.只需使用相同尺寸进行转换即可.
但是当我尝试改变尺寸时,我得到一个扭曲的图像,具有以下模式:
I want to convert YUV420P image (received from H.264 stream) to RGB, while also resizing it, using sws_scale.
The size of the original image is 480 × 800. Just converting with same dimensions works fine.
But when I try to change the dimensions, I get a distorted image, with the following pattern:
- 更改为
481 × 800会产生扭曲的黑白图像,看起来像是从中间切掉了 482 × 800会更失真483 × 800失真但有颜色484 × 800没问题(正确缩放).
- changing to
481 × 800will yield a distorted B&W image which looks like it's cut in the middle 482 × 800will be even more distorted483 × 800is distorted but in color484 × 800is ok (scaled correctly).
现在遵循这种模式 - 只有在除以 4 之间的差异时缩放才能正常工作.
Now this pattern follows - scaling will only work fine if the difference between divides by 4.
这是我解码和转换图像的方式的示例代码.所有方法都显示成功".
Here's a sample code of the way that I decode and convert the image. All methods show "success".
int srcX = 480;
int srcY = 800;
int dstX = 481; // or 482, 483 etc
int dstY = 800;
AVFrame* avFrameYUV = avcodec_alloc_frame();
avpicture_fill((AVPicture *)avFrameYUV, decoded_yuv_frame, PIX_FMT_YUV420P, srcX , srcY);
AVFrame *avFrameRGB = avcodec_alloc_frame();
AVPacket avPacket;
av_init_packet(&avPacket);
avPacket.size = read; // size of raw data
avPacket.data = raw_data; // raw data before decoding to YUV
int frame_decoded = 0;
int decoded_length = avcodec_decode_video2(g_avCodecContext, avFrameYUV, &frame_decoded, &avPacket);
int size = dstX * dstY * 3;
struct SwsContext *img_convert_ctx = sws_getContext(srcX, srcY, SOURCE_FORMAT, dstX, dstY, PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
avpicture_fill((AVPicture *)avFrameRGB, rgb_frame, PIX_FMT_RGB24, dstX, dstY);
sws_scale(img_convert_ctx, avFrameYUV->data, avFrameYUV->linesize, 0, srcY, avFrameRGB->data, avFrameRGB->linesize);
// draws the resulting frame with windows BitBlt
DrawBitmap(hdc, dstX, dstY, rgb_frame, size);
sws_freeContext(img_convert_ctx);
推荐答案
制作位图图片时,图片的宽度必须是4的倍数.
When you make a bitmap image, the width of image MUST be multiple of 4.
所以你必须改变宽度,如 480、484、488、492 ...
So you have to change width like 480, 484, 488, 492 ...
这里是变4的倍数的方法
Here is method to change to multiple of 4
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
void main()
{
BITMAPFILEHEADER bmFileHeader;
BITMAPINFOHEADER bmInfoHeader;
// load image
// ...
// when you use the method, put parameter like this.
int tempWidth = WIDTHBYTES(width * bmInfoHeader.biBitCount);
}
希望你能解决问题.
这篇关于sws_scale YUV -->RGB 失真图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:sws_scale YUV -->RGB 失真图像
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
