使用opencv将nv21 yuv yuyv数据转换为rgb 数据

网上有太多的yuv数据转换rgb数据的方法,但是用户自己写的程序真没有opencv 的快。

两种方法的比较如下


              gettimeofday(&tv, NULL);
               printf("millisecond: %ld \n", (tv.tv_sec * 1000 + tv.tv_usec / 1000)); // 毫秒
#if 1// MTH1   use opencv convert  need 1-2ms after test 
             Mat nv12(480 *3 /2,640, CV_8UC1,(unsigned char*)frame);//from camera  reading a frame format is NV12
             Mat bgr;//define a mat in rgb datas
        cvtColor(nv12,bgr,CV_YUV2BGR_NV12);//converto nv12 to bgr 
#endif
#if     MTH2 //user custom define    need 4ms after test
         //yuvtorgb0((unsigned char *)frame, 640, 480);
              NV12_or_NV21_to_rgb24(1, (unsigned char *)frame,(unsigned char *) rgbbuf,640, 480);//one frame to rgb buffer
              Mat bgr(480,640,CV_8UC3,(uchar*)rgbbuf);//rgb buffer to mat
        // dump_raw((uchar*)rgbbuf,640, 480);

#endif  //opencv is faster 4times than user custom define 

           gettimeofday(&tv, NULL);
               printf("millisecond: %ld  \n", (tv.tv_sec * 1000 + tv.tv_usec /1000)); // 毫秒

方法1是将nv12转换为bgr的,肥肠的方便,并且速度还是肥肠的快,时间是自定义的4倍多。
yuv420 nv12 yv12都可以借助此类方法实现转换RGB 或者bgr.



 
    //  Mat src(Height, width, CV_8UC2, (unsigned char*)srcdata);
    //  Mat dst(Height, width, 16, (unsigned char*)dstdata);
    //  cvtColor(src, dst, COLOR_YUV2RGB_UYVY);

        //yuyv to bgr data 这里通常是USB CAMERA的摄像头 YUV422 
        DEBUG(LOG_DEBUG,"i422 enc yuv start\n");
         Mat yuyv(480 ,640, CV_8UC2,(unsigned char*)frame);//from camera  reading a frame format is NV12
                Mat bgr;//define a mat in rgb datas
        cvtColor(yuyv,bgr,CV_YUV2BGR_YUYV);//converto   bgr  from yuyv 
            //注意长度发生变化 bgr.data的长度是 640*480*3 

          Mat temp;
          if(fra_che_cnt++ >= 10)
            {    
                   DEBUG(LOG_DEBUG,"detection start \n");
            fra_che_cnt =0;
            detectAndDisplay(bgr);  
            DEBUG(LOG_DEBUG,"detection  end \n");

            cvtColor(bgr,temp,COLOR_BGR2YUV_I420);
            picture_buf = temp.data;

            pPic_in->img.plane[0] = picture_buf;         //Y
            pPic_in->img.plane[1] = picture_buf+y_size;  //U
            pPic_in->img.plane[2] = picture_buf+(y_size*5/4);//V
            }else {
                    cvtColor(bgr,temp,COLOR_BGR2YUV_I420);
            picture_buf = temp.data;

            pPic_in->img.plane[0] = picture_buf;         //Y
            pPic_in->img.plane[1] = picture_buf+y_size;  //U
            pPic_in->img.plane[2] = picture_buf+(y_size*5/4);//V
            }


sitemap