How to convert RGB -> YUV -> RGB (both ways)

Yes, invertible transformations exist.

equasys GmbH posted invertible transformations from RGB to YUV, YCbCr, and YPbPr, along with explanations of which situation each is appropriate for, what this clamping is really about, and links to references. (Like a good SO answer.)

For my own application (jpg images, not analog voltages) YCbCr was appropriate, so I wrote code for those two transformations. Indeed, there-and-back-again values differed by less than 1 part in 256, for many images; and the before-and-after images were visually indistinguishable.

PIL’s colour space conversion YCbCr -> RGB gets credit for mentioning equasys’s web page.

Other answers, that could doubtfully improve on equasys’s precision and concision:

  • https://code.google.com/p/imagestack/ includes rgb_to_x and x_to_rgb
    functions, but I didn’t try to compile and test them.

  • Cory Nelson’s answer links to code with similar functions, but it says that
    inversion’s not possible in general, contradicting equasys.

  • The source code of FFmpeg, OpenCV, VLFeat, or ImageMagick.

2019 Edit: Here’s the C code from github, mentioned in my comment.

void YUVfromRGB(double& Y, double& U, double& V, const double R, const double G, const double B)
{
  Y =  0.257 * R + 0.504 * G + 0.098 * B +  16;
  U = -0.148 * R - 0.291 * G + 0.439 * B + 128;
  V =  0.439 * R - 0.368 * G - 0.071 * B + 128;
}
void RGBfromYUV(double& R, double& G, double& B, double Y, double U, double V)
{
  Y -= 16;
  U -= 128;
  V -= 128;
  R = 1.164 * Y             + 1.596 * V;
  G = 1.164 * Y - 0.392 * U - 0.813 * V;
  B = 1.164 * Y + 2.017 * U;
}

Leave a Comment