YUV420 to RGB color conversion Error

后端 未结 2 634
执念已碎
执念已碎 2021-02-06 16:14

I am converting an image in YUV420 format to RGB image in opencv but im getting an Orange colored image after

相关标签:
2条回答
  • 2021-02-06 17:08

    the 1st problem is using the outdated c-api (it's dead & gone. please use c++ instead).

    the 2nd problem is writing your own (slow and error prone) pixel loops

    why not use :

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

    instead ?

    0 讨论(0)
  • 2021-02-06 17:12

    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));
      }
    }
    
    0 讨论(0)
提交回复
热议问题