您的位置:首页 > 其它

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

2015-05-17 20:25 85 查看
先介绍下YUYV

贴两篇文章:



/article/2224576.html

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;
            }
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: