YUV420到RGB颜色转换错误 [英] YUV420 to RGB color conversion Error

查看:755
本文介绍了YUV420到RGB颜色转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

YUV420 格式的图片转换为 RGB opencv 中的图片,但我在转换后获得橙色彩色图像。我使用以下代码来做到这一点。我的代码有问题吗?

I am converting an image in YUV420 format to RGB image in opencv but im getting an Orange colored image after conversion. I used following code to do that. Is there any problem in my code ??

 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图像,

origImage -> YUV image,
img1 -> RGB image,

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

有没有opencv可以将YUV420格式的像素转换为相应的RGB像素的函数? (不是整个图像)

Is there any opencv function which can convert a pixel in YUV420 format to corresponding RGB pixel ? (not entire image)

推荐答案

我通过修改计算RGB值的公式得到了答案,
此代码正常工作罚款

I got answer by modifying the formula for calculating R G B values, This code is working fine

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

这篇关于YUV420到RGB颜色转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆