YUV422格式的数据转换成RGB24

     做相机类程序是经常遇到YUV格式转RGB格式的操作,以下是一个简略有效的算法

typedef unsigned char uint8_t;
void yuv422_to_rgb24(unsigned char *yuv422,unsigned char *rgb24, int width, int height)
{
int x,y;
uint8_t *yuv444;
yuv444 = (uint8_t *) malloc(sizeof(uint8_t) * width * height * 3);
for(x = 0,y = 0;x < width*height*2,y < width*height*3;x+=4,y+=6)
{
yuv444[y] = yuv422[x];
yuv444[y+1] = yuv422[x+1];
yuv444[y+2] = yuv422[x+3];
yuv444[y+3] = yuv422[x+2];
yuv444[y+4] = yuv422[x+1];
yuv444[y+5] = yuv422[x+3];
}
for(x = 0;x < width*height*3;x+=3)
{
rgb24[x+2] = yuv444[x] + 1402*(yuv444[x+2] - 128)/1000;
rgb24[x+1] = yuv444[x]-34414*(yuv444[x+1]-128)/100000 - 71414*(yuv444[x+2]-128)/100000;
rgb24[x] = yuv444[x] + 1772*(yuv444[x+1] - 128)/1000;
if (rgb24[x]>255)rgb24[x]=255;
if (rgb24[x]<0)rgb24[x]=0;
if (rgb24[x+1]>255)rgb24[x+1]=255;
if (rgb24[x+1]<0)rgb24[x+1]=0;
if (rgb24[x+2]>255)rgb24[x+2]=255;
if (rgb24[x+2]<0)rgb24[x+2]=0;
}
free(yuv444);
}


还有很重要的一点就是转换后的数据如果直接显示好像是会图片左右倒置的,在此也给出了解决方案

  


正常显示图像,实现代码为:
::StretchDIBits(hdc,0,0,m_nWidth,m_nHeight,0,0,m_nWidth,m_nHeight,m_pBits,m_pBMI,DIB_RGB_COLORS,SRCCOPY);
水平翻转显示图像,实现代码为:
::StretchDIBits(hdc,0,0,m_nWidth,m_nHeight,m_nWidth,0,-m_nWidth,m_nHeight,m_pBits,m_pBMI,DIB_RGB_COLORS,SRCCOPY);
垂直翻转显示图像,实现代码为:
::StretchDIBits(hdc,0,0,m_nWidth,m_nHeight,0,m_nHeight,m_nWidth,-m_nHeight,m_pBits,m_pBMI,DIB_RGB_COLORS,SRCCOPY);
水平与垂直翻转显示图像,实现代码为:
::StretchDIBits(hdc,0,0,m_nWidth,m_nHeight,m_nWidth,m_nHeight,-m_nWidth,-m_nHeight,m_pBits,m_pBMI,DIB_RGB_COLORS,SRCCOPY);

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将YUV422格式换为RGB格式,您可以使用FFmpeg库中的函数进行换。以下是一个使用FFmpeg进行YUV422RGB换的示例代码: ```c #include <stdio.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> int main() { int width = 640; // 输入图像的宽度 int height = 480; // 输入图像的高度 // 分配输入图像的内存 uint8_t* yuv422Buffer = (uint8_t*)malloc(width * height * 2); // 在这里填充YUV422数据yuv422Buffer // 分配输出图像的内存 uint8_t* rgbBuffer = (uint8_t*)malloc(width * height * 3); // 创建输入图像的AVFrame AVFrame* yuv422Frame = av_frame_alloc(); yuv422Frame->format = AV_PIX_FMT_YUV422P; yuv422Frame->width = width; yuv422Frame->height = height; av_image_fill_arrays(yuv422Frame->data, yuv422Frame->linesize, yuv422Buffer, AV_PIX_FMT_YUV422P, width, height, 1); // 创建输出图像的AVFrame AVFrame* rgbFrame = av_frame_alloc(); rgbFrame->format = AV_PIX_FMT_RGB24; rgbFrame->width = width; rgbFrame->height = height; av_image_fill_arrays(rgbFrame->data, rgbFrame->linesize, rgbBuffer, AV_PIX_FMT_RGB24, width, height, 1); // 创建换上下文 struct SwsContext* swsContext = sws_getContext(width, height, AV_PIX_FMT_YUV422P, width, height, AV_PIX_FMT_RGB24, 0, NULL, NULL, NULL); // 进行换 sws_scale(swsContext, yuv422Frame->data, yuv422Frame->linesize, 0, height, rgbFrame->data, rgbFrame->linesize); // 在这里可以使用rgbBuffer中的RGB数据 // 释放资源 free(yuv422Buffer); free(rgbBuffer); av_frame_free(&yuv422Frame); av_frame_free(&rgbFrame); sws_freeContext(swsContext); return 0; } ``` 请注意,此示例假设您已经填充了正确的YUV422数据yuv422Buffer。您需要根据实际情况修改输入图像的宽度和高度,并在换后使用rgbBuffer中的RGB数据进行进一步处理。还要确保您已经链接了FFmpeg库并包含所需的头文件。 请参考FFmpeg文档以获得更多关于使用FFmpeg进行图像格式换的详细信息和选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值