繁体   English   中英

sws_scale YUV --> RGB 失真图像

[英]sws_scale YUV --> RGB distorted image

我想将YUV420P图像(从H.264流接收)转换为RGB ,同时还使用sws_scale调整其大小。
原始图像的大小为480 × 800 只需使用相同尺寸进行转换即可。
但是当我尝试改变尺寸时,我得到一个扭曲的图像,具有以下模式:

  • 更改为481 × 800会产生扭曲的黑白图像,看起来像是在中间被切掉了
  • 482 × 800会更失真
  • 483 × 800失真但有颜色
  • 484 × 800可以(正确缩放)。

现在遵循这种模式 - 只有在除以 4 之间的差异时,缩放才能正常工作。

这是我解码和转换图像的方式的示例代码。 所有方法都显示“成功”。

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 的倍数。

所以你必须改变宽度,比如 480, 484, 488, 492 ...

这是更改为 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);
}

我希望你能解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM