求大神指教 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 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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;
	}BmpHead;

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

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

    BmpHead m_BMPHeader;
	InfoHead  m_BMPInfoHeader;
    char bfType[2]={'B','M'};
	
    m_BMPHeader.imageSize=3*nWidth*nHeight+54;
    m_BMPHeader.blank=0;
    m_BMPHeader.startPosition=54;
 
    fwrite(bfType,1,sizeof(bfType),fp);
    fwrite(&m_BMPHeader.imageSize,1,sizeof(m_BMPHeader.imageSize),fp);
    fwrite(&m_BMPHeader.blank,1,sizeof(m_BMPHeader.blank),fp);
    fwrite(&m_BMPHeader.startPosition,1,sizeof(m_BMPHeader.startPosition),fp);
    
    m_BMPInfoHeader.Length=40; 
    m_BMPInfoHeader.width=nWidth;
    m_BMPInfoHeader.height=nHeight;
    m_BMPInfoHeader.colorPlane=1;
    m_BMPInfoHeader.bitColor=24;
    m_BMPInfoHeader.zipFormat=0;
    m_BMPInfoHeader.realSize=3*nWidth*nHeight;
    m_BMPInfoHeader.xPels=0;
    m_BMPInfoHeader.yPels=0;
    m_BMPInfoHeader.colorUse=0;
    m_BMPInfoHeader.colorImportant=0;
 
    fwrite(&m_BMPInfoHeader.Length,1,sizeof(m_BMPInfoHeader.Length),fp);
    fwrite(&m_BMPInfoHeader.width,1,sizeof(m_BMPInfoHeader.width),fp);
    fwrite(&m_BMPInfoHeader.height,1,sizeof(m_BMPInfoHeader.height),fp);
    fwrite(&m_BMPInfoHeader.colorPlane,1,sizeof(m_BMPInfoHeader.colorPlane),fp);
    fwrite(&m_BMPInfoHeader.bitColor,1,sizeof(m_BMPInfoHeader.bitColor),fp);
    fwrite(&m_BMPInfoHeader.zipFormat,1,sizeof(m_BMPInfoHeader.zipFormat),fp);
    fwrite(&m_BMPInfoHeader.realSize,1,sizeof(m_BMPInfoHeader.realSize),fp);
    fwrite(&m_BMPInfoHeader.xPels,1,sizeof(m_BMPInfoHeader.xPels),fp);
    fwrite(&m_BMPInfoHeader.yPels,1,sizeof(m_BMPInfoHeader.yPels),fp);
    fwrite(&m_BMPInfoHeader.colorUse,1,sizeof(m_BMPInfoHeader.colorUse),fp);
    fwrite(&m_BMPInfoHeader.colorImportant,1,sizeof(m_BMPInfoHeader.colorImportant),fp);
    fwrite(rgb_buffer,3*nWidth*nHeight,1,fp);
}
生成的bmp图片有的地方一片模糊的红 有的一片模糊的蓝
  • 打赏
  • 举报
回复
相关推荐
发帖
机器视觉
加入

4116

社区成员

图形图像/机器视觉
申请成为版主
帖子事件
创建了帖子
2013-01-25 06:21
社区公告
暂无公告