Skip to content

Instantly share code, notes, and snippets.

@mcarletti
Created November 11, 2019 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcarletti/38789efb2d6adf610c39c4b490175b58 to your computer and use it in GitHub Desktop.
Save mcarletti/38789efb2d6adf610c39c4b490175b58 to your computer and use it in GitHub Desktop.
RGB-YUV color conversion (Matlab code)
function [Y,U,V] = cvtRGB2YUV(R,G,B)
R = double(R);
G = double(G);
B = 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;
Y = uint8(Y);
U = uint8(U);
V = uint8(V);
end
function [R,G,B] = cvtYUV2RGB(Y,U,V)
Y = double(Y) - 16;
U = double(U) - 128;
V = double(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;
R = uint8(R);
G = uint8(G);
B = uint8(B);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment