【问题标题】:YUV420 to RGB color conversion ErrorYUV420 到 RGB 颜色转换错误
【发布时间】:2014-05-16 08:05:09
【问题描述】:

我是convertingYUV420 格式的图像,RGBopencv 中的图像,但我在conversion. 之后得到了Orange 彩色图像,我使用以下代码来执行此操作。我的代码有什么问题吗??

 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 + (int)(1.772f*V); 
    float G = Y - (int)(0.344f*V + 0.714f*U);
    float B = Y + (int)(1.402f*U);

    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));
  }
}

origImage -> YUV 图像,
img1 -> RGB 图像,

http://upload.wikimedia.org/wikipedia/en/0/0d/Yuv420.svg

是否有任何 opencv 函数可以将 YUV420 格式的 pixel 转换为相应的 RGB 像素? (不是整张图)

【问题讨论】:

    标签: c opencv image-processing color-scheme yuv


    【解决方案1】:

    我通过修改计算 R G B 值的公式得到答案, 这段代码运行良好

     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));
      }
    }
    

    【讨论】:

      【解决方案2】:

      第一个问题是使用过时的 c-api(它已经死了。请改用 c++)。

      第二个问题是编写自己的(缓慢且容易出错)像素循环

      为什么不使用:

      cvtColor(crs,dst, CV_YUV2BGR); // or CV_YUV2BGR_I420
      

      不是吗?

      【讨论】:

        猜你喜欢
        • 2020-12-18
        • 2020-01-03
        • 2011-09-27
        • 2022-12-22
        • 2011-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多