# 求大神指教 yuv420转rgb24 存bmp图像色彩出问题 求大神指教

lishi_1991 2013-01-25 06:21:38

``````void ConvertYUVToRGB(unsigned char *yuv, unsigned char *rgb, int width, int height)
{
int R,G,B;
int Y,U,V;
int x,y;
int nWidth = width>>1;
unsigned char *yuv420[3];
yuv420[0] = yuv;
yuv420[1] = yuv + width*height;
yuv420[2] = yuv + width*height*5/4;
//色度信号宽度
for (y=0;y<height;y++)
{
for (x=0;x<width;x++)
{
Y = *(yuv420[0] + y*width + x);
U = *(yuv420[1] + ((y>>1)*nWidth) + (x>>1));
V = *(yuv420[2] + ((y>>1)*nWidth) + (x>>1));

B= 1.164 * (Y - 16) + 2.018 * (U - 128);
G= 1.164 * (Y - 16) - 0.38 * (U - 128) - 0.813 * (V - 128);
R= 1.164 * (Y - 16) ;

//防止越界
if (R>255)R=255;
if (R<0)R=0;
if (G>255)G=255;
if (G<0)G=0;
if (B>255)B=255;
if (B<0)B=0;

*(rgb + ((height-y-1)*width + x)*3) = R;
*(rgb + ((height-y-1)*width + x)*3 + 1) = G;
*(rgb + ((height-y-1)*width + x)*3 + 2) = B;
}
}

}
``````

...全文
136 2 点赞 打赏 收藏 举报

2 条回复

fengbingchun 2013-01-29
OpenCV的cvCvtColor函数就可以直接实现了。
• 打赏
• 举报

lishi_1991 2013-01-25

``````void RGB2BMP(unsigned char *rgb_buffer,int nWidth,int nHeight,FILE*fp)
{
typedef unsigned char  BYTE;
typedef unsigned short WORD;

typedef struct
{
long imageSize;
long blank;
long startPosition;

typedef struct
{
long  Length;
long  width;
long  height;
WORD  colorPlane;
WORD  bitColor;
long  zipFormat;
long  realSize;
long  xPels;
long  yPels;
long  colorUse;
long  colorImportant;

typedef struct
{
BYTE  rgbBlue;
BYTE  rgbGreen;
BYTE  rgbRed;
BYTE  rgbReserved;
}RGBMixPlate;

char bfType[2]={'B','M'};

fwrite(bfType,1,sizeof(bfType),fp);

fwrite(rgb_buffer,3*nWidth*nHeight,1,fp);
}``````

• 打赏
• 举报

4116

2013-01-25 06:21