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

C++ 如何优化YUV到RGB颜色转换代码,c++,opencv,optimization,ffmpeg,C++,Opencv,Optimization,Ffmpeg,我已经编写了一个函数来将YUV420P中的图像转换为RGB,但是将图像(大小:1280 x 720)转换为RGB需要30毫秒,但是当我使用ffmpeg函数()将YUV图像转换为RGB时,同一图像只需要2毫秒。我的代码有什么问题?如何优化我编写的代码?? 我的代码如下 int step = origImage->widthStep; uchar *data = (uchar *)origImage->imageData; int size = origImage->wid

我已经编写了一个函数来将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));
      }
    }
int step=origImage->widthStep;
uchar*数据=(uchar*)原始图像->图像数据;
int size=origImage->width*origImage->height;
IplImage*img1=cvCreateImage(cvGetSize(origImage),IPL_DEPTH_8U,3);
for(int i=0;iheight;i++)
{
for(int j=0;jwidth;j++)
{
浮动Y=数据[i*步+j];
浮点数U=数据[(int)(大小+(i/2)*(步长/2)+j/2)];
浮点V=数据[(int)(大小*1.25+(i/2)*(步长/2)+j/2)];
浮点数R=Y+1.402*(V-128);
浮点数G=Y-0.344*(U-128)-0.714*(V-128);
浮点数B=Y+1.772*(U-128);
如果(R<0){R=0;}如果(G<0){G=0;}如果(B<0){B=0;}
如果(R>255){R=255;}如果(G>255){G=255;}如果(B>255){B=255;}
cvSet2D(img1,i,j,cvScalar(B,G,R));
}
}
在此处,尝试此操作(应减少到25毫秒):

int step=origImage->widthStep;
uchar*数据=(uchar*)原始图像->图像数据;
int size=origImage->width*origImage->height;
IplImage*img1=cvCreateImage(cvGetSize(origImage),IPL_DEPTH_8U,3);
int stepdb=step/2;
浮子尺寸EMB1D25=尺寸*1.25;
int origImagePTheight=origImage->height;
int origImagePTwidth=origImage->width;
对于(inti=0;i255))+255*(B>255);
cvSet2D(img1,i,j,cvScalar(B,G,R));
}
}

我将从使用整数数学而不是浮点开始。Then see应移至code review。找到删除6个if语句的方法,确保算术生成的值在正确的范围内,正如Mark Ransom所说的,使用integer math@Park Young Bae:这是错误的。。。您可以改进代码(例如,通过减少浮点/双精度/整数/字符转换),但老实说,除非您像ffmpeg那样使用花式汇编/MMX技巧,否则您的速度不会像ffmpeg那样快。但是也许你可以使用OpenCV的
cvtColor
函数,这应该是相当优化的。时间减少了2-3毫秒当你评论cvSet2D(img1,i,j,cvScalar(B,G,R))时会发生什么;out?如果I注释浮点R=Y+1.402*V,则有1毫秒的缩减;浮子G=Y-0.344*U-0.714*V;浮点数B=Y+1.772*U;和cvSet2D()处理时间小于1毫秒如果我只注释计算R,G,B的行,则处理时间变为18毫秒((float R=Y+1.402*V;float G=Y-0.344*U-0.714*V;float B=Y+1.772*U;)
 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));
      }
    }