繁体   English   中英

如何优化YUV到RGB颜色转换代码

[英]How to optimize YUV to RGB color conversion code

我已经编写了将YUV420P中的图像转换为RGB的函数,但是将图像(大小:1280 x 720)转换为RGB需要30毫秒,但是当我使用ffmpeg函数( 这样 )将YUV图像转换为RGB时同一张图片只需要2毫秒的时间。 我的代码有什么问题? 如何优化我编写的代码? 我的代码如下

 int step = origImage->widthStep;
 uchar *data = (uchar *)origImage->imageData; 
 int size = origImage->width * origImage->height;
 IplImage* img1 = cvCreateImage(cvGetSize(origImage), IPL_DEPTH_8U, 3);

    for (int i = 0; i<origImage->height; i++)
    {
      for (int j=0; j<origImage->width; j++)
      {
        float Y = data[i*step + j];
        float U = data[ (int)(size + (i/2)*(step/2)  + j/2) ];
        float V = data[ (int)(size*1.25 + (i/2)*(step/2) + j/2)];

        float R = Y + 1.402 * (V - 128);
        float G = Y - 0.344 * (U - 128) - 0.714 * (V - 128);
        float B = Y + 1.772 * (U - 128);


        if (R < 0){ R = 0; } if (G < 0){ G = 0; } if (B < 0){ B = 0; }
        if (R > 255 ){ R = 255; } if (G > 255) { G = 255; } if (B > 255) { B = 255; }

        cvSet2D(img1, i, j,cvScalar(B,G,R));
      }
    }

在这里,尝试此操作(应减少到25毫秒):

 int step = origImage->widthStep;
 uchar *data = (uchar *)origImage->imageData; 
 int size = origImage->width * origImage->height;
 IplImage* img1 = cvCreateImage(cvGetSize(origImage), IPL_DEPTH_8U, 3);

    int stepDb2=step /2;
    float sizeMb1d25=size*1.25 ;
    int origImagePTheight=origImage->height;
    int origImagePTwidth=origImage->width;
    for (int i = 0; i<origImagePTheight; i++)
    {
      float idb2=i/2;
      int iStep=i*step;
      for (int j=0; j<origImagePTwidth; j++)
      {
        float variable=idb2*stepDb2  + j/2;
        float Y = data[iStep + j];
        float U = -128 + data[ (int)(size + variable) ];
        float V = -128 + data[ (int)(sizeMb1d25 + variable)];

        float R = Y + 1.402 * V ;
        float G = Y - 0.344 * U - 0.714 * V;
        float B = Y + 1.772 * U;

        R= R * !(R<0);
        G= G * !(G<0);
        B= B * !(B<0);

        R=R*(!(R>255)) + 255 * (R>255);
        G=G*(!(G>255)) + 255 * (G>255);
        B=B*(!(B>255)) + 255 * (B>255);

        cvSet2D(img1, i, j,cvScalar(B,G,R));
      }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM