将yuyv格式图像转为IplImage(彩色)

来源:互联网 发布:大学生网络惨案 编辑:程序博客网 时间:2024/05/17 15:38

先介绍下YUYV

贴两篇文章:

http://blog.csdn.net/chen825919148/article/details/7921475

http://blog.chinaunix.net/uid-21410064-id-3248638.html


        摄像头格式为YUYV422,百度百科的介绍:

每个色差信道的抽样率是亮度信道的一半,所以水平方向的色度抽样率只是4:4:4的一半。对非压缩的8比特量化的图像来说,每个由两个水平方向相邻的像素组成的宏像素需要占用4字节内存。
下面的四个像素为:[Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]
存放的码流为:Y0 U0 Y1 V1 Y2 U2 Y3 V3
映射出像素点为:[Y0 U0 V1] [Y1 U0 V1] [Y2 U2 V3] [Y3 U2 V3]

YUYV与RGB 之间的转换关系:
R = 1.164*(Y-16) + 1.159*(V-128); 
G = 1.164*(Y-16) - 0.380*(U-128)+ 0.813*(V-128); 
B = 1.164*(Y-16) + 2.018*(U-128)); 

源码:


    cvNamedWindow("img_left");    cvNamedWindow("img_right");//    printf("process_image: start_addr:%d \n",addr);//    addr stores the address of yuyv data    unsigned char* ImgData;    int step = img_left->widthStep;        //img_left=cvCreateImage(img_size,IPL_DEPTH_8U,3);        ImgData = (unsigned char *)img_left->imageData;        for(int row=0;row<img_left->height;row++){            for(int col=0;col<(img_left->width);col++){                int tmpCol=3*col;                int Yvalue,Uvalue,Vvalue;                int tmp;                if((row*img_left->width+col)%2==0){      //match yuv value with the address                    Yvalue=*(unsigned char*)addr;                    Uvalue=*(unsigned char*)(addr+1);                    Vvalue=*(unsigned char*)(addr+3);                }else{                    Yvalue=*(unsigned char*)addr;                    Uvalue=*(unsigned char*)(addr-1);                    Vvalue=*(unsigned char*)(addr+1);                }                                //assign the rgb value                tmp=1.164*(Yvalue-16)+(1.779*(Uvalue-128));                if(tmp>255)   tmp=255;                else if(tmp<0)   tmp=0;                ImgData[row*step+tmpCol] = (unsigned char)tmp;  //dig blue                                tmp=1.164*(Yvalue-16)-0.3455*(Uvalue-128)-0.7169*(Vvalue-128);  //dig green                if(tmp>255)   tmp=255;                else if(tmp<0)   tmp=0;                ImgData[row*step+tmpCol+1]=(unsigned char)tmp;                                tmp=1.164*(Yvalue-16)+1.4075*(Vvalue-128);    //dig red                if(tmp>255)   tmp=255;                else if(tmp<0)   tmp=0;                ImgData[row*step+tmpCol+2]=(unsigned char)tmp;                addr += 2;            }        }




0 0